Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

arnieterm

macrumors regular
Original poster
Aug 28, 2008
201
0
I have tried parsing a locally stored .css file using the below given code but it gives error

Code:
NSString* strFilePath = [ [ NSBundle mainBundle] pathForResource:@"stylesheet" ofType:@"css"];
		NSData* data = [ [ NSData alloc] initWithContentsOfFile:strFilePath];
		NSString* string;
		NSDictionary *dictionary;
		string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
		dictionary = [string propertyList];
		[string release];

The error is as given below
Code:
		 NSParseErrorException', reason: 'XML parser error:
		 Unexpected character . at line 1
		 Old-style plist parser error:
		 Unexpected ';' or '=' after key at line 2
Whats wrong with the code snipet? Is there any other way to parse the .css file on iphone?
Thanks
Arnieterm
 
I've asked the mods to move this to iPhone Programming for you...

I am a little confused as to what you think will happen. You get a path to a CSS file. You load that into NSData object then turn that into a NSString (why: you could have just loaded it straight to the string). You then try to create a NSDictionary from this string. This is where I am confused. You turn any arbitrary string into a dictionary. As noted in the documentation for that method you need "a string in a property list format". A CSS file is not property list format. Which is why you get parse errors.

Oh and you are leaking both your NSData and NSString objects.
 
Thanks for your quick reply
I think I need to read the css entirely into an NSString and then iterate over it character by character to construct a dictionary from it, right?
In the last I need an NSDictionary constructed from this NSString iteration.
Though not tried yet, can we use NSKeyedUnarchive here?
Thanks again
Arnieterm
 
Though not tried yet, can we use NSKeyedUnarchive here?

No as you don't have a keyed archive. You either have to parse the CSS yourself from scratch, use a third-party CSS parsing library (I don't know of any so I can't suggest one before you ask), or look at the documentation and see if there is anyway to get the UIWebView stuff to do what you want.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.