由于未捕获的异常'NSInvalidArgumentException'访问web api而获得终止应用程序

时间:2013-10-01 02:02:37

标签: ios objective-c json ios7 jsonp

我正在尝试使用一个简单的web api,它使用AFJSONRequestOperation在Objective C中返回以下数据。

Results; (
    {
    Category = Groceries;
    Id = 1;
    Name = "Tomato Soup";
    Price = 1;
},
    {
    Category = Toys;
    Id = 2;
    Name = "Yo-yo";
    Price = "3.75";
},
    {
    Category = Hardware;
    Id = 3;
    Name = Hammer;
    Price = "16.99";
}
)

我的Objective-C调用如下所示:

//not the real URL, just put in to show the variable being set
NSURL *url = [NSURL URLWithString:@"http://someapi"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];    
AFJSONRequestOperation *operation;    
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request

                                                            success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                NSLog(@"Results; %@", JSON);
                self.resultsArray= [JSON objectForKey:@"Results"];
             }

                                                            failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error ,id JSON) {
                 //http status code
                 NSLog(@"Received Error: %d", response.statusCode);
                 NSLog(@"Error is: %@", error);
             }
             ];

//run service
[operation start];

当我运行我的代码时,我可以看到NSLog语句中返回的数据。但是,当我尝试使用JSON objectForKey语句将结果设置为我的数组时,我收到以下错误。

-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xe466510
2013-09-30 20:49:03.893 ITPMessageViewer[97459:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xe466510'

我对Objective-C很新,并且无法弄清楚为什么这不起作用。任何帮助或想法将不胜感激。

3 个答案:

答案 0 :(得分:4)

您获得的结果是数组

objectForKey:是一个NSDictionary方法

所以使用valueForKey:这是一种NSArray方法。

self.resultsArray= [JSON valueForKey:@"Results"];

答案 1 :(得分:0)

在您的代码中,JSON(结果)需要遵循以下方法:

    self.resultsArray = (NSArray *)JSON;

它会强制将“id”类型强制为NSArray为你的self.resultsArray类型(它是一个NSArray,对吗?),然后你可以使用这个方法来枚举它。

    [self.resultsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) 
    {
        NSDictionary *_eachResult = (NSDictionary *)obj;
        [_eachResult objectForKey:@"Category"];
        //... 
    }];

希望它可以帮到你。

答案 2 :(得分:0)

我设法更新我的网络服务,以AFJSONRequestOperation满意的格式返回数据。这使我能够正确地解析JSON。仍然不确定为什么它需要这个字典组合,但很高兴它有效。

如果有人在C#中写一个web api与目标C交谈,那就是我做的:

更新是将以下对象作为JSON返回(代码在C#中)。

字典> with string =“results”,IEnumerable是我强类型的对象数组。

public Dictionary<string, IEnumerable<Product>> GetAllProducts()
        {
            var sortedProuct = results.OrderBy(a => a.Category);

            var proddict = new Dictionary<string, IEnumerable<Product>>()
            {
                { "results", results}, 
            };

            return proddict;
        }

这转化为:

Results; {
results = ({
Category = Groceries;
Id = 1;
Name = "Tomato Soup";
Price = 1;},

...