【问题标题】:Getting EXC_BAD_ACCESS in my viewDidLoad在我的 viewDidLoad 中获取 EXC_BAD_ACCESS
【发布时间】:2011-07-26 05:45:38
【问题描述】:

在我的 iOS 应用程序的 viewDidLoad 方法中,我有以下代码行:

loadDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"1.0", "version number", nil]; 

它会导致EXC_BAD_ACCESS 错误。我不知道为什么。 viewDidLoad 方法中没有其他内容使用该对象,除了该行所在的 if/else 语句的另一侧,因此它不可能已经被释放。我只是不确定问题是什么。如果你想看看我的整个viewDidLoad 方法,这里是:

- (void)viewDidLoad
{
    [super viewDidLoad];
    tapsBackground = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    tapsBackground.delegate = self;
    [self.view addGestureRecognizer:tapsBackground];

    saveChangesButton.hidden = YES;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    filePath = [documentsDirectory stringByAppendingPathComponent:@"presets.plist"];

    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
        loadDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    }

    else{
        loadDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"1.0", "version number", nil];
        [loadDict writeToFile:filePath atomically:YES];
    }
}

【问题讨论】:

  • 请发布完整的错误信息。另外,loadDict 声明在哪里?
  • 我得到的唯一错误信息是线程 1:程序接收到的信号:接口中声明了“EXC_BAD_ACCESS”loadDict
  • 另外,感谢您的编辑。我不太擅长使用stackoverflow的文本编辑器

标签: ios nsmutabledictionary


【解决方案1】:

问题是您在第二个字符串(即"version number")中缺少@

loadDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"1.0", "version number", nil];

应该是,

loadDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"1.0", @"version number", nil];

【讨论】:

  • 是的,我已经想通了。不过谢谢!我想我在写那个论点时在想 java =P
【解决方案2】:

我曾经遇到过同样的问题。试试这个,它对我有用:

self.loadDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"1.0", "version number", nil];

我假设您已经将 loadDict 设为属性。在这种情况下,您可以使用 Product>Profile>allocations 来查明问题。

【讨论】:

  • 谢谢。事实证明这不是问题,但我后来在我的程序中确实遇到了这个问题,并尝试修复它,所以谢谢
猜你喜欢
  • 1970-01-01
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多