• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Reading individual bytes from a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Setting a custom NSImageCell (From: Jim Turner <email@hidden>)
 >Re: Setting a custom NSImageCell (From: Mike Abdullah <email@hidden>)
 >Reading individual bytes from a string (From: Phil Faber <email@hidden>)

  • Prev by Date: Re: Resolving a alias w/o mounting file systems
  • Next by Date: addObserver:forKeyPath:options:context
  • Previous by thread: Re: Switching from NSString to NSData for byte input from file. (Was: Reading individual bytes from a string)
  • Next by thread: Adding bindings to a custom view - "simple" implementation
  • Index(es):
    • Date
    • Thread