iOS Developer Interview Questions

iOS Developer Interview Questions

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

What is method swizzling in Objective C and why would you use it?

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (22)

Section 1

(22 cards)

What is method swizzling in Objective C and why would you use it?

Front

Method swizzling allows the implementation of an existing selector to be switched at runtime for a different implementation in a classes dispatch table. Swizzling allows you to write code that can be executed before and/or after the original method. Swizzling should always be done in +load. Method swizzling affects global state, it is important to minimize the possibility of race conditions. +load is guaranteed to be loaded during class initialization. Also swizzling should always be done in a dispatch_once. Atomicity is one precaution, as it is a guarantee that code will be executed exactly once, even across different threads. Grand Central Dispatch's dispatch_once provides desirable behaviors, and should be considered as much as a standard practice for swizzling as thy are initializing singletons.

Back

What is KVC and KVO? Give an example of using KVC to set a value.

Front

KVC stands for Key-Value coding. It's a mechanism by which an object's properties can be accessed using string's at runtime rather than having to statically know the property names at development time. KVO stands for Key-Value Observing and allows a class to observe changes to a property value Example of KVC: @property (nonatomic, copy) NSString *name; NSString* n = [object valueForKey: @"name"]; [object setValue: @"Caleb" forKey:@"name"];

Back

Describe managed object context and the functionality that it provides.

Front

A managed object context (represented by an instance of NSManagedObjectContext) is basically a temporary "scratch pad" in an application for a (presumably) related collection of objects. These objects collectively represent an internally consistent view of one or more persistent stores. A single managed object instance exists in one and only one context, but multiple copies of an object can exist in different contexts. You can think of a managed object context as an intelligent scratch pad. When you fetch objects from a persistent store, you bring temporary copies onto the scratch pad where they form an object graph (or a collection of object graphs). You can then modify those objects however you like. Unless you actually save those changes, though, the persistent store remains unchanged. Key functionality provided by a managed object context includes: * Life-cycle management. The context provides validation, inverse relationship handling, and undo/redo. Through a context you can retrieve or "fetch" objects from a persistent store, make changes to those objects, and then either discard the changes or commit them back to the persistent store. The context is responsible for watching for changes in its objects and maintains an undo manager so you can have finer-grained control over undo and redo. You can insert new objects and delete ones you have fetched, and commit these modifications to the persistent store. * Notifications. A context posts notifications at various points which can optionally be monitored elsewhere in your application. * Concurrency. Core Data uses thread (or serialized queue) confinement to protect managed objects and managed object contexts. In OS X v10.7 and later and iOS v5.0 and later, when you create a context you can specify the concurrency pattern with which you will use it using initWithConcurrencyType:

Back

What is the reuseIdentifier used for?

Front

The reuseIdentifier is used to indicate that a cell can be re-used in a UITableView. For example, when the cell looks the same, but has different content. The UITableView will maintain an internal cache of UITableViewCell's with the reuseIdentifier and allow them to be re-used when dequeueReusableCellWithIdentifier is called. By re-using table cell's the scroll performance of the tableView is better because new views do not have to be created.

Back

What mechanisms does iOS provide to support multi-threading?

Front

NSThread: creates a new low-level thread which can be started by calling the start method NSOperationQueue: allows a pool of threads to be created and used to execute NSOperations in parallel. Grand Central Dispatch: a modern feature of Objective-C that provides a rich set of methods and API's to use in order to support common multi-threading tasks. GCD provides a way to queue tasks for dispatch on either the main thread, a concurrent queue (tasks are run in parallel), or a serial queue (tasks that run in FIFO order)

Back

What is the Responder Chain

Front

When an event happens in a view, for example a touch event, the view will fire the event to a chain of UIResponder objects associated with the UIView. The first responder is the UIView itself, if it does not handle the event then it continues up the chain until UIResponder handles the event. The chain will include UIViewController's parent UIViews and their associated UIViewControllers. If none of those handle the event then the UIWindow is asked if it can handle it and finally if that doesn't handle the event then the UIApplicationDelegate is asked.

Back

What is AutoLayout? What does it mean when a constraint is "broken" by iOS?

Front

AutoLayout is way of laying out UIViews using a set of constraints that specify the location and size based relative to other views or based on explicit values. AutoLayout makes it easier to design screens that resize and layout out their components better based on the size and orientation of a screen. _Constraint_s include: * setting the horizontal/vertical distance between 2 views * setting the height/width to be a ratio relative to a different view * a width/height/spacing can be an explicit static value Sometimes constraints conflict with each other. For example imagine a UIView which has 2 height constraints: one says make the UIView 200px high, and the second says make the height twice the height of a button. If the iOS runtime can not satisfy both of these constraints then it has to pick only one. The other is then reported as being "broken" by iOS.

Back

What considerations do you need when writing a UITableViewController which shows images downloaded from a remote server?

Front

Depending on how many images are being downloaded... Only download the image when the cell is scrolled into view. Which is when cellForRowAtIndexPath is called. Downloading the image asynchronously on a background thread so the UI doesn't get blocked and the user can keep scrolling. When the image has downloaded for a cell, check if that cell is still in the view or whether it has been re-used by another piece of data. If it's been re-used then we should discard the image, otherwise switch back to the main thread to change the image on the cell. * Could also mention offline caching for images if applicable * Placeholder images while the images are being downloaded

Back

What are "strong" and "weak" references? Why are they important and how can they be used to help control memory management and avoid memory leaks?

Front

By default, any variable that points to another object does so with what is referred to as a "strong" reference. A retain cycle occurs when two or more objects have reciprocal strong references (i.e., strong references to each other). Any such objects will never be destroyed by ARC (iOS' Automatic Reference Counting). Even if every other object in the application releases ownership of these objects, these objects (and, in turn, any objects that reference them) will continue to exist by virtue of those mutual strong references. This therefore results in a memory leak. Reciprocal strong references between objects should therefore be avoided to the extent possible. However, when they are necessary, a way to avoid this type of memory leak is to employ weak references. Declaring one of the two references as weak will break the retain cycle and thereby avoid the memory leak. To decide which of the two references should be weak, think of the objects in the retain cycle as being in a parent-child relationship. In this relationship, the parent should maintain a strong reference (i.e., ownership of) its child, but the child should not maintain maintain a strong reference (i.e., ownership of) its parent.

Back

Explain the difference between atomic and nonatomic synthesized properties

Front

Atomic and non-atomic refers to whether the setters/getters for a property will automically read and write values to the property. When the atomic keyword is used on a property, any access to it will be "synchronized". Therefore a call to the getter will be guaranteed to return a valid value, however this does come with a small performance penalty. Hence in some situations nonatomic is used to provide faster access to a property, but there is a chance of a race condition causing the property to be nil under rare circumstances (when a value is being set from another thread and the old value was released from memory but the new value hasn't yet been fully assigned to the location in memory for the property.)

Back

How would you securely store private user data offline on a device? What other security best practices should be taken?

Front

If the data is extremely sensitive then it should never be stored offline on the device because all devices are crackable. The keychain is one option for storing data securely. However it's encryption is based on the pin code of the device. User's are not forced to set a pin, so in some situations the data may not even be encrypted. In addition the users pin code may be easily hacked. A better solution is to use something like SQLCipher which is a fully encrypted SQLite database. The encryption key can be enforced by the application and separate from the user's pin code. Other security best practices are: Only communicate with remote servers over SSL/HTTPS. If possible implement certificate pinning in the application to prevent man-in-the-middle attacks on public WiFi. Clear sensitive data out of memory by overwriting it. Ensure all validation of data being submitted is also run on the server side.

Back

What is the difference between viewDidLoad and viewDidAppear? Which should you use to load data from a remote server to display in the view?

Front

viewDidLoad is called when the view is loaded, whether from a Xib file, storyboard, or programmatically created. ViewDidAppear is called every time the view is presented on the device. Which to use depends on the use case for your data. If the data is fairly static and not likely to change then it can be loaded in viewDidLoad and cached. However if the data changes regularly then using viewDidAppear to load the better is better. In both situations, the data should be loaded asynchronously on a background thread to avoid blocking the UI.

Back

What is a protocol, how do you define your own and when is it used?

Front

A protocol is blueprint for a class. It defines a list of required and optional methods that a class must/can implement if it conforms to the protocol. Any class can implement a protocol and other classes can then send messages to that class based on the protocol methods without it knowing the type of class. * Common use case: UITableView and UICollectionView

Back

What's the difference between not-running, inactive, active, background and suspended execution states?

Front

* Not Running: The app has not been launched or was running but was terminated by the system * Inactive: The app is running in the foreground but is currently not receiving events. (It may be executing other code though.) An app usually stays in this state only briefly as it transitions to a different state * Active: The app is running in the foreground and is receiving events. This is the normal mode for foreground apps. * Background: The app is in the background and executing code. Most apps enter this state briefly on their way to being suspended. * Suspended: The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code. When a low-memory condition occurs, the system may purge suspended apps without notice to make more space for the foreground app.

Back

Explain the difference between copy and retain

Front

Retaining an object means the retain count increases by one. This means the instance of the object will be kept in memory until its retain count drops to zero. The property will store a reference to this instance and will share the same instance with anyone else who retained to it too. Copy means the object will be cloned with duplicate values. It is not shared with any one else.

Back

What is MVC? How is it implemented in iOS? What are some pitfalls you've experienced with it? Are there any alternatives to MVC?

Front

MVC stands for Model, View, Controller. It is a design pattern that defines how to separate out logic when implementing user interfaces. In iOS, Apple provides UIView as a base class for all Views, UIViewController is provided to support the Controller which can listen to events in a View and update the View when data changes. The Model represents data in an application and can be implemented using any NSObject, including data collections like NSArray and NSDictionary. Some of the pitfalls that people hit are bloated UIViewController and not separating out code into classes beyond the MVC format. In terms of alternatives, this is pretty open ended. The most common alternative is MVVM using ReactiveCocoa, but others include VIPER and using Functional Reactive code.

Back

What's the difference between an "app ID" and a "bundle ID" and what is each used for?

Front

An App ID is a two-part string used to identify one or more apps from a single development team. The string consists of a Team ID and a bundle ID search string, with a period (.) separating the two parts. The Team ID is supplied by Apple and is unique to a specific development team, while the bundle ID search string is supplied by the developer to match either the bundle ID of a single app or a set of bundle IDs for a group of apps. Because most people think of the App ID as a string, they think it is interchangeable with Bundle ID. It appears this way because once the App ID is created in the Member Center, you only ever use the App ID Prefix which matches the Bundle ID of the Application Bundle. The bundle ID uniquely defines each App. It is specified in Xcode. A single Xcode project can have multiple Targets and therefore output multiple apps. A common use case for this is an app that has both lite/free and pro/full versions or is branded multiple ways.

Back

What's the difference between using a delegate and notification?

Front

Both are used for sending values and messages to interested parties. A delegate is a one-to-one communication and is a pattern promoted by Apple. In delegation the class raising events will have a property for the delegate and will typically expect it to implement some protocol. The delegating class can then call the delegate's protocol methods. Notification allows a class to broadcast events across the entire application to any observing parties. The broadcasting class doesn't need to know anything about the listeners for this event, therefore notification is very useful in helping to decouple components in an application.

Back

A product manager in your company reports that the application is crashing. What do you do?

Front

1. get the exact steps to reproduce it. 2. find out the device, iOS version. 3. do they have the latest version? 4. get device logs if possible. Once you can reproduce it or have more information then start using tooling. Let's say it crashes because of a memory leak, I'd expect to see someone suggest using Instruments leak tool. A really impressive candidate would start talking about writing a unit test that reproduces the issue and debugging through it. Other variations of this question include slow UI or the application freezing. Again the idea is to see how you problem solve, what tools do you know about that would help and do you know how to use them correctly.

Back

What's your preference when writing UI's? Xib files, Storyboards or programmatic UIView?

Front

There's no right or wrong answer to this, but it's great way of seeing if you understand the benefits and challenges with each approach. Storyboard's and Xib's are great for quickly producing UI's that match a design spec. They are also really easy for product managers to visually see how far along a screen is. Storyboard's are also great at representing a flow through an application and allowing a high-level visualization of an entire application. Storyboard's drawbacks are that in a team environment they are difficult to work on collaboratively because they're a single file and merges become difficult to manage. Storyboards and Xib files can also suffer from duplication and become difficult to update. For example if all button's need to look identical and suddenly need a color change, then it can be a long/difficult process to do this across storyboards and Xibs. Programmatically constructing UIView's can be verbose and tedious, but it can allow for greater control and also easier separation and sharing of code. They can also be more easily unit tested.

Back

What is the difference between atomic and nonatomic properties? Which is the default for synthesized properties? When would you use one vs. the other?

Front

Atomic * is the default behavior * will ensure the present process is completed by the CPU, before another process accesses the variable * is not fast, as it ensures the process is completed entirely Non-Atomic *is NOT the default behavior *faster (for synthesized code, that is, for variables created using @property and @synthesize) *not thread-safe *may result in unexpected behavior, when two different process access the same variable at the same time

Back

What are blocks and how are they used?

Front

Blocks are a way of defining a single take or unit of behavior without having to write an entire Objective-C class. Under the hood Blocks are still Objective-C objects. They are a language level feature that allow programming techniques like lambdas and closures to be supported in Objective-C. Creating a block in Objective-C uses ^{} syntax: myBlock = ^{ NSLog(@"This is a block"); } * it can be invoked by so: myBlock(); * It is essentially a function pointer which also has a signature that can used to enforce type safety at compile and run-time. For example you can pass a block with a specific signature to a method or give it parameters.

Back