ARC forbids autorelease?

为君一笑 提交于 2019-11-28 12:25:40

问题


New to ios and trying to return an NSString from an function. As I understand, I need to [NSString... alloc] init] in order to create the string for return. Also, since I called alloc, I guess I have to autorelease the object myself however I'm getting the message "ARC forbids explicit message send of 'autorelease'" so.. how do I tell ios when I'm done with that allocated NSString?

- (NSString *) generateUID
{
    char foo[32];
    // create buffer of all capital psuedo-random letters
    for (int i = 0; i < sizeof(foo); i++)
       foo[i] = (random() % 25) + 65; // 0-25 + CAPITAL_A

    NSString *uid = [[NSString alloc] initWithBytes:foo length:sizeof(foo) encoding:NSASCIIStringEncoding];
    NSLog (@"uid: %@", uid);
    return (uid);
}

回答1:


ARC = automatic reference counting = the compiler adds the necessary releases and autorelases based on its analysis of your code. You don't see this of course because it happens behind the scenes. As mentioned by sosbom in his comment, you really should read the applicable documentation on the Apple website.




回答2:


You don't.

autorelease is just there for compatibilities sake, prior to iOS 5, you'd have to do:

Thing *myThing = [[Thing alloc] init]; // Retain count: 1
[myArray addObject:myThing];           // Retain count: 2
[myThing release];                     // Retain count: 1

With the above code, the responsability of holding the object is given to the array, when the array gets deleted, it will release its objects.

Or in the case of autorelease.

- (MyThing*)myMethod
{
    return [[[MyThing alloc] init] autorelease];
}

Then it would release the object once it gets to a NSAutoReleasePool and get removed once drained.

ARC takes care of that now, it pretty much inserts the missing pieces for you, so you don't have to worry about it, and the beauty of it, is that you get the advantages of reference counting (vs garbage collector), at the cost of an increased compile-time checking to do the ARC step, but your end users don't care about compile-time.

Add to that, that you now have strong and weak (vs their non-ARC siblings retain and assign, the later one still useful for non-retained things), and you get a nice programming experience without tracing the code with your eyes and counting the retain count on your left hand.




回答3:


Short answer is you don't! ARC will handle the memory management for you. When ARC is turned on, the compiler will insert the appropriate memory management statements such as retain and release messages.

It is best to use ARC as the compiler has a better idea of an object's life cycle and is less prone to human error.

One other important thing to note about ARC. ARC is not the same as traditional garbage collection. There is no separate process or thread running in the background, like java's GC, which deletes objects when there is no longer a reference to them. One could view ARC as compile time garbage collection :).

The only other thing to be aware of is reference cycles and bridging pointers/objects between Objective-C and Obj-C++/C. You might want to look-up http://en.wikipedia.org/wiki/Weak_reference

Hope this helps




回答4:


In general, you should define a constructor method in your class and put the alloc logic in that method. Then, it is much harder to make a type casting error as the alloc/init approach always return (id) and it is not very type safe. This what built-in classes like NSString do, like [NSString stringWithString:@"foo"], for example. Here is a nice little block you can use to write code that works both with older non-arc compilation and with arc enabled.

+ (AVOfflineComposition*) aVOfflineComposition
{
  AVOfflineComposition *obj = [[AVOfflineComposition alloc] init];
#if __has_feature(objc_arc)
  return obj;
#else
  return [obj autorelease];
#endif // objc_arc
}

You then declare the method and create an instance of the object like so:

AVOfflineComposition *obj = [AVOfflineComposition aVOfflineComposition];

Using the above approach is best, as it is type safe and the same code with with arc vs non-arc. The compiler will complain if you attempt to assign the returned object to a variable of another type.



来源:https://stackoverflow.com/questions/17603551/arc-forbids-autorelease

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!