What is the difference between NSArray and NSSet?

  • NSArray memory stores contiguous addresses, whereas NSSet memory does not
  • NSSet is efficient and uses hash search internally. NSArray lookup is traversal
  • NSSet accesses elements via anyObject and NSArray via subscript

2, NSHashTable and NSMapTable?

  • NSHashTable is a generic version of NSSet, weak references to elements, mutable types; You can copy a member while accessing it
  • NSMapTable is a generic version of NSDictionary, with weak references to elements and mutable types. You can copy a member while accessing it

NSHashTable can use option to set element weak reference /copyin, only mutable type. But NSHashTable takes twice as long as NSSet to add objects. NSMapTable and NSDictionary:

3. Assign, retain, weak, copy

  • Assign: Used for basic data types and structures. If the object is decorated, the property value will not be set to nil when destroyed, possibly resulting in wild Pointers.
  • Weak: When the object reference count is 0, the attribute value is automatically set to nil
  • Retain: A strong reference type. This is equivalent to strong in ARC, but blocks cannot be modified with retain because it is unsafe to be the same as assign.
  • Strong: indicates a strong reference type. It is equivalent to copy when modifying blocks.

4. How does the weak attribute automatically set to nil?

  • The Runtime uses the memory layout of the weak attribute to create a hash table. The memory address of the weak attribute is the key and the value of the weak attribute (the address of the weak attribute) is value. When the object reference count is 0 dealloc, the weak property value is automatically set to nil.

5. Circular reference of blocks, internal modification of external variables, and three types of blocks

  • Block strongly references self, and self strongly references block
  • Internal modification of external variables: Blocks do not allow modification of external variables, which are the memory addresses of Pointers on the stack. __block puts the memory address of an external variable in the stack into the heap whenever it is observed to be used by a block.
  • There are three types of blocks: NSGlobalBlack, NSStackBlock, and NSMallocBlock.

6. The underlying implementation principle of KVO? Trigger KVO manually? How does Swift implement KVO?

  • KVO principle: When observing an object, the Runtime dynamically creates classes that inherit from the object and overwrites setter methods of the object being observed. The overridden setter notifies all observed objects before and after the original setter method is called that the object is worth changing, and finally points the isa pointer to the created subclass. The object becomes an instance of the subclass.
  • How to trigger KVO manually: In setter methods, manually implement NSObject methods: willChangeValueForKey, didChangeValueForKey
  • Swift kVO: inherits classes from NSObject, or implements willSet/didSet directly.

7. Why can’t CateGroy add attributes? How do you add? Difference with Extension? Category overrides the original class method? Call order of multiple categories

  • The memory layout of Categroy is determined when the Runtime is initialized. There is no IVAR, so attributes cannot be added by default.
  • Use the runtime’s associated object and override setter and getter methods.
  • Extenstion is created at compile time to add the member variable ivar, which is used to hide class information. You must have the source code of the class to add, such as NSString can not create Extension.
  • The category method will be copied back to the original one when the Runtime initializes it, and will be returned when the classification method is called without calling the original class. How to keep the original class also calls (www.jianshu.com/p/40e28c9f9… .

The load and Initialize methods are different. Will methods that do not implement subclasses call their parent classes? When the app starts, the Runtime initializes the first method in the call sequence parent class -> This class -> This class parent class -> this class (if there is a class directly call the class, This class does not call) does a method that does not implement a subclass call its parent class No Yes Does the parent class implement it No Yes

image

9. Understanding runtime. How to find a method when it is called, how to forward a method when it is not found, the memory layout of an object

When the OC sends a message to an object, the Runtime finds the class to which the object belongs based on the object’s ISA pointer, and then looks for method execution in the list of methods of that class and its parent class. If no Method is found in the top-level parent class, message forwarding is performed: Method resoution, fast forwarding, and Normal forwarding. Can be forwarded to multiple objects)

11. How does AutoReleasepool work and how to use it?

  • The stack structure of a bidirectional linked list composed of several AutoReleasepoolPages, objC_AutoReleasepoolPush, objC_AutoReleasepoolPOP, and objC_AutoRelease
  • Usage scenario: When temporary variables are created for several times, memory increases, and delayed release is required
  • Autoreleasepoolpage memory structure: 4K storage size

image

12. When will Autorelase objects be released?

In the absence of a manual Autorelease Pool, the Autorelease object is released at the end of the current runloop iteration, and it is released because the system adds automatic release pools Push and Pop to each runloop iteration.

13, How does Runloop relate to threads? Runloop mode? What does Runloop do? Internal mechanism?

  • Each thread has a runloop, and the main thread’s runloop starts by default.
  • Mode: mainly used to specify the priority of the event loop at run time
  • Functions: Keep the program running continuously, handle various events at any time, save CPU resources (no event breaks to release resources), render screen UI

14. Occurrence and avoidance of locks and deadlocks used in iOS

  • @synchronized, semaphore, NSLock, etc
  • Deadlock: Multiple threads accessing the same resource at the same time, causing a loop to wait. GCD uses asynchronous threads, parallel queues

15. The difference between NSOperation and GCD

  • The bottom layer of GCD uses C language to write efficiently, and NSOperation is the object-oriented encapsulation of GCD. NSOperation is more convenient to use for special requirements, such as task cancellation, task priority setting, and task status monitoring.
  • NSOperation can set dependencies, whereas GCD can only be implemented with dispatch_barrier_async
  • NSOperation can observe the current operation execution status (execute/cancel) through KVO.
  • NSOperation can set its own queuePriority. GCD can only set queue priority (DISPATCH_QUEUE_PRIORITY_DEFAULT) and cannot set priority in executing blocks
  • NSOperation can customize the operation such as NSInvationOperation/NSBlockOperation, while the GCD mission can customize the package but there is no such a high degree of code reuse
  • GCD is efficient, but NSOperation costs are relatively high

Oc interacts with JS

  • Intercept the url
  • JavaScriptCore(UIWebView only)
  • WKScriptMessageHandler(WKWebView only)
  • WebViewJavaScriptBridge(third-party framework)

17. What are the advantages of SWIFT compared with OC?

Struct, struct, Class

  • Classes can be inherited, structs can’t
  • Class is a reference type and struct is a value type
  • Struct requires mutating keyword modifier when modifying property in function

19, Access control keywords (public, Open, private, filePrivate, internal)

  • Public and open: Public in the module, class and func can be accessed/overloaded/inherited, external access only; Open will do
  • Private and filePrivate: Private modifies class/func, indicating that it can only be used inside the current class source file /func. External files cannot be inherited or accessed. FilePrivate means that it can only be accessed within the current Swift source file
  • Internal: it can be accessed in the entire module or app, with default access level and write or not

20. OC and Swift

  • OC calls swift: import “project name -swift.h” @objc
  • Swift calls oc: bridge files

21. Map, Filter, reduce? What is the difference between map and FlapMap?

  • Map: Each element of an array is transformed by a method that returns a new array (xx.map({0}))
  • Flatmap: Similar to map, except that flatMap does not return nil and unpacks optional arrays. And can also open the nested array into a ([[1, 2], [4] 2, [5, 6]] – > [1,2,2,3,4,5,6])
  • Filter: the user filters elements (xxx.filter({$0 > 25}), filter out elements greater than 25 to form a new array)
  • Reduce: evaluates array elements to a value and receives the initial value ()

image

22. Guard and defer

  • Guard is used to pre-process error data and else exits the program to improve code readability
  • Defer defer and reclaim the resource. Multiple defers execute in reverse order, with nested defers executing first the outer layer and then the inner layer

23, Try, try? With the try!

  • Try: manually capture exceptions
  • try? : The system handles it for us, returns nil if there is an exception; No exception returns the corresponding object
  • try! : Directly tells the system that this method has no exceptions. The program will crash if an exception occurs

24, @autoClosure: Wraps an expression into a closure automatically

Throws and REthrows: Throws the other throws, change the former to REthrows

26. App Startup Optimization strategy? How to optimize before and after main

  • Start time = Pre-main Duration + Main duration
  • Pre-main phase optimization:
  • Delete useless code
  • Abstract duplicate code
  • Things that the +load method does are deferred to initialize, or things that the +load method does not take too long
  • Reduce unnecessary frameworks or optimize existing frameworks
  • Main stage optimization
  • DidFinishLauchingwithOptions code delay in the execution
  • Page optimization for rendering is enabled for the first time

27, Crash protection?

  • unrecognized selector crash
  • KVO crash
  • NSNotification crash
  • NSTimer crash
  • Container crash (array out of bounds, insert nil, etc)
  • NSString crash (string operation crash)
  • Bad Access Crash (wild pointer)
  • UI not on Main Thread Crash

28. Memory leaks?

It focuses on circular reference problems, such as block, NSTime, perform Selector reference counting problems.

29. UI stuck optimization?

Architecture & Design patterns

  • Introduction to the MVC design pattern
  • MVVM introduction, MVC and MVVM differences?
  • ReactiveCocoa heat signal and cold signal
  • Cache architecture design LRU scheme
  • SDWebImage source code, how to achieve decoding
  • AFNetWorking source code analysis
  • Componentization implementation, middleware design
  • How does a hash table work? How to Resolve conflict

31. Data structures & Algorithms

  • Quicksort, merge sort
  • Two-dimensional array lookup (each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function that takes such a two-dimensional array and an integer to determine if the integer is in the array.
  • Binary tree traversal: judge the number of layers of the binary tree
  • Single linked list judgment ring

32. Basic computer skills

  1. HTTP and HTTPS? Socket programming? TCP, udp? The get and post?
  2. TCP three-way handshake and four-way handshake
  3. The difference between processes and threads

By Malassa spring Source: CSDN