I've found an autorelease pool inside a method passing a pointer as argument.
1. Is there any relation between the 2 ?
- (void)exportXMLToString:(NSMutableString **)aString
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableString *theString = *aString;
2. More in general, why do I want to pass the pointer to a string can't I just pass the string itself and returning it back ?
3. If I want to override such method, and invoking the superclass method from it, can I just write this ? (and the same string is used ?
- (void)exportXMLToString:(NSMutableString **)aString
{
NSAutoreleasePool *pool = [[NSAutorel开发者_Python百科easePool alloc] init];
NSMutableString *theString = *aString;
...
[super exportXMLToString:aString];
}
thanks
Passing the string like this makes it so that any changes to the string that are made to the string inside the method will persist outside the method. It takes XML and stores it in the string you provided.
The autorelease pool just handles all the autorelease statements that take place before that pool is drained. If you are executing a bunch of code that could generate a great deal of autoreleased objects, you can wrap that code in its own autorelease pool instead of waiting on for the next time the default one drains. It is unrelated to how the string is passed into the method.
If you plan to invoke the superclass method from your overridden method, you don't need to add another autorelease pool unless you are doing a bunch of autoreleasing of your own outside the superclass method. It will still create and handle its own autorelease pool as it did before.
Note that this is completely wrong:
- (void)exportXMLToString:(NSMutableString **)aString
There is never a reason to have a method that returns void
and takes a **
argument. Instead, it should just return the value directly.
Pass by reference is used very rarely in Objective-C, typically only for NSError**
parameters, though there are a handful of others.
精彩评论