创建NSDictionary的NSMutalbeArray时的EXC_BAD_ACCESS

时间:2015-05-28 10:42:42

标签: ios objective-c nsmutablearray nsdictionary

我正在尝试填充一个字典数组。但是xcode说EXC_BAD_ACCESS

这是一项基本操作。错误在哪里?

@interface MenuViewController ()
@property (nonatomic, strong) NSMutableArray *items;
@end

@implementation MenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.items = [[NSMutableArray alloc]initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuHomeIcon.png",@"image", nil],
                                                        [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuFindIcon.png",@"image", nil],
                                                        [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuProfileIcon.png",@"image", nil],
                                                        [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuSettingsIcon.png",@"image", nil],nil];

}

我尝试了另一种方法,但结果是一样的:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.items = [[NSMutableArray alloc]initWithCapacity:4];

    [self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuHomeIcon.png",@"image", nil]];
    [self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuFindIcon.png",@"image", nil]];
    [self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuProfileIcon.png",@"image", nil]];
    [self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuSettingsIcon.png",@"image", nil]];

}

5 个答案:

答案 0 :(得分:7)

在图像名称字符串

之前缺少@
[self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title","MenuHomeIcon.png",@"image", nil]];

应该是

[self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",@"MenuHomeIcon.png",@"image", nil]];

答案 1 :(得分:1)

你忘记了@参数之一" MenuHomeIcon.png"

self.items = [[NSMutableArray alloc]initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",@"MenuHomeIcon.png",@"image", nil],
                                                    [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",@"MenuFindIcon.png",@"image", nil],
                                                    [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",@"MenuProfileIcon.png",@"image", nil],
                                                    [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",@"MenuSettingsIcon.png",@"image", nil],nil];

答案 2 :(得分:1)

尝试这些..

在self.items中的图像名称字符串之前缺少@ ..

做这些..

self.items = [[NSMutableArray alloc]initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",@"MenuHomeIcon.png",@"image", nil],
                                                    [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",@"MenuFindIcon.png",@"image", nil],
                                                    [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",@"MenuProfileIcon.png",@"image", nil],
                                                    [NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",@"MenuSettingsIcon.png",@"image", nil],nil];

和addObject ..

[self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",@"MenuHomeIcon.png",@"image", nil]];

答案 3 :(得分:0)

我想你忘记添加'@' 所以只需添加此

self.items = [[NSMutableArray alloc] init];

[self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",@"MenuHomeIcon.png",@"image", nil]];

答案 4 :(得分:-1)

以上所有答案看起来都不错,但可能是您要添加实际图像而没有文件? 如果是这样,你应该添加这样的东西:

[self.items addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"home",@"title",[UIImage imageNamed:@"MenuHomeIcon.png"],@"image", nil]];