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 🙂.

Image for post
Image for post

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

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

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.

Image for post
Image for post

3. Fallthrough keyword in swift?

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.

Image for post
Image for post
“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?

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.

Image for post
Image for post

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.

Image for post
Image for post

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

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

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.

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

What are the roles and responsibilities of each superclass?

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?

8. SuperClass of UIWindow?

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

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?

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?

 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 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 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?

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?

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

17. Can we make a let as weak

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

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

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

20. What is Optional Chaining?

21. Bounds vs nativeBounds?

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 👏👏👏👏👏 !!!!

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

Written by

iOS and tvOS developer, dreamer, photographer 🤨

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store