PDA

View Full Version : Reading .css in iphone




arnieterm
Jul 5, 2010, 08:29 AM
I have tried parsing a locally stored .css file using the below given code but it gives error


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

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



robbieduncan
Jul 5, 2010, 08:34 AM
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 (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/initWithContentsOfFile:encoding:error:)). 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.

arnieterm
Jul 5, 2010, 09:11 AM
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

robbieduncan
Jul 5, 2010, 09:14 AM
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.