Expansion of swift2.0 learning and swift2.0 Learning
Expansion: similar to the expansion method function of oc, it adds new methods to existing classes, struct, enumeration, and protocol types.
Extended Syntax:
Declare with the extension Keyword:
extension SomeType {
// new functionality to add to SomeType goes here
}
Extended capabilities:
(1) Add computing attributes and computing attributes
(2) provide a new Constructor
(3) define instance methods and types
(4) define subscript
(5) Define and use nested types
(1) computing attributes
extension Double { var km: Double { return self*1_000 } var m: Double { return self } var cm: Double { return self/100 } var mm: Double { return self/1_000 } var ft: Double { return self/3.28084 } }
let oneInch = 25.4.km print("One inch is \(oneInch) meters") let threeFeet = 3.ft print("Three is \(threeFeet) meters")
(2) constructor
struct Rect { var origin = Point() var size = Size()}extension Rect { init(center: Point, size: Size) { let originX = center.x - (size.width / 2) let originY = center.y - (size.height / 2) self.init(origin: Point(x: originX, y: originY), size: size) }}<pre name="code" class="plain"> var someInt = 5 someInt.square() print(someInt)
// let defaultRect = Rect()// let memberwiseRect = Rect(origin: Point(x: 2.0, y: 2.0), size: Size(width: 3.0, height: 3.0)) let centerRect = Rect(center: Point(x: 4.0, y: 4.0), size: Size(width: 3.0, height: 3.0)) print("\(centerRect.origin),\(centerRect.size)")
(3) Method
A. Examples
extension Int { func repetitions(task: () -> Void) { for _ in 0..<self { task() } } }
3.repetitions({ print("hello!") })
B. Change the instance method
The extension-modified instance method can change the instance itself, just like the struct and enumeration. To change its attributes and methods, you must add the mutating keyword before the instance method, it seems that the method has changed from the original implementation.
extension Int { mutating func square() { self = self * self }}
var someInt = 5 someInt.square() print(someInt)
After the someInt. square () instance method is called, The someInt instance itself changes
(4) subscript
Expansion can add subscript to existing types. The following example uses an Int-type Chestnut to add an inline subscript to it. The value of this subscript starts from the right of the number.
For example, 12345 [0] returns 5
This algorithm is to take the number on a certain digit. If you take the single digit, % 10 is used directly. If you take the number on the ten digits, the number is first divided by 10 and % 10, the number on the hundred bits is first/100 and then % 10, and so on...
So the extended code is:
extension Int { subscript(var digitIndex: Int) -> Int { var decimalBase = 1 while digitIndex > 0 { decimalBase *= 10 --digitIndex } return (self / decimalBase) % 10 } }
You can try a few numbers at will:
231[0] 534[1] 14597346[3] print("\(231[0]), \(534[1]), \(14597346[3])",appendNewLine:false)
(5) nesting
We can expand nesting in existing classes, struct, and enumeration.
extension Int { enum Kind { case Negative, Zero, Positive } var kind: Kind { switch self{ case 0: return .Zero case let x where x > 0: return .Positive default: return .Negative } } }
func printIntegerKinds(numbers: [Int]) { for number in numbers { switch number.kind { case .Negative: print("- ", appendNewline: false) case .Zero: print("0 ", appendNewline: false) case .Positive: print("+ ", appendNewline: false) } } print("")}
printIntegerKinds([3, 19, -27, 0, -6, 0, 7])
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.