NOOB Alert: Parsing a string

Discussion in 'iOS Programming' started by efterdal, Jan 18, 2010.

  1. macrumors newbie

    Joined:
    Jan 18, 2010
    #1
    Hi, Everyone


    I am a NOOB but are learning fast, so please speak to the dumbest person in the audience :p

    I am struggling to parse out some text from a string, the string looks like this.

    MetaData: Title='Macrumors';Url='http://www.macrumors.com';
    What I want is the text between ' aka just Macrumors from 'Macrumors'

    Thank you in advance.
     
  2. macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
  3. thread starter macrumors newbie

    Joined:
    Jan 18, 2010
  4. macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #4
    Using the NSScanner class is probably the easiest way to parse a string.
     
  5. thread starter macrumors newbie

    Joined:
    Jan 18, 2010
    #5
    Hi,

    I have looked at the RSS Tutorial, and think I am onto something, not quite sure. :)

    Here is the output that is printed in the console:

    MetaData: StreamTitle='Members Of Mayday - Sonic Empire';StreamUrl='http://www.radiovolum.no';

    That string is separated in an Array with the pointers metaData.text and metaData2.text

    What I am trying to do is to remove everything that I don't want before its printed out in the GUI.

    Code:
    // clean up the link - ugly formating...
    metaData = [metaData stringByReplacingOccurrencesOfString:@"StreamTitle=" withString:@""];
    metaData = [metaData stringByReplacingOccurrencesOfString:@"'" withString:@""];
    metaData = [metaData stringByReplacingOccurrencesOfString:@"StreamUrl=" withString:@""];
    
    Here is the code of the Array :

    Code:
    - (void)metaDataUpdated:(NSString *)metaData 
    
    {
    	NSArray *listItems = [metaData componentsSeparatedByString:@";"];
    	
    	if ([listItems count] > 0)
    		metadata.text = [listItems objectAtIndex:0];
    	if ([listItems count] > 1)
    		metadata2.text = [listItems objectAtIndex:1];
    }
    
    
    Thank you everyone, I am really happy for that you guys devote time to answer my NOOB posts:)
     
  6. macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #6
    This is how you could do it using NSScanner.

    Code:
    NSString *string = @"StreamTitle='Members Of Mayday - Sonic Empire';StreamUrl='http://www.radiovolum.no';";
    	
    NSScanner *scanner = [[NSScanner alloc] initWithString:string];
    NSString *title, *url;
    	
    [scanner scanUpToString:@"StreamTitle='" intoString:nil];
    [scanner scanString:@"StreamTitle='" intoString:nil];
    [scanner scanUpToString:@"'" intoString:&title];
    	
    [scanner scanUpToString:@"StreamUrl='" intoString:nil];
    [scanner scanString:@"StreamUrl='" intoString:nil];
    [scanner scanUpToString:@"'" intoString:&url];
    	
    NSLog(@"title = '%@', url = '%@'", title, url);
    
     
  7. thread starter macrumors newbie

    Joined:
    Jan 18, 2010
    #7
    I manage to get the clean metadata to print out to the console, but I can't get it to be viewed to my UILabel's.


    I get this error now :

    [​IMG]
     

    Attached Files:

    • meta.png
      meta.png
      File size:
      58.5 KB
      Views:
      409
  8. thread starter macrumors newbie

    Joined:
    Jan 18, 2010
    #8
    I belive I am quite lost :(
     

    Attached Files:

  9. macrumors member

    Joined:
    Jul 2, 2009
    #9
    The problem is that you are already using a variable called metaDataTitle in your class (it looks like it's a UILabel). You need to change the name of the local NSString variable to something else. The second error is simply because you're not passing a string anymore - you're passing the UILabel...which won't work.
     
  10. thread starter macrumors newbie

    Joined:
    Jan 18, 2010
    #10
    Do you mean something like this? :

    Code:
    - (void)metaDataUpdated:(NSString *)metaData
    
    {
    
    	NSArray *listItems = [metaData componentsSeparatedByString:@";"];
    	
    	if ([listItems count] > 0)
    		metadata.text = [listItems objectAtIndex:0];
    	if ([listItems count] > 1)
    		metadata2.text = [listItems objectAtIndex:1];
    	
    	NSString *cleanMeta = metaData;
    	NSScanner *scanner = [[NSScanner alloc] initWithString:cleanMeta];
    
    	[scanner scanUpToString:@"StreamTitle='" intoString:nil];
    	[scanner scanString:@"StreamTitle='" intoString:nil];
    	[scanner scanUpToString:@"'" intoString:&cleanMeta];
    	
    	NSLog(@"%@", cleanMeta);
    }
     
  11. macrumors member

    Joined:
    Jul 2, 2009
    #11
    Your method is receiving a variable called metadata which is an NSString. NSString doesn't have a property called text. So this will not work.

    I think you need to work out what is a UILabel and what is an NSString as you're confusing the two. I find that naming my variables so that it's easy to find out what they are helps - for example metaDataLabel.
     
  12. thread starter macrumors newbie

    Joined:
    Jan 18, 2010
    #12
    Yes, Indeed. I am quite confused, but the output in the cosole is right, so somthing right happens :)

    Code:
    - (void)metaDataUpdated:(NSString *)metaData
    
    {
    
    	NSArray *listItems = [metaData componentsSeparatedByString:@";"];
    	
    	if ([listItems count] > 0)
    		metadata.text = [listItems objectAtIndex:0];
    	if ([listItems count] > 1)
    		metadata2.text = [listItems objectAtIndex:1];
    
    
    	NSScanner *scanner = [[NSScanner alloc] initWithString:metaData];
    	
    	[scanner scanUpToString:@"StreamTitle='" intoString:nil];
    	[scanner scanString:@"StreamTitle='" intoString:nil];
    	[scanner scanUpToString:@"'" intoString:&metaData];
    	
    	
    	NSLog(@"%@", metaData);
    }
    Code:
    2010-01-25 17:00:10.787 RadioVolum.no[41822:600f] MetaData: StreamTitle='Mark Knight, Adam K, Soha - From The Speaker - Original Dub Mix';StreamUrl='http://www.radiovolum.no';
    2010-01-25 17:00:10.787 RadioVolum.no[41822:207] Mark Knight, Adam K, Soha - From The Speaker - Original Dub Mix
     
  13. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    So, your parameter is named metaData and you have an ivar named metadata? That's seems confusing.
     

Share This Page