大家对“实例方法和类型方法”的概念应该不陌生了,在objective-c中很常见。例如:
1. 实例方法(减号开头)
- (instancetype)init;调用的时候,必须先进行实例化一个对象(alloc), 然后调用init方法。
2. 类型方法(加号开头)
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations调用的时候,直接使用UIView的类型就可以了。
而在Swift中写法有所改变,它使用class和static关键字来标示。而且不仅仅是作用在类中,枚举和结构体(enum, struct)中也有相应的方法。
在class类型上下文中,我们使用class;其他的使用static。
一、作用于结构(注:代码中,有关存储属性及计算属性的知识,我会在后续章节进行详细的介绍。)
struct Point {
let x: Double
let y: Double
// 存储属性
static let zero = Point(x: 0, y: 0)
// 计算属性
static var ones: [Point] {
return [
Point(x: 1, y: 1),
Point(x: -1, y: 1),
Point(x: 1, y: -1),
Point(x: -1, y: -1)
]
}
// 类型方法
static func add(p1: Point, p2: Point) -> Point {
return Point(x: p1.x + p2.x, y: p1.y + p2.y)
}
}
let p = Point(x: 3, y: 4)
Point.zero
Point.ones
Point.add(Point(x: 1, y: 2), p2: Point(x: 2, y: 1))
二、作用于类
class Square {
// 类型属性,用class关键字
class var PI: Double{
return 3.14
}
}class Square {
class var PI: Double{
get { return 3.14 }
}
}
但需要注意的是,存储属性不能使用class进行修饰。
class Student {
class var phone: String?
}而用static就可以了。
class Student {
static var phone: String?
}protocol MyProtocol {
static func foo() -> String
}
struct MyStruct: MyProtocol {
static func foo() -> String {
return "MyStruct"
}
}
class MyClass {
class func foo() -> String {
return "MyClass"
}
}在protocol中定义的方法或者计算属性,类型修饰符是static;在实现的结构或者枚举中,修饰符使用static,在实现的class中,修饰符使用class。
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/sinat_27706697/article/details/47282603