Questions 41 to 45 in 50
Page 7
Why is
autoreleasepool
needed still after introduction of ARC?
To Explain essence of autoreleasepool in ARC there are different scenarios one of them is following
As part of Code Fragmentation one large block of code doing business logic could be spit into small chunks. To break to small chunks we use functions that return specific results to perform an aggregate resulting logic.
If we return a instance that is locally allocated in the function (instances allocated in function scope), the reciever may assign it to a retain property which indeed calls retain on instance. In such case the instance count is raised by 1 causing ARC to be misguided include additional or fewer release calls resulting in a leak or crash.
In this case by ensuring to call autorelease we can instruct compiler to treat it as non retain instance after returning it to reciever method so local retainship in reciever function ensures to handle it accordingly without actually causing memory leak.
In Swift this case is avoided by ensuring to have optional returns so they can be handled appropriately when unwrapping the optionals in reciever method.
As part of Code Fragmentation one large block of code doing business logic could be spit into small chunks. To break to small chunks we use functions that return specific results to perform an aggregate resulting logic.
If we return a instance that is locally allocated in the function (instances allocated in function scope), the reciever may assign it to a retain property which indeed calls retain on instance. In such case the instance count is raised by 1 causing ARC to be misguided include additional or fewer release calls resulting in a leak or crash.
In this case by ensuring to call autorelease we can instruct compiler to treat it as non retain instance after returning it to reciever method so local retainship in reciever function ensures to handle it accordingly without actually causing memory leak.
In Swift this case is avoided by ensuring to have optional returns so they can be handled appropriately when unwrapping the optionals in reciever method.
What is
dynamic
?
You use the
@dynamic
keyword to tell the compiler that you will fulfill the API contract implied by a property either by providing method
implementations directly or at runtime using other mechanisms such as dynamic loading of code or dynamic method resolution. It suppresses
the warnings that the compiler would otherwise generate if it can’t find suitable implementations. You should use it only if you know that
the methods will be available at runtime.
What is the difference between retain & assign?
Assign
creates a reference from one object to another without increasing the source’s retain count.if (_variable != object) { [_variable release]; _variable = nil; _variable = object; }
Retain
creates a reference from one object to another and increases retain count of source object.if (_variable != object) { [_variable release]; _variable = nil; _variable = [object retain]; }
How copy and readonly differ from each other? Copy vs Readonly?
Copy
is property access specifier that allows Object to keep its own copy of objects that are set as properties.
In other words copy allows to create new copy of the object with retain count of 1.Any object that is being assigned to a copy property must abide to
NSCopying
protocol.Copy can be used prevent any modifications of our object after assigning it to the other one also allowing our object to clear of memory without worrying about the other object in-memory status
Readonly
allows to indicate that the property is allowed for modifications only from its owner object. Trying to
assign to readonly property outside its implementation scope shall throw compilation error.When we compare
readonly
against copy
, Both Setter and Getter are created for copy property where as only getter is created for readonly.