Swift Flashcards

1
Q

Differences Between Classes and Structs

A

Structs are are accessed as “by value” (using as a param creates a copy) and classes are accessed “by reference” (using as a param passes a pointer).

Classes can inherit from other classes, structs can’t

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which is Better - Class or Struct

A

Class better for inheritance

Struct better for passing between threads, optimize performance
(FASTER since they are allocated on STACK and classes are allocated on HEAP)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Advantages of Array

A

Easy access by index

More type-safe (can only contain 1 type of element)

Arrays have many built-in operations (add, remove, filter, sort, etc)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What property is used to allow objects to be serialized/deserialized ?

A

struct Person: Codable { .. }

All properties need to support codable to be serialized

Then use JsonDecoder()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to handle custom property names when encoding / decoding to JSON

A

Use CodingKeys enum

enum CodingKeys: String, CodingKey {
case name = “full_name”
case age
case address
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 5 different access levels in Swift

A

open - can be accessed / subclassed

public - accessible but not subclass-able

internal - accessible from same MODULE - DEFAULT LEVEL

fileprivate - accessible from same FILE

private - accessible from same class / struct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are extensions and why use them

A

Allow adding new functionality to class / struct (even if source not available)

Group related functionality

Add protocol conformance to a class /struct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can you add new properties to class/struct using extensions

A

Stored properties cannot be added

Computed properties can be added

Reason - A TYPES MEMORY LAYOUT IS DETERMINED AT COMPILE TIME

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are associated types ?

A

Generics for protocols

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why use a protocol ?

A

interface reusability with different types

Abstraction

Dependency injection - more flexible and testable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Auto Layout

A

content hugging - when high, view wants to become as SMALL AS POSSIBLE on that axis

compression resistance - when HIGH, the view wants to become AS BIG AS POSSIBLE on that axis

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are size classes

A

Currently only 2

compact and regular
A device has a size class for each dimension

In Storyboard, you can create ANY, ANY for the default layout and then specific layouts for a particular combination of size classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

UIViewController events (in order)

A

loadView() - Called before the view is created
viewDidLoad()
viewWillLayoutSubviews()
viewDidLayoutSubviews()
viewWIllAppear()
viewDidAppear()
viewWillDisappear()
viewDidDisappear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Ways to Pass Data Between UIViewControllers

A

A delegate method
Dependency injection (params passed when invoked)
Use a closure on the invoked VC
Post a notification

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are different ways to adjust UITableview cell sizes at runtime

A

tableView: heightForRowAtIndex:

Use self-sizing cells (must have a continuous sequence of constrains from top to bottom). rowHeight = automaticDimension

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Tell me about UIViewController navigationItem

A

Contains nav bar title

Contains rightBarButtonItem / leftBarButtonItem

17
Q

Ways to present a UIViewController

A

Fullscreen

PageSheet- partial screen (bottom). VC can be pulled down with a swipe

overCurrentContext - Hides the current VC context

18
Q

How to create custom transitions between VCs

A

Implement the UIViewControllerAnimatedTransitioning protocol

19
Q

How to share small data between app and app extension ?

A

UserDefaults

Setup AppGroup
Enable to app and extension in Portal
UpdateXcode project (the same)
Use appGroup identifier (suiteName:) when accessing UserDefaults

20
Q

How to store objects in USerDefaults ?

A

NSKeyedArchiver - converts object to Data type (more efficient on large objects)

JsonEncoder - also converts to Data object

21
Q

File System - What is the use case for Documents folder ?

A

Items created / edited by the user. backed up to iCloud

22
Q

File System - What is the use case for Library folder ?

A

App-specific data not created by the user - downloaded content, preferences. Backed up to iCloud

23
Q

File System - What is the use case for Cache folder ?

A

Storage for temporary files - not backed up to iCloud. Can be purged when device runs low on disk space

24
Q

File System - What is the use case for Temp folder

A

Temp files. Deleted when app is closed

25
Q

CocoaPods - difference between “pod install” and “pod update”

A

“pod install” installs the dependencies in the POD file

“pod update” updates the dependencies to the latest minor version

25
Q

How to implement Push Notifications in an app

A
  • Add Push Notif capability
  • Generate either:
    • Push SSL cert - good for 1 year, limited to single app
    • APNS token - good until revoked, can send to all apps
  • Regenerate and DL new provisioning profile
  • In application:didFinishLaunching, add call to requestAuthorization (for call user will see system dialog to allow / disallow PN)
  • if granted, on main thread call UIApplication.shared.registerForRemoteNotifications()
  • Handle callback to UIApplication didRegisterForRemoteNotifications (device token)
  • Handle PN while app is open - userNotificationCenter:willPresent:WithCompletionHandler
  • Handle clicking on PN - application:didReceiveRemoteNotification:userInfo:
26
Q

How is class inheritance and protocol extension different ?

A

Class inheritance is vertical (you get everything defined above in the hierarchy)

Protocol extensions are more horizontal. You can pick and choose which protocol you want to adapt (and leave others out). They can also work on structs and enums.

27
Q

Best Way to Approach Concurrency

A

1 - simple await call
2 - async let (for multiple concurrent tasks)
3 - BlockOperation / OperationQueue
4 - Task (allows cancelling work or passing results around)
5 - Task Group

28
Q

How do dispatch queues handle concurrency ?

A

When creating the dispatch queue, the attribute parameter determines if it is serial (default) or concurrent

29
Q

How to run code on the main thread ?

A

DispatchQueue.main.async(execute:)

30
Q

How do dispatch queues handle blocking ?

A

Method call used

dispatchQ.async { }
dispatchQ.sync {}

31
Q

How to enqueue work on an OperationQueue ?

A

let blockOp1 = BlockOperation.init(block: { … })
let blockOp2 = BlockOperation.init(block: {…})
let operationQ = OperationQueue()
opQ.addOperation(blockOp1)
opQ.addOperation(blockOp2)

Also .addOperation can be used to add operations directly without using Blocks

32
Q

How do OperationQueues handle concurrency ?

A

OperationQueue is concurrent by default FOR OPERATION BLOCKS not execution blocks

To get a serial OperationQueue, set operationQ.maxConcurrentOperationCount

33
Q

What is a good use for serial dispatch queues ?

A

Serial queues are often used to synchronize access to a specific resource, since it is guaranteed that only 1 task will execute at a time.