Reading .css in iphone

Discussion in 'iOS Programming' started by arnieterm, Jul 5, 2010.

  1. macrumors regular

    Joined:
    Aug 28, 2008
    #1
    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. thread starter macrumors regular

    Joined:
    Aug 28, 2008
    #3
    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
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     

Share This Page