无法识别的选择器发送到实例

时间:2015-10-22 17:04:16

标签: ios objective-c

我遇到了这种方法的问题,更具体地说,我的逻辑有问题。我尝试将两个数组firstNamelastName组合在一起,然后顺序匹配。我想我可以使用for循环迭代数组计数,然后使用arrayByAddingObjectsFromArray组合数组。

不幸的是,这部分似乎引发了一个错误: __NSCFConstantString arrayByAddingObjectsFromArray:]: unrecognized selector sent to instance 0x10c824218

为什么会这样?选择器是什么意思;是不是我不想尝试传递给它的阵列?

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

3 个答案:

答案 0 :(得分:1)

您收到此错误的原因是没有为NSString对象定义arrayByAddingObjectsFromArray方法。 您正在尝试将此方法与NSSting对象一起使用作为名字[i]返回NSString而不是NSArray。

为什么你可以通过

来实现同样的目标
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);
    }

希望它有所帮助..快乐编码.. :)

答案 1 :(得分:1)

如果我没有正确查看,你希望hello字符串是

  

您好,我的名字是名字姓氏

您可以通过以下方式执行此操作:

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

答案 2 :(得分:1)

方法arrayByAddingObjectsFromArray:用于连接两个数组。它的工作原理如下:

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

如果您真的想将此方法用于此任务,则可以将字符串包装到数组中,如下所示:

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

但这就是效率最低的事情。更好的是立即创建数组:

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

在您的情况下,最好的选择是省略所有数组作业,并使用以下格式创建字符串:

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