ARC forbids autorelease?

后端 未结 4 2068
一向
一向 2020-12-22 07:56

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. Al

相关标签:
4条回答
  • 2020-12-22 08:33

    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.

    0 讨论(0)
  • 2020-12-22 08:36

    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.

    0 讨论(0)
  • 2020-12-22 08:37

    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.

    0 讨论(0)
  • 2020-12-22 08:55

    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

    0 讨论(0)
提交回复
热议问题