iPhone – Can I return the assigned object and release it outside the function?

- (NSString *)allocString{
NSString *str = [[NSString alloc] init];
return str;
}< br />
- (void)viewDidLoad{
NSString *name = [self allocString];

[name release]; // Can I release an object here?
}

Hi, I just wrote a simple example with NSString. In my real program, I used a custom UIView class instead of NSString.

allocString creates an NSString instance with a retention count of 1. There is no automatic release, it only returns an instance.

In the viewDidLoad method, the variable “name” only points to the object returned by the allocString method, so the instance is retained The count is still 1.

Therefore, [Name Release] will reduce its retention count.

Is this correct? Or do I have to release it automatically in the allocString method? Thanks!

What you do will work. You can keep and release any range of your choice Objects, as long as they are all uniform (and when any other objects no longer need the object, the object will be released).

But what you are doing is not traditional. If you want to allow The object survives the life cycle of the method (so you can return and let the method caller receive it), then you should use automatic release.

In your example, the first method may not Keep ownership of the string. It should release it and let the caller pick it up. But, of course, if you release it in the method, it will be released immediately. So you can release it automatically. By automatically releasing it, your first One way is to say “I don’t need this object anymore and give up my ownership, but please keep it in memory for a period of time so that my caller can keep it if it needs it”.

< p>This is an example:

- (NSString *)createString {
/* Create a string. This method owns it. */
NSString *str = [[NSString alloc] init];

/* Autorelease it. This method does not own it, but wants it to stay in memory temporarily. */
[str autorelease]

/* Return it. It will stay in memory till the end of the current run loop */
return str;
}

- ( void)viewDidLoad{
NSString *name = [self createString];
/* We now have a copy of the string. Nobody owns it. */
/* It is due to be released at the end of the curre nt run loop. */

/* If we want to take ownership of it and prevent deallocation, we should retain it. */
[name retain];
}< /pre>

The entire Cocoa API uses this behavior. When you see a class method that returns a new object, the object will be automatically released. For example, when you call [NSArray arrayWithObjects:@“One”,@“Two ",@"Three",nil], you ask the NSArray class to create an array for you. If it exists outside the arrayWithObjects: method due to its auto-release state. If you don’t retain it, it will be released at the end of the current running loop .

- (NSString *)allocString{
NSString *str = [[NSString alloc] init];
return str;< br />}

- (void)viewDidLoad{
NSString *name = [self allocString];

[name release]; // Can I release an object here?
}

Hi, I just wrote a simple example with NSString. In my real program, I used a custom UIView class instead of NSString.

p>

allocString creates an NSString instance with a retention count of 1. There is no automatic release, it only returns an instance.

In the viewDidLoad method, the variable "name" only points to the object returned by the allocString method , So the retention count of the instance is still 1.

Therefore, [Name Release] will reduce its retention count.

Is this correct? Or do I have to release it automatically in the allocString method? Thanks!

What you have done will work. You can keep and release the objects in any range you choose, as long as they are all uniform (and when any other object When the object is no longer needed, the object will be released).

But what you do is not traditional. If you want to allow the object to survive the life cycle of the method (so you You can return and let the method caller receive it), then you should use autorelease.

In your example, the first method may not retain ownership of the string. It should release it and let The caller answers it. But, of course, if you release it in the method, it will be released immediately. So you can automatically release it. By automatically releasing it, your first method is to say "I don't need this object anymore And give up my ownership, but please keep it in memory for a period of time so that my caller can keep it if it needs it".

Here is an example:

< p>

- (NSString *)createString {
/* Create a string. This method owns it. */
NSString *str = [[NSString alloc] init];< br />
/* Autorelease it. This method does not own it, but wants it to stay in memory temporarily. */
[str autorelease]

/* Return it . It will stay in memory till the end of the current run loop */
return str;
}

- (void)viewDidLoad{
NSString *name = [self createString];
/* We now have a copy of the string. Nobody owns it. */
/* It is due to be released at the end of the current run loop. */< br />
/* If we want to take own ership of it and prevent deallocation, we should retain it. */
[name retain];
}

The entire Cocoa API uses this behavior. When you see returning new objects The object will be automatically released. For example, when you call [NSArray arrayWithObjects:@"One",@"Two",@"Three",nil], you ask the NSArray class to create an array for you. If it exists outside the arrayWithObjects: method due to its auto-release state. If it is not retained, it will be released at the end of the current running loop.

Leave a Comment

Your email address will not be published.