不确定如何修复此EXC_BAD_ACCESS错误

Evi*_*gis 0 iphone exc-bad-access objective-c nsstring ios

所以我有一个Task实体类(Core Data),我试图覆盖其中一个字符串(timeIntervalString)的setter,因此它可以显示在表视图单元格的详细文本标签中.出于某种原因,我得到一个像这样的EXC_BAD_ACCESS错误:

在此输入图像描述

[Tasks timeIntervalString]一直持续到37355 ...

这是我的代码:

-(NSString *)timeIntervalString{

    NSUInteger seconds = (NSUInteger)round(self.timeInterval);
if ((seconds/3600) == 0){
    if (((seconds/60) % 60) == 1) {
        self.timeIntervalString = [NSString stringWithFormat:@"%u MIN", ((seconds/60) % 60)];
    } else {
        self.timeIntervalString = [NSString stringWithFormat:@"%u MINS", ((seconds/60) % 60)];
    }
} else if ([self.conversionInfo hour] == 1) {
    if (((seconds/60) % 60) == 0){
        self.timeIntervalString = [NSString stringWithFormat:@"%u HR", (seconds/3600)];
    } else if (((seconds/60) % 60) == 1) {
        self.timeIntervalString = [NSString stringWithFormat:@"%u HR %u MIN", (seconds/3600), ((seconds/60) % 60)];
    } else {
        self.timeIntervalString = [NSString stringWithFormat:@"%u HR %u MINS", (seconds/3600), ((seconds/60) % 60)];
    }
} else {
    if (((seconds/60) % 60) == 0) {
        self.timeIntervalString = [NSString stringWithFormat:@"%u HRS ", (seconds/3600)];
    } else if (((seconds/60) % 60) == 1){
        self.timeIntervalString = [NSString stringWithFormat:@"%u HRS %u MIN", (seconds/3600), ((seconds/60) % 60)];
    } else {
        self.timeIntervalString = [NSString stringWithFormat:@"%u HRS %u MINS", (seconds/3600), ((seconds/60) % 60)];
    }
}
return self.timeIntervalString;
Run Code Online (Sandbox Code Playgroud)

}

有任何想法吗?

Mik*_*ock 5

return self.timeIntervalString将只递归调用相同的timeIntervalString方法.

你可能想要的是什么return _timeIntervalString.

说明:self.timeIntervalString是属性访问器的合成糖,它与[self timeIntervalString]调用-(NSString *)timeIntervalString你在这里定义的方法是一样的.该return _timeIntervalString变化将使它所以你直接访问实例变量,而不是递归调用属性访问器.这是您在编写的任何自定义属性访问器方法中应遵循的一般模式.

编辑:基于评论中的讨论,将此标记为只读属性并且从不实际设置值会更好:

在你的.h文件中:

@property (readonly) NSString *timeIntervalString;
Run Code Online (Sandbox Code Playgroud)

在.m文件中:

-(NSString *)timeIntervalString {
    NSString *value;
    // insert here the body of your timeIntervalString method, as you
    // originally wrote it, but replace all occurences of:
    // self.timeIntervalString = ...
    // with this: value = ...
    return value;
}
Run Code Online (Sandbox Code Playgroud)