Tuesday, December 10, 2013

There are similar to C + + constructors, destructors in OBC-C in it?

First, there is a class

@ Interface ClsDemo1: NSObject
{
NSString * str;
}
......
@ End

In Object-C is not found in seemingly similar to C + + (constructor / destructor) things
So .... NSString * str should apply where the memory, where it is released???
Sorry, but only from C + +, in turn, did not adapt.
Tried
- (ClsDemo1 *) init
{
if (self = [super init])
{
str = [[NSString alloc] init];
}
return self;
}

- (Void) release
{
[Str release];
[Super release];
}
Uh, just the feel of writing, always feel where there is something wrong.
Seeking guidance.

Reply:
 
- (Void) release
{
[Str release];
[Super release];
}


This does not require you to rewrite, you
 
- (Void) dealloc {
[Str release]; str = nil;
[Super dealloc];
}

You can write here.

Can be the equivalent of C + + constructors and destructors.
Reply:
In this case,
If your memory is allocated in the words
initClsDemo1 * A = [[ClsDemo1 alloc] init]; / / A: retainCount = 1;
ClsDemo1 * B = [A copy] ;/ / after this step A.str and B.str seemingly pointing to the same piece of memory space?
[A release] / / A: retainCount = 0, execution dealloc, str also been released, B.str becomes null pointer
?
I do not know what I think there is nothing wrong
Reply:
Will not, copy will reallocate a memory
Reply:
Do not remember. . .

I XOCODE compile time, if there is a class member variables are string, it is generally not as open space. . . .
-Dealloc {}, but also not to release out of it. It feels freed memory is released in the pond. System comes to help me out
autorelease
If you use a temporary variable or some of the member variables [nsstring alloc], then they would go out
release
There, [nsobject COPY] will open up space or retaincount +1, do not remember this
Reply:
Do not remember. . .

I XOCODE compile time, if there is a class member variables are string, it is generally not as open space. . . .
-Dealloc {}, but also not to release out of it. It feels freed memory is released in the pond. System comes to help me out
autorelease
If you use a temporary variable or some of the member variables [nsstring alloc], then they would go out
release
There, [nsobject COPY] will open up space or retaincount +1, do not remember this

No comments:

Post a Comment