词条 | Automatic Reference Counting | ||||||||||||
释义 |
Automatic Reference Counting (ARC) is a memory management feature of the Clang compiler providing automatic reference counting for the Objective-C and Swift programming languages. At compile time, it inserts into the object code messages ARC differs from tracing garbage collection in that there is no background process that deallocates the objects asynchronously at runtime.[3] Unlike garbage collection, ARC does not handle reference cycles automatically. This means that as long as there are "strong" references to an object, it will not be deallocated. Strong cross-references can accordingly create deadlocks and memory leaks. It is up to the developer to break cycles by using weak references.[4] Apple Inc. deploys ARC in their operating systems, such as macOS ({{Nowrap|OS X}}) and iOS. Limited support (ARCLite)[5] has been available since {{Nowrap|Mac OS X}} Snow Leopard and iOS 4, with complete support following in {{Nowrap|Mac OS X}} Lion and iOS 5.[6] Garbage collection was declared deprecated in {{Nowrap|OS X}} Mountain Lion, in favor of ARC, and removed from the Objective-C runtime library in macOS Sierra.[7][8]Objective-CThe following rules are enforced by the compiler when ARC is turned on:
// Without ARC - (void)dealloc{[[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } // With ARC - (void)dealloc{[[NSNotificationCenter defaultCenter] removeObserver:self]; // [super dealloc] is called automatically }
Programs must use special casts, or calls to special functions, to tell the compiler more information about an object's lifetime. // Without ARC - (NSString *)giveMeAString{CFStringRef myString = [self someMethodThatCreatesACFString]; NSString *newString = (NSString *)myString; return [newString autorelease]; } // With ARC - (NSString *)giveMeAString{CFStringRef myString = [self someMethodThatCreatesACFString]; // retain count is 1 NSString *newString = (__bridge_transfer NSString *)myString; // the ownership has now been transferred into ARC return newString; }
// Without ARC - (void)loopThroughArray:(NSArray *)array{for (id object in array) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Create a lot of temporary objects [pool drain]; } } // With ARC - (void)loopThroughArray:(NSArray *)array{for (id object in array) { @autoreleasepool { // Create a lot of temporary objects } } }
Property declarationsARC introduces some new property declaration attributes, some of which replace the old attributes.
1. ^{{cite web|url=https://arstechnica.com/apple/2011/07/mac-os-x-10-7/10/#arc|title=Mac OS X 10.7 Lion: the Ars Technica review|last=Siracusa|first=John|date=July 20, 2011|publisher=Ars Technica|at=At section "Automatic Reference Counting"|work=Ars Technica|accessdate=November 17, 2016}} 2. ^{{cite book|last=Kochan|first=Stephen G.|title=Programming in Objective-C|year=2011|publisher=Addison-Wesley|location=Boston, Mass.|isbn=978-0321811905|pages=408|edition=4th}} 3. ^{{cite book |last=Hoffman |first=Kevin |title=Sams teach yourself Mac OS X Lion app development in 24 hours |year=2012 |publisher=Sams |location=Indianapolis, Ind. |isbn=9780672335815 |pages=73}} 4. ^{{cite web |url=http://clang.llvm.org/docs/AutomaticReferenceCounting.html#general |title=General |publisher=LLVM.org |work=Automatic Reference Counting |accessdate=15 August 2012}} 5. ^{{cite web |title=Objective-C Feature Availability Index |url=https://developer.apple.com/library/ios/releasenotes/ObjectiveC/ObjCAvailabilityIndex/ |publisher=Apple, Inc. |accessdate=2013-10-14}} 6. ^{{cite book |last=Sakamoto |first=Kazuki |title=Pro Multithreading and Memory Management for iOS and OS X with ARC, Grand Central Dispatch and Blocks |year=2012 |publisher=Apress |isbn=978-1430241164 |pages=xii}} 7. ^{{Cite web|url=https://arstechnica.com/apple/2012/07/os-x-10-8/17/#objective-c-enhancements|title=OS X 10.8 Mountain Lion: the Ars Technica review|last=Siracusa|first=John|date=July 25, 2012|at=At section "Objective-C enhancements"|access-date=November 17, 2016|work=Ars Technica}} 8. ^{{Cite web|url=https://web.archive.org/web/20170319065518/https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Introduction.html|title=Xcode 8 Release Notes|date=October 27, 2016|website=Apple Developer|access-date=March 19, 2017}} 9. ^1 2 3 4 5 6 7 {{cite web |title=Transitioning to ARC Release Notes |url=https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html |accessdate=14 September 2012}} 10. ^{{cite web|title=What's New in Xcode 4.2 – Automatic Reference Counting |url=https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_2.html#//apple_ref/doc/uid/00200-SW2 |publisher=Apple Inc. |accessdate=3 October 2012 |deadurl=yes |archiveurl=https://web.archive.org/web/20120820002015/http://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_2.html |archivedate=20 August 2012 |df= }} 11. ^{{Cite web|url=https://developer.apple.com/swift/blog/?id=10|title=Value and Reference Types|date=August 15, 2014|website=Apple Developer|access-date=November 17, 2016}} Zeroing weak referencesZeroing weak references is a feature in Objective-C ARC that automatically clears (sets to Zeroing weak references are indicated by using the declared property attribute Zeroing weak references are only available in Mac OS X Lion (10.7) or later and iOS 5 or later, because they require additional support from the Objective-C runtime. However, some OS X classes do not currently support weak references.[9] Code that uses ARC but needs to support versions of the OS older than those above cannot use zeroing weak references, and therefore must use Converting toXcode 4.2 or later provides a way to convert code to ARC.[10] As of Xcode 4.5, it is found by choosing Edit > Refactor > Convert to Objective-C ARC... Although Xcode will automatically convert most code, some code may have to be converted manually. Xcode will inform the developer when more complex use cases arise, such as when a variable is declared inside an autorelease pool and used outside it or when two objects need to be toll-free bridged with special casts. SwiftIn Swift, references to objects are strong, unless they are declared var myString: String // Can only be a string var myOtherString: String? // Can be a string or nil if let myString = myOtherString { // Unwrap the Optional } Accordingly, a strong reference to an object cannot be of type Optional, as the object will be kept in the heap until the reference itself is deallocated. A weak reference is of type Optional, as the object can be deallocated and the reference be set to nil. Unowned references fall in-between; they are neither strong nor of type Optional. Instead, the compiler assumes that the object to which an unowned reference points is not deallocated as long the reference itself remains allocated. This is typically used in situations where the target object itself holds a reference to the object that holds the unowned reference. var strongReference: MyClass // Strong reference, cannot be nil weak var weakReference: MyClass? // Weak reference, can be nil unowned var unownedReference: MyClass // Weak reference, cannot be nil Swift also differs from Objective-C in its usage and encouragement of value types instead of reference types. Most types in the Swift standard library are value types and they are copied by value, whereas classes and closures are reference types and passed by reference. Because value types are copied when passed around, they are deallocated automatically with the reference that created them.[11] See Also
References{{Reflist|30em}}External links
2 : Objective-C|Memory management |
||||||||||||
随便看 |
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。