Iphone 不确定如何修复此EXC\u BAD\u访问错误
因此,我有一个任务实体类(核心数据),我试图覆盖setter的一个字符串(timeIntervalString),这样它就可以显示在表视图单元格的详细文本标签中。出于某种原因,我得到了一个EXC_BAD_访问错误,如下所示: [Tasks timeIntervalString]一直持续到37355 这是我的密码:Iphone 不确定如何修复此EXC\u BAD\u访问错误,iphone,ios,objective-c,nsstring,exc-bad-access,Iphone,Ios,Objective C,Nsstring,Exc Bad Access,因此,我有一个任务实体类(核心数据),我试图覆盖setter的一个字符串(timeIntervalString),这样它就可以显示在表视图单元格的详细文本标签中。出于某种原因,我得到了一个EXC_BAD_访问错误,如下所示: [Tasks timeIntervalString]一直持续到37355 这是我的密码: -(NSString *)timeIntervalString{ NSUInteger seconds = (NSUInteger)round(self.timeInter
-(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;
}
有什么想法吗?
返回self。timeIntervalString
将递归调用相同的timeIntervalString
方法
您可能需要的是返回\u timeIntervalString
说明:self.timeIntervalString
是属性访问器,它与[self-timeIntervalString]
相同,它将调用您在此处定义的-(NSString*)timeIntervalString
方法。return\u timeIntervalString
更改将使您直接访问实例变量,而不是递归调用属性访问器。这是在编写任何自定义属性访问器方法时都应该遵循的常规模式
编辑:根据评论中的讨论,最好将其标记为只读属性,并且从不实际设置值:
在.h文件中:
@property (readonly) NSString *timeIntervalString;
在.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;
}
返回self.timeIntervalString
将递归调用相同的timeIntervalString
方法
您可能需要的是返回\u timeIntervalString
说明:self.timeIntervalString
是属性访问器,它与[self-timeIntervalString]
相同,它将调用您在此处定义的-(NSString*)timeIntervalString
方法。return\u timeIntervalString
更改将使您直接访问实例变量,而不是递归调用属性访问器。这是在编写任何自定义属性访问器方法时都应该遵循的常规模式
编辑:根据评论中的讨论,最好将其标记为只读属性,并且从不实际设置值:
在.h文件中:
@property (readonly) NSString *timeIntervalString;
在.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;
}
半无关设计问题:在这个方法定义之外,您是否设置过timeIntervalString的值?考虑是否需要设置timeIntervalString的值,而不仅仅是返回它并将其标记为只读属性。不,我实际上没有设置它,我只显示它。但是我确实按照Esker所说的设置了timeIntervalString,可能没有必要将这个值存储在
\u timeIntervalString
中。我将您的属性声明为只读和动态,跳过赋值,只返回格式化的值。这将从一个不必要的实例变量中节省一些内存。半无关设计问题:在这个方法定义之外,您是否设置过timeIntervalString的值?考虑是否需要设置timeIntervalString的值,而不仅仅是返回它并将其标记为只读属性。不,我实际上没有设置它,我只显示它。但是我确实按照Esker所说的设置了timeIntervalString,可能没有必要将这个值存储在\u timeIntervalString
中。我将您的属性声明为只读和动态,跳过赋值,只返回格式化的值。这将从一个不必要的实例变量中节省一些内存。这很有意义!然而,xcode给了我“使用未声明的标识符\u timeIntervalString”。它未声明,因为它的核心数据和设置为@dynamic timeIntervalString。想法?您可能需要一个@synthesis-timeIntervalString=\u-timeIntervalString
,就在.m文件的@implementation行之后。但是,请参阅我编辑的答案以获得改进的解决方案。在最新版本的xcode中,您甚至不需要包含@synthesis,因为它是自动完成的。对,在我的答案中,替代实现不需要@synthesis
,而是使用本地变量。我的@synthesis
建议是在考虑将ivar设置为只读属性之前,它仍在初始答案中设置ivar。只是对NSManagedObject子类的属性进行了一个提示-编译器不会自动合成该属性。您需要显式地@synthesis NSManagedObject子类的属性。这是因为创建ivar通常不是程序员想要的。这可能就是为什么使用未声明的标识符\u timeIntervalString
才有意义!然而,xcode给了我“使用未声明的标识符\u timeIntervalString”。它未声明,因为它的核心数据和设置为@dynamic timeIntervalString。想法?您可能需要一个@synthesis-timeIntervalString=\u-timeIntervalString
,就在.m文件的@implementation行之后。但是,请参阅我编辑的答案以获得改进的解决方案。在最新版本的xcode中,您甚至不需要包含@synthesis,因为它是自动完成的。对,在我的答案中,替代实现不需要@synthesis
,而是使用本地变量。我的@synthesis
建议是在考虑将ivar设置为只读属性之前,它仍在初始答案中设置ivar。只是对NSManagedObject子类的属性进行了一个提示-编译器不会自动合成该属性。您需要显式地@synthesis NSManagedObject子类的属性。这是因为创建ivar通常不是程序员想要的。这可能就是为什么您使用了未声明的标识符\u timeIntervalString