• 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
Two Questions About Parsing a String
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Two Questions About Parsing a String


  • Subject: Two Questions About Parsing a String
  • From: Ian Jackson <email@hidden>
  • Date: Tue, 22 Jul 2008 21:09:35 +1200

Hi,

I'm looking for some help parsing a string from a file.

Firstly, getting the string is causing some issues. I read the String Programming Guide for Cocoa, and got this:

NSString *path = ...;
NSData *data = [NSData dataWithContentsOfFile:path];

// assuming data is in UTF8
NSString *string = [NSString stringWithUTF8String:[data bytes]];

along with a warning that you must not use:

 stringWithContentsOfFile:

So, I tried to do as I was told, but using the first example, I find that successfully getting a string from the data is quite random. When I start the application, my first attempt to load the file may or may not result and a string being created, but if not, repeatedly trying to load the file eventually works and I get the string. I'm not sure what the encoding is, (I'm trying to create a .obj reader), but setting it at UTF8 in Xcode doesn't help.
So, I took a peek at the dark side, and tried the soon to be deprecated stringWithContentsOfFile: which works fine all the time. Any thoughts on why the first example might be failing randomly?


My second question is NSScanner related. Going through the .obj file, I have managed to get a system going where it scans for key characters (e.g. "v" for vertex, "#" for commented text etc). Roughly:

	while ([theScanner isAtEnd] == NO)
	{
		[theScanner scanUpToCharactersFromSet:vCharacters intoString:NULL];
		[theScanner scanCharactersFromSet:vCharacters intoString:&testString];

if ([testString isEqualToString:@"#"]) {
[theScanner scanUpToString:@"\n" intoString:&dumpString];
NSLog(@"dumpString is %@", dumpString);
}

else if ([testString isEqualToString:@"o"]) {
[theScanner scanUpToString:@"\n" intoString:&theObjectName];
NSLog(@"name: %@", theObjectName);
}

else if ([testString isEqualToString:@"v"]) {
[theScanner scanFloat:&xVert];
[theScanner scanFloat:&yVert];
[theScanner scanFloat:&zVert];
NSLog(@"Vertex %i is: x = %f, y = %f, z = %f", ++i, xVert, yVert, zVert);
}
}
}


However, the files also include identifiers such as "usemtl", which could appear at any time. So any ideas how you go about searching for a set of characters, and a set of strings simultaneously? i.e. how do I search for the characters without momentarily ignoring the strings or vice versa? This seems to be quite straightforward with fscanf, but it seems a bit odd going to C, when I'm trying to do this in Objective- C.

Any help with either question would be much appreciated.

Thank you,

Ian.
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Two Questions About Parsing a String
      • From: Jens Alfke <email@hidden>
    • Re: Two Questions About Parsing a String
      • From: PGM <email@hidden>
  • Prev by Date: Re: Weak link usage? @property?
  • Next by Date: Re: File types opened by NSDocument architecture
  • Previous by thread: Re: File types opened by NSDocument architecture
  • Next by thread: Re: Two Questions About Parsing a String
  • Index(es):
    • Date
    • Thread