PDA

View Full Version : NOOB Alert: Parsing a string




efterdal
Jan 18, 2010, 04:33 AM
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.



iphonejudy
Jan 18, 2010, 05:02 AM
Chk this
http://theappleblog.com/2008/08/04/tutorial-build-a-simple-rss-reader-for-iphone/

efterdal
Jan 18, 2010, 05:15 AM
Thank you, i will look at that one :)

bredell
Jan 18, 2010, 06:07 AM
Using the NSScanner class is probably the easiest way to parse a string.

efterdal
Jan 18, 2010, 07:40 AM
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.


// 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 :

- (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:)

bredell
Jan 18, 2010, 03:39 PM
This is how you could do it using NSScanner.


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);

efterdal
Jan 25, 2010, 05:24 AM
This is how you could do it using NSScanner.


NSString *string = metaData;

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);


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 :

http://att.macrumors.com/attachment.php?attachmentid=212130&stc=1&d=1264422955

efterdal
Jan 25, 2010, 06:57 AM
I belive I am quite lost :(

Troglodyte
Jan 25, 2010, 07:27 AM
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.

efterdal
Jan 25, 2010, 07:37 AM
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.

Do you mean something like this? :

- (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);
}

Troglodyte
Jan 25, 2010, 09:01 AM
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.

efterdal
Jan 25, 2010, 10:04 AM
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.

Yes, Indeed. I am quite confused, but the output in the cosole is right, so somthing right happens :)

- (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);
}

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

dejo
Jan 25, 2010, 10:15 AM
So, your parameter is named metaData and you have an ivar named metadata? That's seems confusing.