iOS Interview Questions 🤯😖😒

In this article, I will try to add as many Q&A’s related to iOS . I will keep on updating this article. Hope it helps you guys 🙂.

Abhimuralidharan
8 min readMar 14, 2019
source: stocksnap.io

1. Do we have an extension in Objective — C?

The answer is YES.

2. Difference between a category and extension in objective C?

A category can be declared for any class, even if you don’t have the original implementation source code.

A class extension bears some similarity to a category, but it can only be added to a class for which you have the source code at compile time (the class is compiled at the same time as the class extension).

Categories allow you to add methods outside of the main interface file. Whereas Extensions must be implemented in main Interface file. Which means we can safely conclude you cannot use extensions for extending Builtin classes or Classes for which you don’t have the source code, there you should use Categories. And to use extensions you need access to the source of the class you are extending.

3. Fallthrough keyword in swift?

In Swift, switch statements don’t fall through the bottom of each case and into the next one. That is, the entire switch statement completes its execution as soon as the first matching case is completed.

In swift, fallthrough statement is used in switch case to execute case statements which is next to the matched case statements based on our requirements.

“case 4” is also executed even though it doesn’t match the criteria

The fallthrough keyword does not check the case conditions for the switch case that it causes execution to fall into. The fallthrough keyword simply causes code execution to move directly to the statements inside the next case (or default case) block, as in C’s standard switch statement behavior.

4. How to make a method inside a protocol optional in swift?

We can do it in two ways.

The pure — swift way should be like providing the default implementation using protocol extension.

The objective — C compatibility way is using the @objc and optional keyword.

Important: Drawback of objc — compatibility way is that MyProtocolObjc in the above example became a class — only protocol. A struct cannot conform to this protocol.

Error: non-class type ‘SomeStruct’ cannot conform to class protocol ‘MyProtocolObjc’

5. Reverse a string without using “reversed()” in-built function

If the interviewer asks you to write the code on a paper, just kill him on the spot🔫💣🔪. For me, it will be really difficult to write optimized code without xcode.

Last time, when this question was asked, this is what I thought of doing first.[ pen and paper method] .

extension String {func reverse() -> String {var tempString = “”for char in self {tempString = String(char) + tempString}return tempString}}

I was able to cut — shot it to a one-liner using Playgrounds . You have this logic using Higher-order function “Reduce” on the string. 🔥🔥🔥🔥🔥

extension String {
func reverse() -> String { return self.reduce(""){ "\($1)" + $0 } }
}

6. Explain the class hierarchy of UIButton.

NSObject → UIResponder → UIView → UIControl → UIButton.

UITextField, UITextView, UISlider, UIDatePicker, UIPageControl, UISegmentedControl, UIStepper, UISwitch etc are all inheriting from UIControl.

What are the roles and responsibilities of each superclass?

UIResponder: An abstract class responsible for responding to and handling events. A responder implements the touchesBegan(_:with:), touchesMoved(_:with:), first responder handling, keyboard, and input related events etc.

UIView: An object that manages the content for a rectangular area on the screen.

UIControl: It is the base class for controls.

  • It handles the control state like enabled, disabled, focused, highlighted etc.
  • It handles events like touchDown, touchupInside, valueChanged, editing events for UITextField etc.
  • Adding target, removing the target, handling actions etc.

7. SuperClass of UIViewController?

UIResponder → UIViewController

8. SuperClass of UIWindow?

UIView → UIWindow

9. How many windows are there for an iOS app?

An iOS app can only have one active key window at a time.

Apple doc says: Every application has at least one window that displays the application’s user interface on a device’s main screen. If an external display is connected to the device, applications can create a second window to present content on that screen as well.

10. Difference between Self and self in Swift?

‘Self’ is only available in a protocol or as the result of a method in a class.

Reference: Hacking with swift.

When you’re writing protocols and protocol extensions, there’s a difference between Self (capital S) and self (lowercase S). When used with a capital S, Self refers to the type that conforms to the protocol, e.g. String or Int. When used with a lowercase S, self refers to the value inside that type, e.g. “hello” or 556.

NB: BinaryInteger is a protocol.

extension BinaryInteger { func squared() -> Self { return self * self }}

11. What is the difference between static vs class functions/variables in Swift classes?

static and class both associate a method/property with a class, rather than an instance of a class. The difference is that subclasses can override class methods; they cannot override static methods. So, in a Class, static function keyword can be written like:

 final class someFunction()

So , static is internally final .

static methods are static dispatch means the compiler know which method will be executed at runtime as the static method can not be overridden while the class method can be a dynamic dispatch as subclass can override these.

12. Explain the final keyword or… How can we prevent overrides & subclassing in a class?

You can prevent a method, property, or subscript from being overridden by marking it as final. Do this by writing the final modifier before the method, property, or subscript’s introducer keyword (such as final var, final func, final class func, and final subscript).

You can mark an entire class as final by writing the final modifier before the class keyword in its class definition (final class). Any attempt to subclass a final class is reported as a compile-time error.

As mentioned earlier, you can also use static keyword which does the functionality of that of a final class.

Reference: StackOverflow

13. Explain atomicity in both Objective-C and swift?

The default value in Objective — C: atomic

The default value in Swift: non-atomic

Defining a property as atomic will guarantee that a valid value will be returned. Declaring a property atomic makes the compiler generate additional code that prevents concurrent access to the property. This additional code locks a semaphore, then gets or sets the property, and then unlock the semaphore. If thread A is calling the getter simultaneously with thread B and C calling the setter with different values, thread A may get any one of the three values returned — the one prior to any setters being called or either of the values passed into the setters in B and C. Likewise, the object may end up with the value from B or C, no way to tell.

Atomic properties suffer from minor performance hit due to locking and unlocking before and after setting/getting a value.

Non-atomic properties have no guarantee regarding the returned value. It can be the correct value, a partially written value or even some garbage value.

There is no atomic/non-atomic keyword in Swift. we should use locks/semaphore etc to make a property atomic when multiple threads try to access the same value.

Atomicity property attributes (atomic and nonatomic) are not reflected in the corresponding Swift property declaration, but the atomicity guarantees of the Objective-C implementation still hold when the imported property is accessed from Swift.

14. What is a Singleton pattern? How to create one in swift?

A Singleton can only have one instance for the lifetime of the application it exists in. Singletons exist to give us singular global state. Such examples are NSNotificationCenter, UIApplication, and NSUserDefaults.

final class Singleton {static let sharedInstance = Singleton() private init() {} // init should be private}

Any variable declared with the let keyword is a constant and, therefore, read-only and thread-safe.

Static keyword makes sure it is lazily initialized inside a dispatch_once block. Static is internally final by default.

dispatch_once: Executes a block object once and only once for the lifetime of an application.

Making the init method as private so that nobody can access the init() method directly and create a new instance of the Singleton class.

15. Can enums have stored properties?

Enums can have methods, subscripts, and computed properties. But it cannot have stored properties.

16. Can you install an iOS app on a real device without a paid developer account?

YES, you can. But, without enrolling in the Apple Developer Program, your app will only last for 7 days on your device. After that, you’ll have to re-deploy it to your device via Xcode. If you’re enrolled in the Apple Developer Program, you won’t have this inconvenience.

17. Can we make a let as weak

No, ‘weak’ must be a mutable variable, because it may change at runtime. When we declare something as weak, it becomes an optional. It may or may not have a value. It will be mutated [set to nil] once there is no reference to it.

class SomeClass { }class SomeOtherClass {weak let someClassObj: SomeClass? = nil 
// error:
'weak' must be a mutable variable, because it may change at runtime
}

18. Can we make a let as unowned

Yes. unowned references will always have a value. It is not an optional.

Note: UNOWNED references can be a let or a var.

19. Is it possible to create segues between different storyboards within the same project?

Yes. Using storyboard references.

20. What is Optional Chaining?

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil. If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil, the property, method, or subscript call returns nil. Multiple queries can be chained together, and the entire chain fails gracefully if any link in the chain is nil.

21. Bounds vs nativeBounds?

nativeBounds: This rectangle is based on the device in a portrait-up orientation. This value does not change as the device rotates.

bounds: This rectangle is specified in the current coordinate space, which takes into account any interface rotations in effect for the device. Therefore, the value of this property may change when the device rotates between portrait and landscape orientations.

Enjoy!!. I will be adding more questions in this article. Just follow me so that you don’t miss any of them.

If you enjoyed reading this post, please share and give some claps so others can find it 👏👏👏👏👏 !!!!

You can follow me on Medium for fresh articles. Also, connect with me on LinkedIn.

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

--

--