Objective c 合并数组的值

Objective c 合并数组的值,objective-c,Objective C,我有一个数组,其中包含从JSON获得的所有名称 NSArray *name = [newResponseObject valueForKey:@"name"]; 我还有另一个数组,它包含我从JSON获得的所有姓氏 NSArray *lastname = [newResponseObject valueForKey:@"lastname"]; 我要做的是创建另一个包含[name,lastname]的数组,并显示我的选择器视图的值。例如“约翰·史密斯” 我曾尝试使用arrayWithArray

我有一个数组,其中包含从JSON获得的所有名称

NSArray *name = [newResponseObject valueForKey:@"name"];
我还有另一个数组,它包含我从JSON获得的所有姓氏

 NSArray *lastname = [newResponseObject valueForKey:@"lastname"];
我要做的是创建另一个包含[name,lastname]的数组,并显示我的选择器视图的值。例如“约翰·史密斯”

我曾尝试使用
arrayWithArray
,但这只是将所有值添加到一个数组中


实现这一目标的正确方法是什么

实现这一点有很多方法

可能最简单的方法是在源代码处连接

NSArray *fullName = [NSString stringWithFormat:@"%@ %@", [newResponseObject valueForKey:@"name"], [newResponseObject valueForKey:@"lastname"]];

根据您计划在以后使用它们做什么,您应该决定如何存储它们。正如@Woodstock提到的,您可以将它们存储在一个串接字符串数组“name lastname”中,类似于下面的选项1

// option 1 storing them in concatonated strings
NSArray *firstNames = @[@"John", @"Ralph", @"Bob"];
NSArray *lastNames = @[@"Smith", @"Jones", @"Miller"];
NSMutableArray <NSString *> *firstAndLastNames = [NSMutableArray array];
for (NSInteger index = 0; index < firstNames.count; index++) {
    [firstAndLastNames addObject:[NSString stringWithFormat:@"%@ %@", [firstNames objectAtIndex:index], [lastNames objectAtIndex:index]]];
}
NSLog(@"First and last names = %@", firstAndLastNames);
// option 2 storing them in arrays
NSMutableArray <NSArray *> *firstAndLastNamesInArray = [NSMutableArray array];
for (NSInteger index2 = 0; index2 < firstNames.count; index2++) {
    [firstAndLastNamesInArray addObject:@[[firstNames objectAtIndex:index2], [lastNames objectAtIndex:index2]]];
}
NSLog(@"First and last names in array = %@", firstAndLastNamesInArray);
但根据您的描述,您可能希望将它们存储为一个数组,类似于下面的选项2

// option 1 storing them in concatonated strings
NSArray *firstNames = @[@"John", @"Ralph", @"Bob"];
NSArray *lastNames = @[@"Smith", @"Jones", @"Miller"];
NSMutableArray <NSString *> *firstAndLastNames = [NSMutableArray array];
for (NSInteger index = 0; index < firstNames.count; index++) {
    [firstAndLastNames addObject:[NSString stringWithFormat:@"%@ %@", [firstNames objectAtIndex:index], [lastNames objectAtIndex:index]]];
}
NSLog(@"First and last names = %@", firstAndLastNames);
// option 2 storing them in arrays
NSMutableArray <NSArray *> *firstAndLastNamesInArray = [NSMutableArray array];
for (NSInteger index2 = 0; index2 < firstNames.count; index2++) {
    [firstAndLastNamesInArray addObject:@[[firstNames objectAtIndex:index2], [lastNames objectAtIndex:index2]]];
}
NSLog(@"First and last names in array = %@", firstAndLastNamesInArray);
备选案文2: 使用第二个选项的好处是,如果您只想获得名字或姓氏,您可以通过索引找到它(不必尝试按空格分隔名字和姓氏——有些名字或姓氏中也有空格,因此如果使用选项1,这可能会打乱您仅获取其中一个名字的逻辑)

i、 e.使用选项2,您可以通过以下操作找到第三个人的姓氏:

[[firstAndLastNamesInArray objectAtIndex:2] objectAtIndex:1]
另一个选项是创建一个Person对象,让它具有firstName和lastName属性,然后存储一个Person对象数组


还有一件事你也没有提到(但我假设是真的),那就是你从服务器上获取的这些数组的顺序是否正确——即firstName[3]是否与lastName[3]相关?你是否总是保证每个人都有一个名字和lastName?

NSMutableArray*result=[[NSMutableArray alloc]init];对于(NSDictionary*aDictionary in newReponseObject){[result addObject:[NSString stringWithFormat:@“%@%@”、aDictionary[@“name”]、aDictionary[@“lastname”]];}
?但是给出
newResponseObject
对回答你的问题很有帮助。拥有两个包含相关内容的独立数组是一个非常糟糕的主意。你能同时更改JSON响应格式吗?你应该拥有一个包含两个键的对象数组。很多人,每个人都有自己的名字和姓氏。其他人请选择R4Ns解决方案。编写一个循环,如果您确实确定两个数组的长度相同,请访问其中的两个数组。另外,
-valueForKey:
NSKeyValueCoding
的一部分,您应该使用
-objectAtIndex:
访问数组,并使用
-objectForKey:
访问字典,除非您有理由这样做唱NSCoding@Larme这基本上就是我要找的thanks@JulianF.Weinert是的,我完全理解你的意思,但不幸的是,我无法编辑json格式。这实际上取决于什么是
newResponseObject
。如果
newResponseObject
[{name]:@“name1”,@“lastname”:@“lastname1”},@{“name”:@“name2”,@“lastname”:@“lastname2”}]
这可能会产生一个奇怪的结果。
@{“name”:@“name1”,@“lastname”:@“lastname1”}
我知道作者缺少这段信息,但如果其他人来这里。。。
[[firstAndLastNamesInArray objectAtIndex:2] objectAtIndex:1]