2020 Big Factory Interview Questions

Posted May 25, 20208 min read

1. What is the difference between NSArray and NSSet?

  • The storage address in the NSArray memory is continuous, but the NSSet is not continuous
  • NSSet has high efficiency, internal hash search is used; NSArray search needs to be traversed
  • NSSet accesses elements through anyObject, NSArray accesses through subscripts

2. NSHashTable and NSMapTable?

  • NSHashTable is a generic version of NSSet, with weak references to elements and variable types; can be copied when accessing members
  • NSMapTable is a general version of NSDictionary, which has weak references to elements and variable types; it can be copied when accessing members

(Note:The difference between NSHashTable and NSSet:NSHashTable can set element weak reference/copyin through option, only variable type. But when adding object, NSHashTable takes twice as much time as NSSet.
The difference between NSMapTable and NSDictionary:same as above)

  1. Attribute keywords assign, retain, weak, copy
  • assign:used for basic data types and structures. If the object is decorated, when destroyed, the attribute value will not be automatically set to nil, which may cause a wild pointer.
  • weak:When the object reference count is 0, the attribute value is also automatically set to nil
  • retain:Strong reference type, which is equivalent to strong under ARC, but block cannot be modified with retain, because it is not safe to assign.
  • strong:Strong reference type, equivalent to copy when modifying the block.

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

  • Runtime will perform memory layout on the weak attribute and build a hash table:use the memory address of the weak attribute object as the key, and the value of the weak attribute(weak own address) as the value. When the object reference count is 0 dealloc, the weak attribute value is automatically set to nil.

5. Block circular reference, internal modification of external variables, three kinds of block

  • block strongly references self, self strongly references block
  • Internal modification of external variables:block does not allow modification of the value of external variables, where external variables refer to the memory address of the pointer in the stack. The role of \ _ \ _ block is to put the memory address of the external variable on the stack on the heap as long as the variable is used by the block.
  • Three kinds of blocks:NSGlobalBlack(global), NSStackBlock(stack block), NSMallocBlock(heap block)

6. How does KVO implement the bottom layer? Trigger KVO manually? How does swift implement KVO?

  • KVO principle:When observing an object, runtime will dynamically create a class inheriting from the object, and rewrite the setter method of the observed object. The rewritten setter method will be responsible for notifying all observed objects before and after calling the original setter method Finally, the isa pointer of the object will be pointed to the created subclass, and the object will become an instance of the subclass.
  • How to manually trigger KVO:In the setter method, manually implement two methods of NSObject:willChangeValueForKey, didChangeValueForKey
  • swift's kvo:a class inherited from NSObject, or willset/didset directly.

7. Why can't categroy add attributes? How to add? Difference from Extension? category overrides the original class method? Multiple category calling sequence

  • The memory layout of the category when Runtime is initialized has been determined, there is no ivar, so you cannot add attributes by default.
  • Use the associated objects of runtime, and override setter and getter methods.
  • Extenstion created at compile time, you can add member variable ivar, generally used as hidden class information. You must add the source code of the class, such as NSString, you cannot create an Extension.
  • The category method will be copied to the original when the runtime is initialized, and directly returned when the classification method is called, and the original class is no longer called. How to keep the original class also call( https://www.jianshu.com/p/40e28c9f9da5 ).
  • The calling sequence of multiple categories follows the compilation order in Build Phases-> Complie Source.

8. Similarities and differences between load method and initialize method. -Mainly talk about the execution time, their respective uses, will the methods that do not implement the subclass call the parent class?
load initialize When the app starts, when the runtime is initialized, the first method is called before the call of the calling method. Parent class-> this class-> category parent class-> this class(if there is a classification directly call the classification, this class will not be called) Whether the methods of the subclass are not implemented, will the parent class be called?

9. Understanding of runtime. -Mainly how to find the cache when the method is called, how to find the method, how to forward it when the method is not found, the memory layout of the object

When sending a message to an object in OC, the runtime will find the class to which the object belongs according to the isa pointer of the object, and then find the method to execute in the method list of the class and the method list of the parent class. If no method is found in the top-level parent class, message forwarding will be performed:Method resoution(implementation method), fast forwarding(forward to other objects), normal forwarding(complete message forwarding. Can be forwarded to multiple objects)

10. What is the difference between SEL and IMP in runtime?

Each class object has a method list. The method list stores the method name, method implementation, and parameter type. SEL is the method name(number), and IMP points to the first address of the method implementation.

11. Principles and usage scenarios of autoreleasepool?

  • The stack structure of a doubly linked list composed of several autorelease poolpages, objc \ _autoreleasepoolpush, objc \ _autoreleasepoolpop, objc \ _autorelease
  • Usage scenario:When creating a temporary variable multiple times causes the memory to rise, you need to delay the release
  • The memory structure of autoreleasepoolpage:4k storage size

12. When will the Autorelase object be released?

In the case where there is no hand to add Autorelease Pool, the Autorelease object is released at the end of the current runloop iteration, and the reason it can be released is that the system adds the automatic release pool Push and Pop in each runloop iteration.

13. What is the relationship between Runloop and threads? Runloop mode? What is the role of Runloop? Internal mechanism?

  • Each thread has a runloop, the runloop of the main thread is started by default.
  • mode:Mainly used to specify the priority of the event loop at runtime
  • Function:keep the program running continuously, handle various events at any time, save cpu resources(no event break to release resources), render screen UI

14. The occurrence and avoidance of locks and deadlocks used in iOS

  • @synchronized, semaphore, NSLock, etc.
  • Deadlock:Multiple threads access 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 is written in C language with high efficiency. NSOperation is an object-oriented encapsulation of GCD. For special needs, such as canceling tasks, setting task priority, and task status monitoring, NSOperation is more convenient to use.
  • NSOperation can set dependencies, and GCD can only be achieved through dispatch \ _barrier \ _async
  • NSOperation can observe the current operation execution status(execution/cancel) through KVO
  • NSOperation can set its own priority(queuePriority). GCD can only set the queue priority(DISPATCH \ _QUEUE \ _PRIORITY \ _DEFAULT), and cannot set the priority in the executed block
  • NSOperation can customize operations such as NSInvationOperation/NSBlockOperation, and GCD execution tasks can be customized package but not so high code reuse
  • Efficient GCD, relatively high NSOperation overhead

16, oc and js interaction

  • Block url
  • JavaScriptCore(only for UIWebView)
  • WKScriptMessageHandler(only for WKWebView)
  • WebViewJavaScriptBridge(third-party framework)

17. What are the advantages of Swift over OC?

The difference between 18, struct and Class

  • class can inherit, struct cannot
  • class is a reference type, struct is a value type
  • struct requires mutating keyword to modify property in function

19, access control keywords(public, open, private, filePrivate, internal)

  • public and open:public is inside the module, both class and func can be accessed/reloaded/inherited, external only can be accessed; and open can be
  • private and filePrivate:private modify class/func, which means it can only be used inside the current class source file/func, and it cannot be inherited and accessed externally; while filePrivate means it can only be accessed within the current swift source file
  • internal:can be accessed in the entire module or app, the default access level, writeable or not

20, OC and Swift mixed

  • OC calls swift:import "project name-swift.h" @objc
  • swift call oc:bridge file

21, map, filter, reduce? What is the difference between map and flapmap?

  • map:Each element in the array is converted by a certain method, and finally a new array is returned(xx.map({$0 \\ * $0}))
  • flatmap:similar to map, The difference is that the array returned by flatmap does not exist nil, and will unpack optional; and you can also open the nested array into a(\ [\ [1,2 ], \ [2,3,4 ], \ [5,6 ]]-> \ [1,2,2,3,4,5,6 ])
  • filter:user filter elements(xxx.filter({$0> 25}), filter out elements greater than 25 to form a new array)
  • reduce:calculate the combination of array elements as a value, and receive the initial value()

22, guard and defer

  • guard is used to process error data in advance, else exits the program, improving code readability
  • Defer delayed execution and reclaimed resources. Multiple defers are executed in reverse order, nested defer is executed first in the outer layer, then in the inner layer

23, try, try? And try!

  • try:catch exception manually
  • try ?:The system helps us deal with it, returns nil if there is an exception; returns the corresponding object if there is no exception
  • try !:Tell the system directly that there is no exception in this method. If there is an abnormal program, it will crash

24, @autoclosure:Automatically encapsulate an expression into a closure

25, throws and rethrows:when throwing another throws, change the former to rethrows

26, App startup optimization strategy? How to optimize before and after main function execution

  • Startup time = pre-main time + main time
  • Pre-main stage optimization:
  • Delete useless codes
  • Abstract repeated code
  • The things done by the + load method are delayed into initialize, or the things of + load should not take too much time
  • Reduce unnecessary framework, or optimize existing framework
  • Main stage optimization
  • Delayed execution of code in didFinishLauchingwithOptions
  • Optimized page for first rendering

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(UI not on Main Thread Crash(mechanism to be improved))

28, memory leak problem?

Mainly focused on circular reference issues, such as block, NSTime, perform selector reference counting issues.

29, UI Caton optimization?

30 Architecture & Design Mode

  • MVC design pattern introduction
  • MVVM introduction, the difference between MVC and MVVM?
  • ReactiveCocoa's hot and cold signals
  • Cache architecture design LRU solution
  • SDWebImage source code, how to achieve decoding
  • AFNetWorking source code analysis
  • Component implementation, middleware design
  • How does the hash table work? How to resolve conflicts

31, data structure & algorithm

  • Quick sort, merge sort
  • Two-dimensional array search(each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Please complete a function, enter such a two-dimensional array and an integer to determine the array Does it contain the integer)
  • Traversal of the binary tree:determine the number of layers of the binary tree
  • Single linked list judgment ring

32, Computer Basics

  1. http and https? socket programming? tcp, udp? get and post?
  2. tcp three-way handshake and four-way handshake
  3. The difference between process and thread