NSMutableArray addObjects崩溃

时间:2014-05-15 23:40:25

标签: ios objective-c nsmutablearray

我正在向我的NSMutableArray添加数组,但它崩溃了。 这是我的代码:

 NSURL *url = [NSURL URLWithString:jsonUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if (data.length >0 && connectionError ==nil) {
        NSDictionary *myData = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];


        self.currWeather = [[NSMutableArray alloc] init];
        self.weatherIconUrl =[[NSMutableArray alloc] init];

        self.currWeather = myData[@"data"][@"current_condition"][0][@"weatherIconUrl"];
        self.weather = [[myData objectForKey:@"data"] objectForKey:@"weather"];


        self.weatherIconUrl = [self.weather valueForKeyPath:@"weatherIconUrl"];

        [self.weatherIconUrl addObjectsFromArray:self.currWeather]; //here is the problem
        NSLog(@"the url is %@",self.weatherIconUrl);
    }
}];

我试图改变" currWeather"从NSMutableNSArray,但又失败了。

这是错误:

-[__NSArrayI addObjectsFromArray:]: unrecognized selector sent to instance 0x908e3a0
2014-05-15 16:21:58.376 myRestSample[2239:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObjectsFromArray:]: unrecognized selector sent to instance 0x908e3a0'

任何人都知道为什么?

1 个答案:

答案 0 :(得分:1)

你的self.weatherIconUrl只是一个不可变的NSArray而不是NSMutableArray。

//Assuming weatherIconUrl is declared as an NSMutableArray, you could try this:
self.weatherIconUrl = [[self.weather valueForKeyPath:@"weatherIconUrl"]mutableCopy];