Swift Study Notes
Variables and Operators
In Swift, a variable is a constant value that can be modified. The let keyword is used to declare a constant variable, which cannot be modified. The var keyword is used to declare a variable that can be modified.
let varA: Int = 12
let varB = 12
let varC = 12
let varD: Float = 12
print(varA)
print(varB)
print(varC)
print(varD)
A tuple is a collection of values that can be used to store multiple values in a single variable. In Swift, a tuple is declared using the ( ) syntax.
var abc = (10, 12, 30, "abc")
var score = (id: "1001", name: "Joe Smith", english_score: 30, chinese_score: 90)
score.english_score = 100
Operators
Swift has several operators that can be used to perform various operations. The following are some of the most commonly used operators:
Reference operator: Used to invoke a property or method of an instance.Question mark operator: Used to declare an optional type.Exclamation point operator: Used to force an optional value to be unwrapped.Arrow operator: Used to describe a function or method return type.Colon operator: Used to separate a set of dictionary key-value pairs.Range operator: Used to specify a range of values.
let short: Int16 = 277
let sshort: Int8 = 100
// Int8 (short) + sshort
short + Int16(sshort)
Attributes
In Swift, an attribute is a property or method that can be used to store data or perform operations. There are several types of attributes, including:
Property type: Used to store data in a constant or variable property.Storage attributes: Used to store data in a constant or variable property.Calculated attribute: Used to calculate a value based on other attributes.
var strLin = "loyinglin" // set the initial value
var str:? String // declared optional
Access Control
In Swift, access control is used to specify the level of access that a variable or attribute has. There are three levels of access control:
Public: Can be accessed by any code in the same module or other modules.Internal: Can only be accessed by code in the same module.Private: Can only be accessed by code in the same source file.
public class Person {
private var name: String = "loying"
internal var number: Int = 2010551111
}
Class Structure
In Swift, a class is a reference type that can be used to create objects. There are several types of classes, including:
Value type: A value type is a class that has no unique identifier.Reference type: A reference type is a class that has a unique identifier.
class Person {
var name: String = "loying"
var number: Int = 2010551110
}
Optional Chaining
In Swift, optional chaining is a feature that allows you to call a method or access a property of an object even if the object is nil.
var nilInt:? String = "abc"
let textNil = nilInt
if let test = nilInt {
print("OK \(test) \(textNil) \(textNil!)")
}
Function
In Swift, a function is a block of code that can be executed when a certain condition is met. There are several types of functions, including:
Convenience constructor: A convenience constructor is a function that can be used to create an instance of a class.Specify constructor: A specify constructor is a function that can be used to create an instance of a class.
func plus (width: Int, height: Int) -> Int {
return width + height
}
Inheritance
In Swift, inheritance is a feature that allows a class to inherit the properties and methods of another class.
class Person {
var name: String = "loying"
var number: Int = 2010551110
}
Error Handling
In Swift, error handling is a feature that allows you to handle errors that occur when a certain condition is met.
do {
let path: String = NSBundle.mainBundle().pathForResource("text", ofType: "txt")!
let str = try NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding)
print(str)
} catch let err as NSError {
err.description
}
Capture List
In Swift, a capture list is a feature that allows you to capture the value of a variable in a closure.
var array: [AnyObject] = []
array.append(1)
array.append("miao")
Closure
In Swift, a closure is a feature that allows you to capture the value of a variable in a block of code.
func plusAfterMul(plus: Int, factor1: Int, factor2: Int, funMul: (Int, Int) -> Int) -> Int {
return plus + funMul(factor1, factor2)
}
Any, AnyObject
In Swift, Any and AnyObject are protocols that can be used to represent any instance of a class.
let swiftInt: Int = 1
let swiftString: String = "miao"
var array: [AnyObject] = []
array.append(swiftInt)
array.append(swiftString)
Lazy Initialization
In Swift, lazy initialization is a feature that allows you to delay the initialization of a variable until it is actually needed.
class Person {
lazy var name: String = "loying"
var number: Int = 2010551110
}
Generic Function Application
In Swift, generic function application is a feature that allows you to apply a generic function to a specific type.
func plusMore(width: (Length: Int, Size: Int), _ mul: Int) -> (Length: Int, Size: Int) {
let ret = (Length: width.Length * mul, Size: width.Size * mul)
return ret
}
Protocol
In Swift, a protocol is a feature that allows you to define a set of methods and properties that a class must implement.
protocol Person {
var name: String { get }
var number: Int { get }
}
Association Type
In Swift, an association type is a feature that allows you to define a set of methods and properties that a class must implement.
class Person {
var name: String = "loying"
var number: Int = 2010551110
}
Operator Overloading
In Swift, operator overloading is a feature that allows you to define custom operators for a class.
class Person {
var name: String = "loying"
var number: Int = 2010551110
}