iOS Interview Questions And Answers

1_h1kjhezFp3MYXCtOxMz4yg

  • What is made up of NSError object?
    There are three parts of NSError object a domain, an error code, and a user info dictionary. The domain is a string that identifies what categories of errors this error is coming from.
  • What is the difference strong, weak, read only and copy ?

    strongweakassign property attributes define how memory for that property will be managed.

    Strong means that the reference count will be increased and
    the reference to it will be maintained through the life of the object

    Weak, means that we are pointing to an object but not increasing its reference count. It’s often used when creating a parent-child relationship. The parent has a strong reference to the child but the child only has a weak reference to the parent.

    Read only, we can set the property initially but then it can’t be changed.

    Copy, means that we’re copying the value of the object when it’s created. Also prevents its value from changing.

  • What’s the difference between the frame and the bounds? 
    The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

    The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

    So, imagine a view that has a size of 100×100 (width x height) positioned at 25,25 (x,y) of its superview. The following code prints out this view’s bounds and frame:

    // This method is in the view controller of the superview
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSLog(@"bounds.origin.x: %f", label.bounds.origin.x);
        NSLog(@"bounds.origin.y: %f", label.bounds.origin.y);
        NSLog(@"bounds.size.width: %f", label.bounds.size.width);
        NSLog(@"bounds.size.height: %f", label.bounds.size.height);
    
        NSLog(@"frame.origin.x: %f", label.frame.origin.x);
        NSLog(@"frame.origin.y: %f", label.frame.origin.y);
        NSLog(@"frame.size.width: %f", label.frame.size.width);
        NSLog(@"frame.size.height: %f", label.frame.size.height);
    }

    And the output of this code is:

    bounds.origin.x: 0
    bounds.origin.y: 0
    bounds.size.width: 100
    bounds.size.height: 100
    
    frame.origin.x: 25
    frame.origin.y: 25
    frame.size.width: 100
    frame.size.height: 100

    So, we can see that in both cases, the width and the height of the view is the same regardless of whether we are looking at the bounds or frame. What is different is the x,y positioning of the view. In the case of the bounds, the x and y coordinates are at 0,0 as these coordinates are relative to the view itself. However, the frame x and y coordinates are relative to the position of the view within the parent view (which earlier we said was at 25,25).

    There is also a great presentation that covers UIViews. See slides 1-20 which not only explain the difference between frames and bounds but also show visual examples.

  • Explain MVC

    • The model is the brain of the application. It does the calculations and creates a virtual world for itself that can live without the views and controllers. In other words, think of a model as a virtual copy of your application, without a face!
    • view is the window through which your users interact with your application. It displays what’s inside the model most of the time, but in addition to that, it accepts users’ interactions. Any interaction between the user and your application is sent to a view, which then can be captured by a view controller and sent to the model.
    • Controllers in iOS programming usually refer to view controllers. Think of view controllers as a bridge between the model and your views. They interpret what is happening on one side (what the user does on the view side, or the information provided by the model) and use that information to alter the other side as needed.
  • 32- Explain MVVM
    MVVM in iOS means creating an object filled with data that your screen uses, separately from your Model classes. It usually maps all the items in your UI that consume or produce data, like labels, textboxes, datasources or dynamic images. It often does some light validation of input (empty field, is valid email or not, positive number, switch is on or not) with validators. These validators are usually separate classes not inline logic.

    Your View layer knows about this VM class and observes changes in it to reflects them and also updates the VM class when the user inputs data. All properties in the VM are tied to items in the UI. So for example a user goes to a user registration screen this screen gets a VM that has none of it’s properties filled except the status property that has an Incomplete status. The View knows that only a Complete form can be submitted so it sets the Submit button inactive now.

    Then the user starts filling in it’s details and makes a mistake in the e-mail address format. The Validator for that field in the VM now sets an error state and the View sets the error state (red border for example) and error message that’s in the VM validator in the UI.

    Finally, when all the required fields inside the VM get the status Complete the VM is Complete, the View observes that and now sets the Submit button to active so the user can submit it. The Submit button action is wired to the VC and the VC makes sure the VM gets linked to the right model(s) and saved. Sometimes Models are used directly as a VM, that might be useful when you have simpler CRUD-like screens.

    I’ve worked with this pattern in WPF and it works really great. It sounds like a lot of trouble setting up all those observers in Views and putting a lot of fields in Model classes as well as ViewModel classes but a good MVVM framework will help you with that. You just need to link UI elements to VM elements of the right type, assign the right Validators and a lot of this plumbing gets done for you without the need for adding all that boilerplate code yourself.

    Some advantages of this pattern:

    • It only exposes the data you need
    • Better testability
    • Less boilerplate code to connect UI elements to data

    Disadvantages:

    • Now you need to maintain both the M and the VM
    • You still can’t completely get around using the VC iOS.
  • What is the Swift main advantage?
    To mention some of the main advantages of Swift:

    • Optional Types, which make applications crash-resistant
    • Built-in error handling
    • Closures
    • Much faster compared to other languages
    • Type-safe language
    • Supports pattern matching
  • Swift’s pattern matching techniques

    • Tuple patterns are used to match values of corresponding tuple types.
    • Type-casting patterns allow you to cast or match types.
    • Wildcard patterns match and ignore any kind and type of value.
    • Optional patterns are used to match optional values.
    • Enumeration case patterns match cases of existing enumeration types.
    • Expression patterns allow you to compare a given value against a given expression.
  • What are benefits of Guard?
    There are two big benefits to guard. One is avoiding the pyramid of doom, as others have mentioned — lots of annoying if let statements nested inside each other moving further and further to the right. The other benefit is provide an early exit out of the function using break or using return. For more details please got to the link.
  • What is NSFetchRequest?

    NSFetchRequest is the class responsible for fetching from Core Data. Fetch requests are both powerful and flexible. You can use fetch requests to fetch a set of objects meeting the provided criteria, individual values and more.

    let request: NSFetchRequest<NSFetchRequestResult> = Level.fetchRequest()

    or

    let request: NSFetchRequest<Level> = Level.fetchRequest()

     

  • Explain NSFetchedResultsController?

    NSFetchedResultsController is a controller, but it’s not a view controller. It has no user interface. Its purpose is to make developers’ lives easier by abstracting away much of the code needed to synchronize a table view with a data source backed by Core Data.

    Set up an NSFetchedResultsController correctly, and your table will mimic its data source without you have to write more than a few lines of code.

  • Explain Forced Unwrapping

    When we defined a variable as optional, then to get the value from this variable, we will have to unwrap it. This just means putting an exclamation mark at the end of the variable.