Skip to content

Instantly share code, notes, and snippets.

@schmittsfn
Created October 15, 2018 14:10
Show Gist options
  • Select an option

  • Save schmittsfn/94207380e19df465988d1dcdea554b6d to your computer and use it in GitHub Desktop.

Select an option

Save schmittsfn/94207380e19df465988d1dcdea554b6d to your computer and use it in GitHub Desktop.

iOS Quiz

Swift

  1. What is the difference between var and let ?
  • let is a constant variable and thus can be assigned once only -- its value is immutable.
  • var is a variable that can be assigned multiple times -- its value is therefore mutable.
  1. What is an Optional? What happens when using the if let syntax?
  • An Optional is a wrapper around an object instance that returns nil when said instance doesn't have a valid value.
  • Using if let allows you to test an Optional instance against nil and change control flow depending on the result. The process of assigning an Optional to a variable inside a conditional statement is called 'Optional Binding'.
  1. What is the difference between if and guard?
  • if executes the code it envelops when its conditional expression evaluates to true.
  • guard executes the code it envelops when its conditional expression evaluates to false. Moreover, using guard the compiler expects control flow to leave the current scope upon "failure" of the conditional expression. guard therefore is most often used as a failsafe to literally "guard" against unwanted states.
  1. 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 final in a method's declaration or declaring the class that contains the method as final.
    • using Access Control methods like the keyword private to 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 Optimization mode to allow the compiler to infer whether internal declarations have any overrides or not.
    • not using the dynamic keyword which forces Swift to use dynamic dispatch.

General

  1. 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.
  1. 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.
  1. How is Dictionary implemented?
  • Dictionary is implemented as a hash table with convenience methods to operate on said hash table. More specifically, it's a struct whose keys must implement the Hashable protocol.
  1. 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 associatedtype member -- i.e. protocols in swift don't allow type parameters.

iOS

  1. What are the possible states of an iOS app?
  • Possible states are:
    • Not running
    • Inactive
    • Active
    • Background
    • Suspended
  1. 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.
  1. 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 deinit callback and observe whether they are deallocated.
  • A retain cycle can be fixed by using the weak keyword with the object that causes the retain cycle. weak references to an instance do not increment the retain count on an object and are auto-zeroing (their value becomes nil) in case the instance is deallocated (due to a retain count of 0). Another way to fix retain cycles is by using the keyword unowned on a reference to an instance which acts as a simple pointer to the latter without auto-zeroing behavior.
  1. 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 NotificationCenter contained in Apple's Foundation Framework.
    • Key-Value-Observing (KVO) via NSKeyValueObserving contained in Apple's Foundation Framework.
  1. 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 Operation or a subclass thereof to an OperationQueue.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment