Skip to content

Instantly share code, notes, and snippets.

View jazzedge's full-sized avatar

Rocco Labellarte jazzedge

View GitHub Profile
See:https://makeapppie.com/2016/09/23/why-do-we-need-delegates-in-ios-and-watchos/
01. Model
Describes and acts upon or modifies data. There is no user I/O
02. View
Deals with all user interaction. It tells us about the state of the view and it can change the view's appearance and behaviour. It never interacts with the data.
See: https://makeapppie.com/2016/09/23/why-do-we-need-delegates-in-ios-and-watchos/
See: https://makeapppie.com/2016/06/27/using-segues-and-delegates-for-navigation-controllers-in-swift-3-0/
Delegation uses protocols. Protocols are a set of properties and methods that while declared in one place, another class implements. They allow for a layer of abstraction. A class adopts a protocol to do something. The protocol defines what the something is. The adopting class will have the code how it gets done.
See: https://learnappmaking.com/pass-data-view-controllers-swift-how-to/
See: https://stackoverflow.com/questions/35313747/passing-data-with-unwind-segue
When your app has multiple user interfaces, you’ll want to move data from one UI to the next. How do you pass data between view controllers in Swift?
Passing data between view controllers is an important part of iOS development. You can use several ways to do so, and all of them have advantages and drawbacks.
In this article, you’ll learn 6 different methods of passing data between view controllers, including working with properties, segues and NSNotificationCenter. You’ll start with the easiest approach, then move on to more complicated practices.
01. Passing Data Forward With Properties (A → B)
See: https://makeapppie.com/2016/06/27/using-segues-and-delegates-for-navigation-controllers-in-swift-3-0/
To move data from Foo One to Foo Two, we will override prepare(for segue:).
In your source VC add the following code:
// segue ViewController -> ViewControllerB
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
{
if segue.identifier == "viewNext" {
You can easily duplicate view controllers in Interface Builder by selecting them and pressing Cmd+D.
The guard statement means "if the condition(s) are NOT met then do this"
The else basically means DO THIS if the condition is NOT met
So if x is not > 0 then the code in { ... } IS executed.
guard x > 0 else {
// Value requirements not met, do something
return
}
See: Apple MPC video: https://www.youtube.com/watch?v=eNWOkm3e8qo
See: https://www.youtube.com/watch?v=WzVdIMq-jXY
Singletons
A singleton is a single instance of a class that is present at all times in memory. So why do we care about this? Well, let's say that you are building an app that connects to a database. You need a place to put all of your data service connections. This would be a perfect place to use singletons. Look at the code below; it will show you how to construct a singleton:
// Declaration
class DataService {
static var shared = DataService()
func createUser() {
}
}
01. Don't Force Unwrap Optionals
Optionals are a very powerful feature of Swift. They are just types like int and String, annotated by a question mark after the type declaration. If you want to declare a variable as an optional string, you would just write:
var someVariable: String?
This tells the compiler that there either can be a value or there might be no value at all. String? and String are considered to be two different types.
Think of optionals as a gift box. As I mentioned, this gift box may or may not have a value, and if you want to find out, you must unwrap the optional first. There are many ways of doing this:
See: http://www.techotopia.com/index.php/An_Introduction_to_CloudKit_Data_Storage_on_iOS_8
Within the scope of an application’s cloud container, each user has a unique, application specific iCloud user ID and a user info record where the user ID is used as the record ID for the user’s info record.
The record ID of the current user’s info record can be obtained via a call to the fetchUserRecordID(completionHandler:) method of the container instance. Once the record ID has been obtained, this can be used to fetch the user’s record from the cloud database:
container.fetchUserRecordID(completionHandler: {recordID,
error in
if let err = error {
// Failed to get record ID
} else {
// Success – fetch the user’s record here