in this reference page..
http://developer.apple.com/library/...ols/NSObject_Protocol/Reference/NSObject.html
it says
"You must complete the object initialization (using an init method) before invoking release."
but I tried code below, I got no errors (with -Wall compile option)
and I tried another...
I thought "alloc" is just reserve memory and "init" will do something like wipe out that memory with zeros.
But result above shows that alloc zero out the memory.
What kind of work is done by NSObject's init method?
Thanks in advance~
system info:
i686-apple-darwin10-gcc-4.2.1
mac osx 10.6.7
http://developer.apple.com/library/...ols/NSObject_Protocol/Reference/NSObject.html
it says
"You must complete the object initialization (using an init method) before invoking release."
but I tried code below, I got no errors (with -Wall compile option)
Code:
#import <Foundation/NSObject.h>
#import <stdio.h>
@interface MyObject : NSObject {
int var;
}
-(void) set: (int) arg;
-(int) get;
@end
@implementation MyObject
-(void) set: (int) arg {
var = arg;
}
-(int) get {
return var;
}
@end
int main(int argc, const char *argv[]) {
id anObject = [MyObject alloc];
[anObject release];
return 0;
}
and I tried another...
Code:
#import <Foundation/NSObject.h>
#import <stdio.h>
@interface MyObject : NSObject {
int var;
}
-(void) set: (int) arg;
-(int) get;
@end
@implementation MyObject
-(void) set: (int) arg {
var = arg;
}
-(int) get {
return var;
}
@end
int main(int argc, const char *argv[]) {
id anObject = [MyObject alloc];
printf("addr: %p\n", anObject);
[anObject set: 5];
[anObject release];
anObject = [MyObject alloc];
printf("addr: %p\n", anObject);
printf("var: %d\n", [anObject get]);
return 0;
}
result:
addr: 0x100108ec0
addr: 0x100108ec0
var: 0
I thought "alloc" is just reserve memory and "init" will do something like wipe out that memory with zeros.
But result above shows that alloc zero out the memory.
What kind of work is done by NSObject's init method?
Thanks in advance~
system info:
i686-apple-darwin10-gcc-4.2.1
mac osx 10.6.7