EXC_BAD_ACCESS 创建 NSDictionary 的 NSMutalbeArray 时

EXC_BAD_ACCESS when creating NSMutalbeArray of 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]];

}

图片名称字符串前少了一个@

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

应该是

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

您忘记将 @ 指向参数之一 "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];

试试这些..

@ 在您的 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]];

以上答案看起来都很好,但你会不会想要添加一个实际的图像而不是文件? 如果是这样,您应该添加如下内容:

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

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

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

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