Tips, tricks & notes about Apple's Swift;
- Functions - Use func to declare a function. Call a function by following its name with a list of arguments in parentheses. Use -> to separate the parameter names and types from the function’s return type.
- Functions can also take a variable number of arguments, collecting them into an array - func sumOf(numbers: Int...) -> Int {
- Functions can be nested. Nested functions have access to variables that were declared in the outer function. You can use nested functions to organize the code in a function that is long or complex.
- Functions are a first-class type. This means that a function can return another function as its value.
- Functions are actually a special case of closures: blocks of code that can be called later. The code in a closure has access to things like variables and functions that were available in the scope where the closure was created, even if the closure is in a different scope when it is executed
- Closures are self-contained blocks of functionality that can be passed around and used in your code.
- You can write a closure without a name by surrounding code with braces ({}). Use in to separate the arguments and return type from the body.
- Single statement closures implicitly return the value of their only statement - let mappedNumbers = numbers.map({ number in 3 * number })
- A closure passed as the last argument to a function can appear immediately after the parentheses. When a closure is the only argument to a function, you can omit the parentheses entirely - let sortedNumbers = numbers.sort { $0 > $1 }
- Use class followed by the class’s name to create a class. A property declaration in a class is written the same way as a constant or variable declaration, except that it is in the context of a class. Likewise, method and function declarations are written the same way.
- Use an initializer to set up the class when an instance is created. Use init to create one.
- init(name: String)self.name = name}
- Notice how self is used to distinguish the name property from the name argument to the initializer.
- Completion Handlers, used for closures in many functions (see AlertViews) - good post This is the BEST tutorial I found, it hs all the flavors - http://sourcefreeze.com/uialertcontroller-ios-8-using-swift/
- Alert screens, used for popups & User instruction - good tutorial
- You use this representation to create an adaptive interface, which is an interface that automatically adjusts so that it looks good in the context of the current device and orientation.
- var window: UIWindow? - the question mark means it's optional ( ie it may not exist ie (may be nil))
- variables are mutable -
var myVariable = 42
- constants are immutable -
let myConstant = 42
AppDelegate
class is where you write your custom app-level code.
Singleton - Swift - ObjectiveC migration
Singletons provide a globally accessible, shared instance of an object. You can create your own singletons as a way to provide a unified access point to a resource or service that’s shared across an app, such as an audio channel to play sound effects or a network manager to make HTTP requests.
Importing Objective-C into Swift - To import a set of Objective-C files in the same app target as your Swift code, you rely on an Objective-C bridging header to expose those files to Swift. Xcode offers to create this header file when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.
Migration - Migration methodology
Provides an opportunity to revisit an existing Objective-C app and improve its architecture, logic, and performance by replacing pieces of it in Swift. For a straightforward, incremental migration of an app, you’ll be using the tools learned earlier—mix and match plus interoperability. Mix-and-match functionality makes it easy to choose which features and functionality to implement in Swift, and which to leave in Objective-C. Interoperability makes it possible to integrate those features back into Objective-C code with no hassle. Use these tools to explore Swift’s extensive functionality and integrate it back into your existing Objective-C app without having to rewrite the entire app in Swift at once
NSUserDefaults - Migration methodology
class NSUserDefaults : NSObject
Description
The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved. Applications record such preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults since they’re commonly used to determine an application’s default state at startup or the way it acts by default.