1. List and explain: iOS Application States
Not running: has not been lanuched or terminated by the system
Inactive: Running in foreground and not receiving events Stay briefly on their way to be active.
Active:Running in foreground and receiving events.
Background: Running in background and executing code. Stay briefly on their way to be suspended.
Suspended: Remains in memory but does execute any code.
2. iOS Compiler optimizes references to string objects that have the same value
NSString *firstName = @"Jack";NSString *secondName = @"Jack";if (firstName == secondName) { NSLog(@"areEqual");} else { NSLog(@"areNotEqual");}
firstName and secondName are pointers to different objects that have the same value. But the optimization machanism of iOS compiler let both pointers actually pointing to same address.
In Swift, Closures and functions are also assigned by reference. If variable or constant A and B are assigned to the same closure or function, they gonna have exactly the same value in memory.
3. Concurrency
==Thread: Not suggested, hard to design
==Dispatch queues: First in First out only. GCD takes care of creating the needed threads and scheduling tasks to run on those threads.
==Operation Queue: Implemented by NSOperationQueue not limited to FIFO order and support complex execution order graphs for your tasks.
4.Swift Retain Cycle
1 class A { 2 3 let b:B 4 5 init() { 6 7 b = B() 8 9 b.a = self10 11 }12 13 deinit () {14 15 print("A deinit")16 17 }18 19 }20 21 class B {22 23 var a:A? = nil24 25 deinit {26 27 print(B deinit")28 29 }30 31 }
To resolve a retain cycle
When property A and B must not be nil, one should be assigned unowned reference and the other one should be implicitly unwrapped optional
When one of property A or B could be nil, the one which can't be nil should be assigned unowned.
When both of the property A and B could be nil, assign weak for one property.
5. Objective-C 单例写法 Singleton
@implementation MyManager+ (id)sharedManager { static MyManager * staticInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ staticInstance = [[self alloc] init]; }); return staticInstance;}@end
6. Available prefixs to keyword func in Swift
override: to override methods in superclass you must write override before func
class: to implement a class method you must write class before func
static: to implement a structures/enumerations method you must write static before func
mutating: for all structures/enumerations methods that could change the instance properties, you must write mutating before func
7. Closures Classification
Global Function: do not capture values
Nested Function: capture values from enclosing function
Closure Expression: A unamed closure that capture values from surrounding context
{(parameters) -> (return type) in statements}
8. Closure Shorthand
reversed = names.sort( { (s1: a href="" String /a , s2: a href="" String /a ) -> a href="" Bool /a in return s1 > s2 } )
Inferring Type from context
reversed = names.sort( { s1, s2 in return s1 > s2 } )
Implicit Returns from Single-Expression Closures
reversed = names.sort( { s1, s2 in s1 > s2 } )
Shorthand Argument Names
reversed = names.sort( { $0 > $1 } )
Operator Functions
reversed = names.sort(>)