At 6:50 -0600 26/2/09, James Cicenia wrote:
In .h :
NSMutableArray *currentStates;
@property (nonatomic, copy) NSMutableArray *currentStates;
Now in my appdelegate I have:
@synthesize currentStates;
then in a method:
while (sqlite3_step(statement) == SQLITE_ROW) {
char *str = (char *)sqlite3_column_text(statement, 0);
[self.currentStates addObject:(str) ? [NSString
stringWithUTF8String:str] : @""];
}
However currentStates never gets populated and always has zero objects.
What am I doing wrong. And, yes str has data.
Where is currentStates actually allocated?
Initially all ivars are set to nil (or 0 or whatever). So unless
you've set it somewhere, self.currentStates will return nil, and in
Objective C, you can send a message to nil and it is simply a NoOp
(ie, you wont get any error or exception).
currentStates needs to be allocated.
The next issue (and this is almost certainly where your problem is)
is that your @property is set to copy, so:
[self.currentStates addObject:@"hello"];
is equivalent:
[[self currentStates] addObject:@"hello"];
And [self currentStates] returns a copy of the NSMutableArray. And
so you create a copy, then you add the string, then it is
autoreleased.
So the issue is that the property specified "copy", which I don't
think actually makes sense for a NSMutableArray (besides
[NSMutableArray copy] actually returns an immutable NSArray (is
there any way to specify mutableCopy in an @property?)).
So you probably need to change the @property from copy to retain.
Alternatively, use the property without the setter/getter reference, ie:
[currentStates addObject:@"hello"];
I hope that clears up more than it confuses,
Peter.
@interface TestObject : NSObject {
NSMutableArray *currentStates;
}
@property (nonatomic, retain) NSMutableArray *currentStates;
@end
@implementation TestObject
@synthesize currentStates;
- (id) init
{
self = [super init];
if (self != nil) {
self.currentStates = [NSMutableArray array];
}
return self;
}
- (void) Test;
{
[self.currentStates addObject:[NSString stringWithUTF8String:"Hello"]];
}
- (void) dealloc
{
[currentStates release];
[super dealloc];
}
@end
int main(int argc, char *argv[])
{
NSLog( @"main" );
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
TestObject* obj = [[TestObject alloc] init];
[obj Test];
NSLog( @"%@", obj.currentStates );
[pool release];
// return NSApplicationMain(argc, (const char **) argv);
}
--
Run macros from your iPhone with Keyboard Maestro Control!
or take a break with Aragom Space War for your iPhone
Keyboard Maestro <http://www.keyboardmaestro.com/> Macros for your Mac
Aragom Space War <http://www.stairways.com/iphone/aragom> Don't get killed!
<http://www.stairways.com/> <http://download.stairways.com/>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden