- What is the difference between
varandlet?
letis a constant variable and thus can be assigned once only -- its value is immutable.varis a variable that can be assigned multiple times -- its value is therefore mutable.
- What is an Optional? What happens when using the
if letsyntax?
- An Optional is a wrapper around an object instance that returns
nilwhen said instance doesn't have a valid value. - Using
if letallows you to test an Optional instance againstniland change control flow depending on the result. The process of assigning an Optional to a variable inside a conditional statement is called 'Optional Binding'.
- What is the difference between
ifandguard?
ifexecutes the code it envelops when its conditional expression evaluates totrue.guardexecutes the code it envelops when its conditional expression evaluates tofalse. Moreover, usingguardthe compiler expects control flow to leave the current scope upon "failure" of the conditional expression.guardtherefore is most often used as a failsafe to literally "guard" against unwanted states.
- How does Swift perform dynamic method dispatch?
- Dynamic method dispatch in Swift is performed at runtime using a virtual method table for each class that maps method name strings to function pointers pointing to the method's implementation. These vtables are generated during compilation.
Since accessing the function pointer via index inside the vtable adds some overhead, there exist ways in the Swift language to avoid dynamic dispatch and allow for direct calls to a method's implementation, namely:- using the keyword
finalin a method's declaration or declaring the class that contains the method asfinal. - using Access Control methods like the keyword
privateto allow the compiler to infer that a method can never be overridden. - using structs since they cannot be subclassed and thus only one implementation of a method can exist.
- using the
Whole Module Optimizationmode to allow the compiler to infer whetherinternaldeclarations have any overrides or not. - not using the
dynamickeyword which forces Swift to use dynamic dispatch.
- using the keyword
- What makes a race condition happen?
- When the expected state of another thread will never be fulfilled, preventing the current thread to continue execution beyond the location that depends on said state.
- What’s the difference between static and dynamic linking?
- When statically linking a library into your program said library's source code is added to your program's executable. Only code that is used inside your application is added -- said code is always loaded into memory during startup time causing longer startup times.
- Dynamically linking a library tells your program where to find the code outside your program's executable. Dynamically linked libraries' source code is loaded into memory only when referenced, either during startup or runtime.
- How is
Dictionaryimplemented?
Dictionaryis implemented as a hash table with convenience methods to operate on said hash table. More specifically, it's a struct whose keys must implement theHashableprotocol.
- Explain Type Erasure? Why is it necessary?
- Type Erasure is a pattern in generic programming that replaces concrete types with type parameters, effectively erasing an entity's or method's type allowing it instead to be determined at compile time.
- Type Erasure in Swift is necessary when working around the limitation of Protocols with Associated Types (PATs) which can only be used as generic constraints. The compiler cannot use PATs like concrete types since it would not be able to infer the type of the
associatedtypemember -- i.e. protocols in swift don't allow type parameters.
- What are the possible states of an iOS app?
- Possible states are:
- Not running
- Inactive
- Active
- Background
- Suspended
- How does TableViewCell reuse system work?
- The TableViewCell reuse system works by reusing instances of UITableViewCell that are not displayed rather than creating new instances for every row of the table view. This increases performance as well as reduce memory footprint.
- What is a Block Retain Cycle? How to detect and fix it?
- A Block Retain Cycle occurs when the block of a closure holds a strong reference to an object which itself holds a strong reference to the closure. The result of which is that neither can be deallocated.
- You can detect a retain cycle using:
- Xcode's built-in memory graph -- a tool that visualizes the ownership relationships between objects.
- Xcode's 'Allocations' and 'Leaks' instruments.
- Manual insertion of logs into an object's
deinitcallback and observe whether they are deallocated.
- A retain cycle can be fixed by using the
weakkeyword with the object that causes the retain cycle.weakreferences to an instance do not increment the retain count on an object and are auto-zeroing (their value becomesnil) in case the instance is deallocated (due to a retain count of 0). Another way to fix retain cycles is by using the keywordunownedon a reference to an instance which acts as a simple pointer to the latter without auto-zeroing behavior.
- What are the ways you can observe changes in an object’s state
- You can observe changes in an object's state using:
- a custom implementation of the Observer Pattern.
- Notifications via the
NotificationCentercontained in Apple's Foundation Framework. - Key-Value-Observing (KVO) via
NSKeyValueObservingcontained in Apple's Foundation Framework.
- What are the tools available for scheduling parallel jobs?
- Tools available for scheduling parallel jobs are:
- injecting blocks of code into dispatch queues via Grand Central Dispatch (or GCD).
- doing work in custom Threads'
main()function. - using a Thread's runloop to invoke selectors.
- adding an
Operationor a subclass thereof to anOperationQueue.
- When should you use CALayer?
- One should use CALayer directly when working with:
- more low-level, performance critical drawing like paths, gradients, stencils, shadows, image manipulation (e.g. masking, blending).
- custom and more complex animations using the CoreAnimation framework.
- Describe UIView's render cycle.
- A UIView's drawing cycle is triggered on demand when a UIView's content becomes either partially or fully visible to the user, or when the visible content is updated. Drawing instructions are placed inside UIView's
drawRect:method which gets called by the system whenever afore mentioned on demand rendering events occur.
- Explain how App Store Code Signing works.
- App Store Code signing works by using a public-private key pair to verify that any changes made to your application on the app store are indeed made by you, the owner of the private key.
- What are the rules of thumb for mixing Swift and ObjC code in an app?
- When using ObjC code, annotate method signatures and properties with nullability information to make sure you leverage Swift's type safety mechanisms.
- To decomplexify the code base, focus primarily on using one language and migrate code into said language where possible and time permitting.
- You have to improve the scrolling performance of a UITableView , how do you proceed?
- Use Time Profiler to help identify bottlenecks in the app's stack trace.
- Analyze and optimize each area accordingly; here a list of actions that will improve scrolling performance:
- Asynchronously load remote data used inside each cell.
- Do any processing work on data used inside the cell in the background.
- Cache data used inside the cells where possible.
- Use lower quality images when scrolling and replace them with higher quality images once scrolling has finished.
- Reduce the amount of auto-layout operations necessary for each cell.