0

I'm having trouble with this method, more specifically I'm having trouble with my logic. I'm trying to combine two array's firstName and lastName both sequentially matching up. I was thinking I could us a for loop to iterate through the array count and then combine the array's using arrayByAddingObjectsFromArray.

Unfortunately it seems that this part throws up an error : __NSCFConstantString arrayByAddingObjectsFromArray:]: unrecognized selector sent to instance 0x10c824218

Any ideas why this is? What does it mean by selector; is that it doesn't like the array i'm trying to pass to it?

- (NSString *)badgeForSpeaker:(NSString *)speaker{ NSArray *firstName = @[@"Adele", @"Edsger", @"Joan", @"Clarence", @"Margaret", @"George", @"Tim", @"Jean"]; NSArray *lastName = @[@"Goldberg",@"Dijkstra",@"Clarke",@"Ellis",@"Hamilton",@"Boole",@"Berners-Lee",@"Bartik"]; NSString *uppercaseString = [speaker copy]; NSMutableString *hello = [[NSMutableString alloc]init]; for (NSUInteger i =0; i < [lastName count] ; i++) { uppercaseString = [lastName[i] capitalizedString]; hello = [@"Hello, my name is " mutableCopy]; firstName = [firstName[i] arrayByAddingObjectsFromArray:lastName[i]]; NSString *fullNameString = [firstName componentsJoinedByString:@" "]; [hello appendFormat:@"%@",fullNameString]; NSLog(@"%@",hello); } return hello; } 
4
  • 2
    firstName[i] returns NSString object. the method arrayByAddingObjectsFromArray belongs to NSArray Commented Oct 22, 2015 at 17:07
  • Can you tell us what you expect Hello to be ? Commented Oct 22, 2015 at 17:14
  • My fault, I expect "hello" to return a String "Hello, my name is [firstName] [lastName]." Commented Oct 22, 2015 at 17:42
  • Possible duplicate of How can I debug 'unrecognized selector sent to instance' error Commented Jul 8, 2019 at 5:54

3 Answers 3

1

You are getting this error as arrayByAddingObjectsFromArray method is not defined for NSString object. You are trying to use this method with a NSSting object as first name[i] returns a NSString not an NSArray.

Why r u complicating things you can achieve the same by

NSArray *firstName = @[@"Adele", @"Edsger", @"Joan", @"Clarence", @"Margaret", @"George", @"Tim", @"Jean"]; NSArray *lastName = @[@"Goldberg",@"Dijkstra",@"Clarke",@"Ellis",@"Hamilton",@"Boole",@"Berners-Lee",@"Bartik"]; NSMutableArray *array = [[NSMutableArray alloc] init]; NSString *hello; for (NSUInteger i = 0; i < [lastName count] ; i++) { NSString *fullName = [[[firstName objectAtIndex:i] uppercaseString]stringByAppendingString:[@" " stringByAppendingString:[[lastName objectAtIndex:i]uppercaseString]]]; [array addObject:fullName]; hello = [NSString stringWithFormat:@"Hello, my name is %@", fullName]; NSLog(@"%@",hello); } 

Hope it helps.. Happy Coding.. :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help lucky!
1

If I undestand correctly, you want the hello string to be

Hello, my name is Firstname Lastname

You can do this by:

NSArray *firstName = @[@"Adele", @"Edsger", @"Joan", @"Clarence", @"Margaret", @"George", @"Tim", @"Jean"]; NSArray *lastName = @[@"Goldberg",@"Dijkstra",@"Clarke",@"Ellis",@"Hamilton",@"Boole",@"Berners-Lee",@"Bartik"]; NSMutableArray *fullNameArray = [[NSMutableArray alloc] init]; NSString *hello; NSString *uppercaseString = [speaker copy]; for (NSUInteger i = 0; i < [lastName count] ; i++) { uppercaseString = [lastName[i] capitalizedString]; //join firstName and lastName in a string NSString *fullName = [[firstName objectAtIndex:i] stringByAppendingString:[NSString stringWithFormat:@" %@", uppercaseString]]; //add fullName string to fullNameArray [fullNameArray addObject:fullName]; //set it in hello string hello = [NSString stringWithFormat:@"Hello, my name is %@", fullName]; NSLog(@"%@",hello); } 

1 Comment

Thanks tnylee, this definitely closer matches my original code and logic. Appreciate the help
1

Method arrayByAddingObjectsFromArray: is used to connect two arrays. It works like this:

NSArray * first = @[@1, @2, @3]; //[1, 2, 3] NSArray * second = @[@4, @5]; //[4, 5] NSArray * bothArrays = [first arrayByAddingObjectsFromArray:second]; // [1, 2, 3, 4, 5] 

If you really wanna use this method for this task, you can wrap strings into arrays, like this:

NSArray * fullNameComponents = [@[firstName[i]] arrayByAddingObjectsFromArray:@[lastName[i]]]; 

but it is like the most unefficient whay to do that. Much better is to create array instantly:

NSArray * fullNameComponents = @[firstName[i], secondName[i]]; 

In your case the best option will be to omit all the array job, and create your string using format:

NSString * hello = [NSString stringWithFormat:@"Hello, my name is %@ %@", firstName[i], secondName[i]]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.