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;
}
Ty Lertwichaiworawit
  • 2,950
  • 2
  • 23
  • 42
Diego Aguirre
  • 151
  • 1
  • 1
  • 10

3 Answers3

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

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
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);
}
Ty Lertwichaiworawit
  • 2,950
  • 2
  • 23
  • 42
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]]; 
Anton Onizhuk
  • 96
  • 1
  • 5