Re: Reading individual bytes from a string
Re: Reading individual bytes from a string
- Subject: Re: Reading individual bytes from a string
- From: Douglas Davidson <email@hidden>
- Date: Mon, 17 Apr 2006 10:56:20 -0700
On Apr 17, 2006, at 10:02 AM, Phil Faber wrote:
Newbie question. I've written a piece of code (see below) which
prompts the user to select a file, puts the contents of that file
into a variable [fileContents], and then (attempts) to copy out a
single character from that file into a separate field [statusText].
(My next goal will be to get the programme to examine EVERY
character in the file, one after the other, and then write those
characters back to a new file)
The line which tries to access character 8 results in: "warning:
passing argument 1 of 'setStringValue:' makes pointer from integer
without a cast" which I guess means I've misunderstood how to use
the command. From my (newbie) understanding of Cocoa, I think that
this means I'm mixing field types.
Can anyone please guide me?
Your immediate problem is that a string and a character in a string
are different things. You can create a string from a single
character using methods like stringWithCharacters:length: or
stringWithFormat: (with format %C).
You have some other problems, however. First,
stringWithContentsOfFile: is deprecated, because it is ambiguous; it
does not specify the encoding of the file. In general, you need to
know the encoding of a text file in order to read it properly. You
should also check to make sure the string exists and has at least 9
characters before looking for characterAtIndex:8.
Also, the things you are reading from the string are not bytes; they
are Unicode characters in UTF-16. In general, it is best to avoid
where possible dealing with individual characters in a string,
because they do not necessarily stand by themselves; the minimum
semantically meaningful unit is a composed character cluster, as
given by e.g. rangeOfComposedCharacterSequenceAtIndex:. That is why
most Cocoa APIs are written in terms of strings rather than characters.
Douglas Davidson
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden