Swift (general) Flashcards Preview

Programming > Swift (general) > Flashcards

Flashcards in Swift (general) Deck (40):
1

What is a UIEelement?

It's an object class in the view controller (eg a button)

2

What is an @IBAction?

@IBActions are connected to UIElements (eg a button). They call code when the button is tapped. Connects ViewController (eg with button) to the code - eg button is controlling the code execution.

3

What is an @IBOutlet

@IBOutlets are connected to UIElements (eg button). They change the look of the UIElement (button) in the ViewController. Connects code to ViewController - eg the code changes the look of the button.

4

What is UINavigation Controller?

UINavigation Controller is a class that lets you navigate between different UIViewControllers (ie screens). It can handle a stack of different UIViewControllers (ie the screens). The first stack is called the rootViewController

5

What does AVFoundation framework do?

It deals with audio and video

eg AVAudio class

6

What is an AVAudioSession?

Need to record and playback audio. There's just one that exists for the entire time the app is running. You grab it with the .sharedInstance() method - ie AVAudioSession.sharedInstance()

7

What does NSSearchPathForDirectoriesInDomains

Creates a list of directory search paths

8

What is delegation?

When you get one object to do the job of anothers

9

What is a gesture recogniser?

A subclass of UIGestureRecogniser that detects a specific touch sequence and calls an action on its target when the sequence is detected. eg pinch, swipe, tap

10

What is the scene dock?

It's the row of icons above the canvas (ie ViewController)

11

What does ctrl + dragging from ViewController (yellow circle) to the UIElement (eg button) do?

It connects an @IBOutlet

12

What does ctrl + dragging from a UIElement (eg button) to the ViewController (yellow circle)?

It connects an @IBAction

13

What is a tag?

Found in the Attributes Inspector, a tag is an integer that you can use to identify View objects in your app

(Related method is -[UIView tag]

14

What is a framework?

A framework a shared library of of code that includes associated resources such as interface files and images (eg MKMapView) (can be added in xcode using imports UK for eg)

15

What is UITabBarController?

UITabBarController is an array of view controllers. It also has a tab bar at the bottom of the screen with a tab for each view controller in its array.

16

What is a function?

A function is a self contained piece of code that performs a specific task. The three types in swift are Global Functions, Nested Functions, and Methods

17

Where are Global Functions?

Global Functions are functions that can be called from anyplace in an app (ie in a class) (eg print, min, and abs)

18

What is a Method (function)?

When a function is defined in, and associated with a particular class, it is called a Method.

19

When do you use dot syntax?

eg append is a method. A function that's part of the String class. So we call the append Method on an instance of the string class using dot syntax

20

What is a class?

A class is a unit we use to package together related data and functionality.

21

What is an instance of a class?

One of its moving parts. (eg think of Mouse Trap. One of the wheels is an instance of a class)

22

What makes up a class?

A class is made up of properties and methods
eg properties
let title: String
let releaseYear: Int
eg methods (every class needs an init method)
init()

23

Breakdown:
class Movie {
let title: String
let releaseYear: Int

init(title: String, releaseYear: Int) {
self.title = title
self.releaseYear = release year
}

class Movie {
let title: String
let releaseYear: Int

^*PROPERTIES
*INITALISE BELOW

init(title: String, releaseYear: Int) {
self.title = title
self.releaseYear = release year

^*USING self. BECAUSE THE PARAMETERS IN THE init() HAVE THE SAME NAMES AS THE PROPERTIES. USE self. TO REPRESENT THE OBJECT BEING INITIALISED.
}

24

What are Type Properties? (aka Class Properties)

There belong more to the class, and not objects within the class. They don't vary across classes, so their value stays the same.
ie it has the same value for every instance of a class
eg
static let permittedRatings = ["G", "PG"]
OR
class let permittedRatings = ["G", "PG"]

25

What is a Computed Property?

A property that's based on computed data in the class
eg the average of a range of scores

(Nb they have getters and setters)

26

What are Instance Methods?

Instance Methods are called on instances of the class

eg returning a list of movies that were produced in 1986

27

What are Class Methods?

These belong to the entire class

eg
isSourceTypeAvailable belongs to the UIImagePickerController class

(nb source type will be a photo album or camera)

28

What happens if you use 'class' instead of 'static' for Class Method?

eg
class func isSourceTypeAvaiable

If you use 'class' instead of 'static' subclasses will be able to override the method

29

What are enums? What are structs?

Enums define a set of related values. Structs are a convenient way to bundle properties together. They can have their own methods and conform to protocols

enum PrimaryColour {
case red
case green
case blue
}

or enum PrimaryColour {
case red, green blue
}

30

What is the difference between Structs/Enums and Classes?

Structs/Enums are value types, while Classes are reference types

31

Between Classes, Enums, and Structs, which have inheritance?

Only classes have inheritance (ie subclasses inherit from the parent class)

32

What are protocols and extensions?

Protocols and extensions are tools used to package models of functionality that expand on classes, enumerations and structs. Protocols can be shared across classes, and extensions are used to customise existing classes. The both allows you to reuse code and dry program (don't repeat yourself).

33

Describe protocols in more detail

A protocol is a list of related method signatures. Adopting a protocol is like signing a method to implement every method on the list (eg anything that conforms to Apple's built in comparable protocol can be compared). Protocols are an essential component of the delegate pattern, the patter by which one type calls upon another type to perform some operation.

34

What are extenstions?

Extensions offer a way to add computed properties and methods to an existing class, struct, or enum. They are often used to extend types for which you don't have access to the code (eg apple frameworks, or third party libraries)

35

What do closures include?

Closures include global functions, nested functions, and closure expressions

36

What is a closure expression?

A closure expression is an unnamed, self contained block of code that can be passed as an argument to a function. Closure expressions are used to specify an action to be executed at some time in the future.

37

Simplify a closure expression using shorthand arguments

var soups = ["tomato", "french onion"]
var sortedSoups = soups.sort({ (soup1: String, soup2: String) -> Bool in
return soup2 > soup1
})

var sortedSoups = soups.sort({
$1 > $0
})

The closure expression parameter and return types can be inferred...so no need for 'String' and 'Bool' and '->'. Since this is a single expression closure, it can be inferred that it returns the result of that single expression, so we can also leave out 'return'. We can use the shorthand argument names, $0 and $1 for soup1 and soup2, so no need for these.

38

Describe the ways of connecting the switches in ColorMaker

1. For Red switch: in the 'View Controller Scene' ctrl click. Drag 'Value Changed' to the yellow View Controller on Storyboard. Select the desired method (ie changeColorComponent). This connects the switch to the method. Then drag from 'New Referencing Outlet' to the yellow ViewController icon to connect the switch to the redControl @IBOutlet redControl swich

2. For Green switch; an easier method. Just drag the Green button to the method in ViewController.swift (ie the block of code). This will connect all the above.

39

What are UIControl classes?

UIControl are the classes that turn user input (eg a touch) into events using the target/action pattern.

40

Explain sliders and changing colours

Here's a trick that will come in handy. The sliders will give you a Float value between 0 and 1, but the UIColor object must be constructed with CGFloat values.

If you have a Float and you need a CGFloat you can use the CGFloat constructor to convert between them

let f: Float = 0.5
let cgf: CGFloat = CGFloat(f)