0

当我尝试合并 2 时遇到问题NSString

NSSring我从 JSON 中提取 2 ,它的图表是:

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:]: unrecognized selector sent to instance.

谁能帮我?

4

2 回答 2

1

所有 “发送到实例的无法识别的选择器”。错误的意思是一样的:你认为某个对象有一个方法,但它在运行时真的没有。

由于 Objective-C 的动态特性,如果你不确定某个对象有一个方法,你应该总是调用 respondsToSelector: 来测试它,像这样:

if ([myObj respondsToSelector:@selector(someMethod)]) {
    [myObj someMethod];
} 

在这种情况下,

NSString *fullnameList = [usersList valueForKey:@"fullname"];

是一个 NSString。该类没有 insertObjects:atIndexes: 方法。也许您必须将其声明为 NSMutableArray

于 2012-05-04T07:49:14.343 回答
1

你应该使用

 NSMutableDictionary* dataDict =   [NSMutableDictionary dictionaryWithObjects:picturesList   forKeys:namesList];
 // Whenever key needed for fetching record from Dictionary just write       
 NSArray* keyArr = [dataDict AllKey];

现在您拥有所有密钥,您可以借助上述密钥获取记录。

于 2012-05-04T08:00:37.867 回答