jspooner
jspooner

Reputation: 11315

Objective C bool property and memory

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 {
    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

Upvotes: 2

Views: 736

Answers (3)

Matt.M
Matt.M

Reputation: 1148

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

Upvotes: 2

0x90
0x90

Reputation: 6259

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".

Upvotes: 1

Simon Lee
Simon Lee

Reputation: 22334

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

Upvotes: 4

Related Questions