Day-4 Flashcards
Composite Objects
A way to extend the definition of a class from one or more objects from other classes.
When you define a subclass, it inherits all the instance variables and methods of the parent class. In some cases, this is undesirable.
Retain Cycles
A retain cycle happens when two objects have strong references to each other.
A weak reference is a non-owning relationship where the source object does not retain the object to which it has a reference
Since blocks and closures capture strong references to all objects accessed in their bodies, if the object represented by self were to get a strong pointer to the block or closure, then we would have a retain cycle
Weak : Outlets, Self to use inside blocks.
Key Value Coding
NSKeyValueCoding : A protocol that defines mechanisms for indirectly accessing object properties
We access them with the “Key Path”
- (void)setValue:(id)value forKey:(NSString *)key;
- (id)valueForKey:(NSString *)key;
// Typical
self. name = @”Adam”;
self. email = @”me@me.com”;
// KVC
[self setValue:@”Adam” forKey:@”name”];
[self setValue:@”me@me.com” forKey:@”email”];
// KVC, Two properties deep.
NSNumber *length = [self valueForKeyPath:@”name.length”];
KVO
Alternative to ( didSet and willSet in Swift )
Another protocol. It is considered informal, meaning you wont see it advertised in the docs when objects conform, because NSObject is assumed to conform to it
Object properties can be observed indirectly by their key
addObserver:forKeyPath:options:context
observeValueForKeyPath:ofObject:change:context
If you mutate properties outside of the generated setter, you’ll need to call these methods manually to be KVO compliant:
- (void)willChangeValueForKey:(NSString *)key;
- (void)didChangeValueForKey:(NSString *)key;
New - check the new value from the observer
Old - check the previous value from the observer
KVO Gotchas
The concept to remove the observer so we can tell if we have an observer..
http://www.appcoda.com/understanding-key-value-observing-coding/
Memory Management
Memory management is all about cleaning up unused memory so that it can be used again
Strong Vs. Weak
Strong increment the retain count in the memory, which means the memory will hold on to that pointer.
(@property is important to not lose in the app so it’s Strong)
Weak is used to point at something but doesn’t use the memory to hold on to it.
(@outlet is weak because what if the storyboard is gone? the reference to the label or UIText will have no meaning staying there in code).
ARC (important subject for interviews)
ARC eliminates all the potential pitfalls associated with manual reference counting. Under the hood, reference counts are still being maintained and tracked. However, the system determines when to retain an object and when to autorelease or release it
By default, all object pointer variables are strong variables
The old object reference will be released before the assignment is made
Strong variables are initialized to zero by default
Weak variables references are used to avoid retain cycles and memory leaks
Turn off the MRC (memory reference code) manually
Turn off the Arc:
1) create a file as Cocoa touch with their subclass
2) go to the application “Build Phases menu” then Compile Sources
4) type in “-fno-objc-arc” in the file we just created and want to disable