Swift (general) Flashcards
What is a UIEelement?
It’s an object class in the view controller (eg a button)
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.
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.
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
What does AVFoundation framework do?
It deals with audio and video
eg AVAudio class
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()
What does NSSearchPathForDirectoriesInDomains
Creates a list of directory search paths
What is delegation?
When you get one object to do the job of anothers
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
What is the scene dock?
It’s the row of icons above the canvas (ie ViewController)
What does ctrl + dragging from ViewController (yellow circle) to the UIElement (eg button) do?
It connects an @IBOutlet
What does ctrl + dragging from a UIElement (eg button) to the ViewController (yellow circle)?
It connects an @IBAction
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]
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)
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.
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
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)
What is a Method (function)?
When a function is defined in, and associated with a particular class, it is called a Method.
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
What is a class?
A class is a unit we use to package together related data and functionality.
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)
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()
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.
}
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”]
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)
What are Instance Methods?
Instance Methods are called on instances of the class
eg returning a list of movies that were produced in 1986
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)
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
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
}
What is the difference between Structs/Enums and Classes?
Structs/Enums are value types, while Classes are reference types
Between Classes, Enums, and Structs, which have inheritance?
Only classes have inheritance (ie subclasses inherit from the parent class)
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).
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.
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)
What do closures include?
Closures include global functions, nested functions, and closure expressions
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.
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.
Describe the ways of connecting the switches in ColorMaker
- 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
- 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.
What are UIControl classes?
UIControl are the classes that turn user input (eg a touch) into events using the target/action pattern.
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)