How to split a delimited NSString into NSArray
Asked Answered
W

1

12

I have a little problem when I try to split delimited string into an Array. Basically, I want to pass result from MECARD QRCode and add new entry to addressBook.

Here is my code (for "FirstName" field only) : :

NSLog(@"found CB");
NSLog(@"_code.text = %@", code.content);
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();

NSString *_n = [NSString stringWithFormat:@"_code.text = %@", code.content];
NSArray *n = [_n componentsSeparatedByString:@";"];
NSLog(@"_code.text = %@",n);

ABRecordSetValue(person, kABPersonFirstNameProperty, _name, nil);

ABAddressBookAddRecord(addressBook, person, nil);
CFRelease(addressBook);

ABNewPersonViewController *c = [[ABNewPersonViewController alloc] init];
[c setNewPersonViewDelegate:self];
[c setDisplayedPerson:person];
CFRelease(person);
[self.navigationController pushViewController:c animated:YES];
[c release];

MECARD QRCode is well decoded & viewController appears... But all the URL (as : "MECARD:N:name;ORG:company;TEL:89878978; ...Etc.) goes in first field (FistName field)...

What am missing to separate my MECARD URL & send right data in right field?

Whither answered 1/6, 2013 at 15:11 Comment(0)
N
26

Hope it helps

NSArray *chunks = [string componentsSeparatedByString: @";"];
Neal answered 1/6, 2013 at 15:21 Comment(3)
Thanks for your tip, BlueConga, but if you read my code snippet, you can see : "NSArray *n = [_n componentsSeparatedByString:@";"];", cause in MECARD, data is separated by ";", not by ",". Anyway, replace ";" by "," doesn't work anymore...Whither
hmm in example you have missed "n" - it is [_ componentsSeparatedByString:@";"]; and maybe should be [_n ... ?Neal
u're right : my sample is corrected now : "NSArray *n = [_n componentsSeparatedByString:@";"];". But my split problem is still alive... :-(Whither

© 2022 - 2024 — McMap. All rights reserved.