开发者

Objective C bool property and memory

开发者 https://www.devze.com 2023-03-13 08:44 出处:网络
How do I add a public boolean property to my Location model?Example: location.has_lights = YES; I don\'t quite understand why I need to retain NSString but the IDE shows me an error when trying to re

How do I add a public boolean property to my Location model? Example: location.has_lights = YES;

I don't quite understand why I need to retain NSString but the IDE shows me an error when trying to retain a bool.

This code produces a 'EXC_BAD_ACCESS'

RKLocation.h

#import <RestKit/RestKit.h>
@interface RKLocation : RKObject 开发者_JAVA百科{
    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


A bool is not an object type, it is a scalar, so you don't retain / release it.


Try using BOOL instead of bool.

Also this question that was asked just a few minutes ago might be of help: Objective-c dealloc of boolean value


An NSString is an object. It's stored on the Heap.

A Boolean is not an object but a scalar data type customarily stored on the Stack. You don't need to retain it.

Retaining in objectiveC tells the runtime "the object that pointer points to is still needed, don't remove it yet".

0

精彩评论

暂无评论...
验证码 换一张
取 消