问题描述
|
如何将公共布尔属性添加到位置模型?例如:
location.has_lights = YES;
我不太明白为什么我需要保留Nsstring,但是IDE尝试保留布尔值时却显示错误。
这段代码产生一个\'EXC_BAD_ACCESS \'
RKLocation.h
#import <RestKit/RestKit.h>
@interface RKLocation : RKObject {
Nsstring *_name;
bool has_lights;
}
@property (nonatomic,retain) Nsstring *name;
@property (nonatomic) bool has_lights;
@end
RKLocation.m
#import \"RKLocation.h\"
@implementation RKLocation
@synthesize name = _name;
@synthesize has_lights;
- (void)dealloc {
[_name release];
[super dealloc];
}
@end
解决方法
尝试使用BOOL代替bool。
几分钟前提出的这个问题也可能有帮助:
Objective-C布尔值的解除分配
, 布尔不是对象类型,而是标量,因此您不能保留/释放它。
, NSString是一个对象。它存储在堆中。
布尔值不是对象,而是通常存储在堆栈中的标量数据类型。
您不需要保留它。
保留在objectiveC中告诉运行时“仍然需要指针指向的对象,请不要删除它”。