Objective C布尔属性和内存

问题描述:

如何向我的位置模型添加公共布尔属性?例如:location.has_lights = YES;Objective C布尔属性和内存

我不明白为什么我需要保留NSString,但IDE试图保留bool时显示错误。

此代码产生一个 '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 dealloc of boolean value

一个bool不是对象类型,这是一个标量,所以你不要保留/释放它。

NSString是一个对象。它存储在堆上。

布尔值不是一个对象,而是通常存储在堆栈上的标量数据类型。 你不需要保留它。

保留在objectiveC中告诉运行时“指针指向的对象仍然需要,不要删除它”。