当我尝试合并2 NSString
我从JSON中提取2 NSSring
,其图表是:
thumbList :(“picture1.jpg”,“picture2.jpg”,“picture3.jpg”......)
fullnameList :(“name1”,“name2”,“name3”......)
我的意图是使用以下方案将它们合并为一个: (“name1”,“picture1.jpg”,“name2”,“picture2.jpg”,“name3”,“picture3.jpg”......)
NSArray *array_webdata=[[NSArray array] init];
NSString *searchStatus = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
array_webdata = [parsedata objectWithString:searchStatus error:nil];
//String with all data of each user
NSString *usersList = [array_webdata valueForKey:@"results"];
NSLog(@"\n results? = %@ \n", usersList);
//String with thumbs
NSString *thumbList = [usersList valueForKey:@"thumb"];
NSLog(@"\n thumbs? = %@ \n", thumbList);
//String with usernames
NSString *fullnameList = [usersList valueForKey:@"fullname"];
NSLog(@"\n fullnames? = %@ \n", fullnameList);
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:1];
[indexes addIndex:3];
[fullnameList insertObjects:thumbList atIndexes:indexes];
NSLog(@"array: %@", fullnameList);
但是当我尝试执行时显示下一条错误消息: [__ NSArrayI insertObjects:atIndexes:]:无法识别的选择器发送到实例。
任何人都可以帮助我吗?
答案 0 :(得分:1)
所有“无法识别的选择器发送到实例。”错误意味着相同:您认为某个对象有一个方法,但它确实没有在运行时。
由于Objective-C的动态特性,如果你不确定某个对象有一个方法,你应该总是测试它调用respondsToSelector:像这样:
if ([myObj respondsToSelector:@selector(someMethod)]) {
[myObj someMethod];
}
在这种情况下,
NSString *fullnameList = [usersList valueForKey:@"fullname"];
是一个NSString。该类没有insertObjects:atIndexes:方法。也许您必须将其声明为NSMutableArray
答案 1 :(得分:1)
你应该使用
NSMutableDictionary* dataDict = [NSMutableDictionary dictionaryWithObjects:picturesList forKeys:namesList];
// Whenever key needed for fetching record from Dictionary just write
NSArray* keyArr = [dataDict AllKey];
现在你拥有所有密钥,你可以在上面的密钥的帮助下获取记录。