Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 13, 2007, 03:17 PM   #1
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Key-Value Coding Question

Sorry if this is a newbish question, but I have so far got used to make programs only by using very standard features that concern key-value coding. I have this simple piece of code:

Code:
#import <Foundation/Foundation.h>

@interface test:NSObject{
	NSMutableString *simpleValue;
	NSMutableArray *simpleArray;
}


@end


@implementation test:NSObject
- (id) init {
	self = [super init];
	if (self != nil) {
		simpleValue = [NSMutableString stringWithString:@"hello world!"];
		simpleArray = [NSMutableArray arrayWithObjects:@"hello 1", @"hello 2", nil];
	}
	return self;
}

- (void) dealloc {
	[simpleValue release];
	[simpleArray release];
	[super dealloc];
}

@end

int main (int argc, char *argv[])
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	test *t = [[test alloc]init];
	[t takeValue:@"hello again!" forKey:@"simpleValue"];
	NSLog(@"%@",[t valueForKey:@"simpleValue"]);
	
	
	[pool release];
	return 0;
}
When ran, the debugger gives me a runtime error at the "[pool release]" line. Why is that? What is key-value coding in general, and how can I make my classes use it?
Soulstorm is offline   0 Reply With Quote
Old Feb 13, 2007, 03:24 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
What's the error?

Apple have a great guide to Key Value coding.

Basically it's a nice way to provide generic access to an objects properties in a predictable and controllable method. Bindings is built directly on top of this...
robbieduncan is offline   0 Reply With Quote
Old Feb 13, 2007, 03:32 PM   #3
Soulstorm
Thread Starter
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by robbieduncan View Post
What's the error?

Apple have a great guide to Key Value coding.

Basically it's a nice way to provide generic access to an objects properties in a predictable and controllable method. Bindings is built directly on top of this...
Actually, the debugger comes up, and it doesn't tell me what the error is. I guess you must compile and run it to see for yourself.

the following code gives me errors again...
Code:
#import <Foundation/Foundation.h>

@interface test: NSObject
{
	NSString *simpleValue;
	NSArray *simpleArray;
}

-(void)setSimpleValue:(NSString *)newValue;
-(void)setSimpleArray:(NSArray *)newArray;

-(NSArray *)getSimpleArray;
-(NSString *)getSimpleValue;

@end


@implementation test
- (id) init {
	self = [super init];
	if (self != nil) {
		simpleValue = [NSMutableString stringWithString:@"hello world!"];
		simpleArray = [NSMutableArray arrayWithObjects:@"hello 1", @"hello 2", nil];
	}
	return self;
}


-(void)setSimpleValue:(NSString *)newValue
{
	[simpleValue release];
	simpleValue = [newValue retain];
}

-(void)setSimpleArray:(NSArray *)newArray
{
	[simpleArray release];
	simpleArray = [newArray retain];
}


-(NSArray *)getSimpleArray
{
	return simpleArray;
}

-(NSString *)getSimpleValue
{
	return simpleValue;
}

- (void) dealloc {
	[simpleValue release];
	[simpleArray release];
	[super dealloc];
}

@end

int main (int argc, char *argv[])
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	
	test *testClass = [[test alloc]init];
	
	[testClass setValue:@"hello world!" forKey:@"simpleValue"];
	NSLog([testClass getSimpleValue]);
	
	
	[pool release];
	return 0;
}
...but I don't know why
Soulstorm is offline   0 Reply With Quote
Old Feb 13, 2007, 03:43 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Bad memory management...

When you create an instance of the test class you create two objects that are autoreleased (simpleValue and simpleArray). When you then call setSimpleValue this autoreleased object is released again. You should never release an object you have not retained...

So when your pool gets released it tries to release all the objects in it. But you've released it and it crashes.

If you retain the two objects it doesn't crash

A better solution to init is to do something like:

Code:
- (id) init {
	self = [super init];
	if (self != nil) {
		[self setSimpleValue:@"hello world!"];
	}
	return self;
}
This will cause any bindings to get triggered: your way does not...
robbieduncan is offline   0 Reply With Quote
Old Feb 13, 2007, 05:53 PM   #5
Soulstorm
Thread Starter
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
That corrected the problem I was having. Thanks a lot robbieduncan!
Soulstorm is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How to map ALT key as Cmd key in OSX? Abazigal Mac Basics and Help 2 Jul 23, 2013 07:09 AM
Help, my Control key maps to my Command key pacman1176 Mac OS X 10.7 Lion 2 Jan 22, 2013 04:37 PM
Key lime question dukebound85 Community Discussion 7 Jan 6, 2013 01:19 PM
Question about Portal 2 CD Key Issue w/ Steam spaceballl Mac and PC Games 9 Aug 9, 2012 12:42 AM
Make the enter key act like the return key qtrim Mac Basics and Help 6 Jul 28, 2012 03:37 PM

Forum Jump

All times are GMT -5. The time now is 03:54 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC