本文是学习《The Swift Programming Language》整理的相关随笔,基本的语法不作介绍,主要介绍Swift中的一些特性或者与OC差异点。
系列文章:
- Swift4 基础部分:The Basics
- Swift4 基础部分:Basic Operators
- Swift4 基础部分:Strings and Characters
- Swift4 基础部分:Collection Types
- Swift4 基础部分:Control Flow
- Swift4 基础部分:Functions
- Swift4 基础部分:Closures
- Swift4 基础部分: Enumerations
- Swift4 基础部分: Classes and Structures
- Swift4 基础部分: Properties
实例方法(Instance Methods)
实例方法我们直接看例子:
class Counter {
var count = 0;
func increment(){
count += 1;
}
func increment(by amount: Int){
count += amount;
}
func reset(){
count = 0;
}
}
let counter = Counter();
counter.increment();
print("count:\(counter.count)");
counter.increment(by: 2);
print("count:\(counter.count)");
counter.reset();
print("count:\(counter.count)");
执行结果:
count:1
count:3
count:0
self属性(The self Property)
Every instance of a type has an implicit property called self, which is
exactly equivalent to the instance itself. You use the self property to refer to the current instance within its own instance methods.
- 每一个实例都有一个隐含属性叫做self,self完全等同于该实例本身。你可以在一个实例的实例方法中使用这个隐含的self属性来引用当前实例。
针对上述例子做改造,得到的结果完全一致。
class Counter {
var count = 0;
func increment(){
self.count += 1;
}
func increment(by amount: Int){
self.count += amount;
}
func reset(){
self.count = 0;
}
}
let counter = Counter();
counter.increment();
print("count:\(counter.count)");
counter.increment(by: 2);
print("count:\(counter.count)");
counter.reset();
print("count:\(counter.count)");
函数中修改值类型数据(Modifying Value Types from Within Instance Methods)
Structures and enumerations are value types. By default, the properties of
a value type cannot be modified from within its instance methods.
- 结构体与枚举都是值类型的数据,他们内部的属性值不能通过实例方法更改。
However, if you need to modify the properties of your structure or
enumeration within a particular method, you can opt in to mutating
behavior for that method. The method can then mutate (that is, change) its
properties from within the method, and any changes that it makes are
written back to the original structure when the method ends. The method
can also assign a completely new instance to its implicit self property,
and this new instance will replace the existing one when the method ends.
- 如果你确实需要在某个特定方法中修改结构体或者枚举的属性,你可以选择变异(mutating)这个方法,可以通过该方法就可以从方法内部改变它的属性;并且它做的任何改变在方法结束时还会保留在原始结构中。方法还可以给它隐含的self属性赋值一个全新的实例,这个新实例在方法结束后将替换原来的实例。
例子:
struct Point{
var x = 0.0,y = 0.0
mutating func moveBy(x deltaX:Double,y deltaY:Double){
x += deltaX;
y += deltaY;
}
}
var somePoint = Point();
somePoint.moveBy(x: 1.0, y: 1.0);
print("The point is now at (\(somePoint.x), \(somePoint.y))");
执行结果:
The point is now at (1.0, 1.0)
在变异方法中给self赋值(Assigning to self Within a Mutating Method)
Mutating methods can assign an entirely new instance to the implicit self property.
- 变异方法能够赋给隐含属性self一个全新的实例。
例子:
struct Point{
var x = 0.0,y = 0.0
mutating func moveBy(x deltaX:Double,y deltaY:Double){
self = Point(x:x + deltaX,y:y + deltaY);
}
}
var somePoint = Point();
somePoint.moveBy(x: 1.0, y: 1.0);
print("The point is now at (\(somePoint.x), \(somePoint.y))");
执行结果:
The point is now at (1.0, 1.0)
Mutating methods for enumerations can set the implicit self parameter to be a different case from the same enumeration:
- 枚举的变异方法可以把self设置为相同的枚举类型中不同的成员.
例子:
enum TriDateSwitch{
case off, low, high
mutating func next() {
switch self {
case .off:
self = .low;
case .low:
self = .high;
case .high:
self = .off;
}
}
}
var ovenLight = TriDateSwitch.low;
print("ovenLight:\(ovenLight)");
ovenLight.next();
print("ovenLight:\(ovenLight)");
ovenLight.next();
print("ovenLight:\(ovenLight)");
执行结果:
ovenLight:low
ovenLight:high
ovenLight:off
类方法(Type Methods)
You can also define methods that are called on the type itself. These
kinds of methods are called type methods. You indicate type methods by
writing the static keyword before the method’s func keyword. Classes may
also use the class keyword to allow subclasses to override the
superclass’s implementation of that method.
- 你也可以定义类型本身调用的方法,这种方法就叫做类型方法。声明类的类型方法,在方法的func关键字之前加上关键字class;声明结构体和枚举的类型方法,在方法的func关键字之前加上关键字static。
自己写一个计算器的例子:
class Calculator {
class func add(_ firstNum:Double,_ secondNum:Double) -> Double{
return firstNum + secondNum;
}
class func minus(_ firstNum:Double,_ secondNum:Double) -> Double{
return firstNum - secondNum;
}
class func multiplied(by firstNum:Double,_ secondNum:Double) -> Double{
return firstNum * secondNum;
}
class func divided(by firstNum:Double,_ secondNum:Double) -> Double{
return firstNum / secondNum;
}
}
print("Add : \(Calculator.add(1.0,2.0))");
print("Minus : \(Calculator.minus(1.0,2.0))");
print("Multiplied : \(Calculator.multiplied(by:1.0,2.0))");
print("Divided : \(Calculator.divided(by:1.0,2.0))");
执行结果:
Add : 3.0
Minus : -1.0
Multiplied : 2.0
Divided : 0.5
上述的类改为结构体实现:
struct Calculator{
static func add(_ firstNum:Double,_ secondNum:Double) -> Double{
return firstNum + secondNum;
}
static func minus(_ firstNum:Double,_ secondNum:Double) -> Double{
return firstNum - secondNum;
}
static func multiplied(by firstNum:Double,_ secondNum:Double) -> Double{
return firstNum * secondNum;
}
static func divided(by firstNum:Double,_ secondNum:Double) -> Double{
return firstNum / secondNum;
}
}
print("Add : \(Calculator.add(1.0,2.0))");
print("Minus : \(Calculator.minus(1.0,2.0))");
print("Multiplied : \(Calculator.multiplied(by:1.0,2.0))");
print("Divided : \(Calculator.divided(by:1.0,2.0))");
执行结果:
Add : 3.0
Minus : -1.0
Multiplied : 2.0
Divided : 0.5