I have a two word string in another view controller containing a user defined first and last name
NSString *userName = ([self hasAttributeWithName:kContractorName] ? [self attributeWithName:kContractorName].value : [self.certificate.contractor.name uppercaseString]);
when retrieving this string in another view controller I want to extract only the first name.
I researched SO on using scanner and found a very helpful answer here: Objective C: How to extract part of a String (e.g. start with '#'), and im almost there.
The problem is I can only seem extract the second name with my variation on the origial code. Im scanning my string up to the space between first and second name, this returns the second name fine. Just need a nudge now on how to set this to extract the first name instead of the second
NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:userName];
[scanner scanUpToString:@" " intoString:nil]; // Scan all characters before
while(![scanner isAtEnd]) {
NSString *name = nil;
[scanner scanString:@" " intoString:nil]; // Scan the character
if([scanner scanUpToString:@" " intoString:&name]) {
// If the space immediately followed the , this will be skipped
[substrings addObject:name];
}
[scanner scanUpToString:@" " intoString:nil]; // Scan all characters before next
}