Swift

1. What is the difference between class and structure?

  • Inheritance enables one class to inherit the characteristics of another.
  • Type casting enables you to check and interpret the type of a class instance at runtime.
  • Deinitializers enable an instance of a class to free up any resources it has assigned.
  • Reference counting allows more than one reference to a class instance.
  • Structures are always copied when they are passed around in your code, and do not use reference counting

2. What is Downcasting ?

When we’re casting an object to another type in Objective-C, it’s pretty simple since there’s only one way to do it. In Swift, though, there are two ways to cast — one that’s safe and one that’s not .

  • as used for upcasting and type casting to bridged type
  • as? used for safe casting, return nil if failed
  • as! used to force casting, crash if failed. should only be used when we know the downcast will succeed.

3. What is the difference Non-Escaping and Escaping Closures?

  • A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns.
  • When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter’s type to indicate that the closure is allowed to escape.

4. Pass by reference and by value?

In Swift parameters and return types of functions are either passed by reference or by value.

Passing by value results in a copy of the object, and changes to the copy will not affect the original. By default in Swift class instances are passed by reference and structs passed by value. Swift’s built-in data types like Array and Dictionary, are implemented as structs.

5. What is the difference open & public access level?

Open allows other modules to use the class and inherit the class; for members, it allows others modules to use the member and override it.

Public only allows other modules to use the public classes and the public members. Public classes can no longer be subclassed, nor public members can be overridden.

6. fileprivate vs private?

File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file. Syntax: fileprivate Example: fileprivate class SomeFilePrivateClass {}

Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration. Syntax: private Example: private class SomePrivateClass {}

7. Please explain final keyword into the 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).

8. What is the major purposes of Frameworks? Frameworks have three major purposes:

  • Code encapsulation
  • Code modularity
  • Code reuse

You can share your framework with your other apps, team members, or the iOS community. When combined with Swift’s access control, frameworks help define strong, testable interfaces between code modules.

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

10. What is a category and when is it used?

A category is a way of adding additional methods to a class without extending it. It is often used to add a collection of related methods. A common use case is to add additional methods to built in classes in the Cocoa frameworks. For example adding async download methods to the UIImage class.

results matching ""

    No results matching ""