Interview Questions: What are properties and instance variables in Objective-C and Swift?

Update: I'm working on a book that helps you ace iOS interviews. Sign up to the waitlist here: http://iosinterviewguide.com/

In this series of blog posts I talk about commonly asked tech iOS interview questions.

This could be a part of memory management question or a standalone question. Properties, instance variables, constants, and local variables are very important to understand working with Objective-C and Swift because they define how you refer and work with your data.

Expected answer:

Properties in Objective-C are used to store data in instances of classes. They define memory management, type, and access attributes of the values they store such as strong, weak, assign, readonly, readwrite, etc. Properties store values assigned to them in an instance variable that by convention has the same name as the property but starts with an underscore prefix. When you declare a property in Objective-C it will also synthesize it, meaning create a getter and setter to access and set the underlying instance variable.

strong, weak, assign property attributes define how memory for that property will be managed. It is going to be either strongly referenced, weakly referenced (set to nil if deallocated), or assigned (not set to nil if deallocated).

One great feature of Objective-C properties that is often overlooked is Key Value Observation (KVO). Every Objective-C property can be observed for changes enabling low-level Functional Reactive Programming capabilities.

In Swift, however, properties defined with a simple let or var and are strong by default. They can be declared as weak or unowned references with weak and unowned keywords before let/var. Swift properties in types are called stored properties. Unlike Objective-C properties, they do not have a backing instance variable to store their values. They do declare setters and getters that can be overridden though.

Swift enforces basic dependency injection with properties. If you define a let or var property it has to be either initialized in the property declaration and will be instantiated with that type's instance or it has to be injected in a designated initializer instead. Optional properties don't have to be initialized right away or injected because by their nature they can be nil.

Also, Swift properties can't be KVOed and instead have a greatly simplified mechanic built in - Property Observers (willSet/didSet). The only way to have property KVO in Swift is to subclass from NSObject or its subclasses.

Class or type properties are the properties defined for the entire type/class rather than individual instances of that type. In Swift, they can be defined with static keyword for value types (struct, enum) and with class keyword for class types. In Objective-C since Swift 3 and Xcode 8, you can also define class properties using class keyword in property declaration.

In both Swift and Objective-C properties can be lazy loaded. In Swift, you'd use @lazy directive in front of a property declaration. In Objective-C, you'd have to override property getter and set and initialize its value only if the underlying instance variable is nil.

Red flag:

You don't have to go too deep into the details of properties implementations and features in Swift and Objective-C but you have to know at least the basics of strong/weak/unowned referencing.

Update: I'm working on a book that helps you ace iOS interviews. Sign up to the waitlist here: http://iosinterviewguide.com/

comments powered by Disqus