iphone - NSMutableDictionary上的EXC_BAD_ACCESS

标签 iphone ios objective-c exc-bad-access nsmutabledictionary

我从iOS开发开始,我有以下代码:

首先,我声明listOfItems NSMutableArray:

@interface SAMasterViewController () {
    NSMutableArray *listOfItems;
}
@end

现在,这是给我“EXC_BAD_ACCESS(代码= 1,地址= 0x5fc260000)”错误的代码。
该错误在“individual_data”对象的最后一行给出。
listOfItems = [[NSMutableArray alloc] init];
for(NSDictionary *tweetDict in statuses) {
    NSString  *text          = [tweetDict objectForKey:@"text"];
    NSString  *screenName    = [[tweetDict objectForKey:@"user"] objectForKey:@"screen_name"];
    NSString  *img_url       = [[tweetDict objectForKey:@"user"] objectForKey:@"profile_image_url"];
    NSInteger unique_id      = [[tweetDict objectForKey:@"id"] intValue];
    NSInteger user_id        = [[[tweetDict objectForKey:@"user"] objectForKey:@"id"] intValue ];

    NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                            text, @"text_tweet",
                                            screenName,@"user_name",
                                            img_url, @"img_url",
                                            unique_id, @"unique_id",
                                            user_id, @"user_id", nil];
    [listOfItems addObject:individual_data];
}

提前致谢。

最佳答案

您不能将NSInteger或任何其他非Objective-C类放入数组或字典中。您需要将它们包装在 NSNumber 中。

NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                            text, @"text_tweet",
                                            screenName,@"user_name",
                                            img_url, @"img_url",
                                            [NSNumber numberWithInteger:unique_id], @"unique_id",
                                            [NSNumber numberWithInteger:user_id], @"user_id", nil];
//Or if you want to use literals
NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                            text, @"text_tweet",
                                            screenName,@"user_name",
                                            img_url, @"img_url",
                                            @(unique_id), @"unique_id",
                                            @(user_id), @"user_id", nil];

关于iphone - NSMutableDictionary上的EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14663446/

相关文章:

ios - tableview 在使用 xib 单元格时发现 nil

iphone - CATextLayer 在 iOS7 中的视频中不可见

iphone - shouldAutorotateToInterfaceOrientation 有问题

ios - 如何创建谓词来获取同一属性的所有不重复结果的实体?

ios - UITableView 异步 UIImage 设置

ios - Objective-C 类(最佳实践)

iphone - 为什么这会导致内存泄漏

android - StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

ios - iOS 支持的 SVG 特性

ios - iOS 中的类别与实用程序类