Objective-C线程1:信号SIGABRT没有坏连接

时间:2014-12-27 18:01:49

标签: ios objective-c xcode

这是我的代码。

NSURL *quoteURL = [NSURL URLWithString:@"http://qwoatzz.com/quoates.json"];

NSData *jsonData = [NSData dataWithContentsOfURL:quoteURL];

NSError *error = nil;

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
// NSLog(@"dataDictionary : %@", dataDictionary);

NSDictionary *posts = [dataDictionary objectForKey:@"picture1"];

self.quoteArray = [posts objectForKey:@"quote"];
self.personArray = [posts objectForKey:@"person"];

int r = arc4random_uniform(quoteArray.count);

mainText.text = [quoteArray objectAtIndex:r];
subText.text = [personArray objectAtIndex:r];

我在以“self.quoteArray”开头的行上收到了Thread 1:signal SIGABRT错误。我在任何视图控制器的故事板中没有断开的连接。我在控制台中也得到了“libc++abi.dylib: terminating with uncaught exception of type NSException”。有什么问题?

正在运行的代码:

SURL *quoteURL = [NSURL URLWithString:@"http://qwoatzz.com/quoates.json"];


NSData *jsonData = [NSData dataWithContentsOfURL:quoteURL];

NSError *error = nil;

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
// NSLog(@"dataDictionary : %@", dataDictionary);

NSArray *posts = [dataDictionary objectForKey:@"picture3"];

int i = 0;

NSString *quote = [((NSDictionary *)(posts[i])) objectForKey:@"quote"];
NSString *person = [((NSDictionary *)(posts[i])) objectForKey:@"person"];

i++;

NSString *quote1 = [((NSDictionary *)(posts[i])) objectForKey:@"quote"];
NSString *person1 = [((NSDictionary *)(posts[i])) objectForKey:@"person"];

i++;

NSString *quote2 = [((NSDictionary *)(posts[i])) objectForKey:@"quote"];
NSString *person2 = [((NSDictionary *)(posts[i])) objectForKey:@"person"];

NSArray *quotesArray = [NSArray arrayWithObjects:quote, quote1, quote2, nil];
NSArray *personArray = [NSArray arrayWithObjects:person, person1, person2, nil];

int r = arc4random_uniform(quotesArray.count);

mainText.text = [quotesArray objectAtIndex:r];
subText.text = [personArray objectAtIndex:r];

我不再使用该代码了,因为我在JSON文件中会有一些未指定数量的对象。

编辑:当我收到显示此错误的错误时,它现在指示我到main.m:

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

,错误在“return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

2 个答案:

答案 0 :(得分:0)

代码段中的上下文不足,而且错误消息中只有部分信息,因此无法在此确定。你应该首先确认" picture1"实际上是返回一个字典,它包含数组" quote"你期望它。

比较两个代码部分,[dataDictionary objectForKey:@"picture1"];返回一个NSArray,其中包含posts工作版本中的NSDictionaries,但您将其视为仅在新代码中返回NSDictionary。这可能是问题所在。

从SIGABRT发布大部分错误消息会立即帮助缩小问题类型。

答案 1 :(得分:0)

当您转到提供的网址并查看下面的结果时,问题很明显。响应是字典。与 picture1 键关联的对象是 字典数组

{
    "picture1":
    [
        {
            "quote":"A little nonsense now and then, is cherished by the wisest men.", 
            "person":"Roald Dahl"
        }, 
        {
            "quote":"It's a good idea always to do something relaxing prior to making an important decision in your life.",
            "person":"Paulo Coelho"
        },
        {
            "quote":"Work. Don't Think. Relax.",
            "person":"Ray Bradbury"
        }   
    ]
} 

你需要获取key的对象(你可以使用快捷方式yourDict[@"theKey"]而不是objectForKey)来获取picture1 - 这是一个数组。那么该数组中的每个对象都是一个字典,所以如果你想获得所有的引用/人,这就是你要做的:

NSArray *picture1Posts = dataDictionary[@"person1"];
NSMutableArray *quotes = [NSMutableArray array];
NSMutableArray *persons = [NSMutableArray array];

for (NSDictionary *dict in picture1Posts) {
   [quotes addObject:dict[@"quote"];
   [persons addObject:dict[@"person"];
}

请注意,将nil插入数组会导致数据崩溃,因此您需要确保dict[@"quote"]dict[@"person"]在添加之前不会出现问题,这只是为了告诉你你的问题。