写在前面
用Swift来做项目也有一段时间了,与Objective-c 相比,直观的感觉是用Swift写代码更快,更流畅。但是不包括写要和c连接的代码,如果要写和c相关的,还是用OC比较好,所以现在的代码基本上是用OC来写一层c的转换层。
下面是Swift和OC的知识点对比,算是对这一段时间用到的知识的梳理。
Swift Extensions vs OC categories
Swift 中的扩展可以:
- 添加计算型属性和计算型静态属性 (注意: 只能是计算属性的变量, 普通变 量不能添加)
- 定义实例方法和类型方法
- 提供新的构造器
- 定义下标
- 定义和使用新的嵌套类型
- 使一个已有类型符合某个协议
- 不能重载以前类的方法
extension Int {
mutating func square() {
self = self * self
}
}
用mutating关键字可以使函数可以改变自己/自己的变量。在extension和struct里面都需要用这个关键字。
在Objective-C中,在头文件里面是这么写的, 然后在.m里面实现:
@interface NSString (ReverseStringExtension)
- (NSString *)reverseString; // implementent in .m file
@end
@implementation NSString (ReverseStringExtension)
- (NSString *)reverseString {
}
@end
OC 的categories (分类)的能力:
- 不能添加自身的任何变量
- 可以重载类里面的方法,但是不建议, 如果重载了,分类里面的方法,有更高的优先级;
- 分类里面的方法可以没有实现
- 使用分类添加的新方法会影响这个类的所有子类
然后OC 2.0 添加了一个简化的category, 称为Extension:
- 当你有源代码的时候,用extension来私有化函数和变量;
- 声明了的函数必须实现;
@interface XYZPerson ()
@property (readwrite) NSString *uniqueIdentifier;
@end
@implementation XYZPerson
...
@end
@interface XYZPerson () {
id _someCustomInstanceVariable;
}
...
@end
总结
Swift 的Extension和OC的category非常相似,区别在于Swift 的Extension没有名字, 不能重载以前类的方法。
函数的变量参数的修改
Swift
var
函数参数默认是常量。试图在函数体中更改参数值将会导致编译错误。这意味着你不能错误地更改参数值。但是有的时候,就是想改,那么用var关键字,但是对变量参数所进行的修改在函数调用结束后便消失了,并且对于函数体外是不可见的。变量参数仅仅存在于函数调用的生命周期中。
func alignRight(var string: String, totalLength: Int, pad: Character) -> String {
...
return string
}
let originalString = "hello"
let paddedString = alignRight(originalString, totalLength: 10, pad: "-")
Inout
如果你想要一个函数可以修改参数的值,并且想要在这些修改在函数调用结束后仍然存在,那么就应该把这个参数定义为输入输出参数(In-Out Parameters)。输入输出参数不能有默认值.
实现:
func swapTwoInts(inout a: Int, inout _ b: Int) {
let temporaryA = a
a = b
b = temporaryA
}
调用
swapTwoInts(&someInt, &anotherInt)
OC
而OC是用指针来做实现的,想怎么改就可以怎么改。
总结
Swift在这一方面比OC要严格许多。
也可以看到Swift淡化了指针的概念,这个嘛,是初学者的福利,是要和c代码打交道的程序员的悲哀,后面会写一章,Swift调用c的,真是一边码代码,一边听苹果在呐喊,不要用指针呀,用了要小心呀。
Swift其他关于函数的独有的知识点:
多重返回值函数
你可以用元组(tuple)类型让多个值作为一个复合值从函数中返回.
func minMax(array: [Int]) -> (min: Int, max: Int) {
...
return (currentMin, currentMax)
}
let bounds = minMax([8, -6, 2, 109, 3, 71])
print("min is \(bounds.min) and max is \(bounds.max)")
函数参数名称
函数参数都有一个外部参数名(external parameter name)和一个本地参数名(local parameter name).外部参数名用来标记传递给函数调用的参数,本地参数名在实现函数的时候使用. 但是一般使用其本地参数名作为自己的外部参数名。
调用的时候,第一个参数可以省略。
func someFunction(firstParameterName: Int, secondParameterName: Int) {
// function body goes here
// firstParameterName and secondParameterName refer to
// the argument values for the first and second parameters
}
someFunction(1, secondParameterName: 2)
当然也可以都写:
func someFunction(externalParameterName localParameterName: Int) {
// function body goes here, and can use localParameterName
// to refer to the argument value for that parameter
}
你也可以让调用者忽略参数名, 用 _
func someFunction(firstParameterName: Int, _ secondParameterName: Int) {
// function body goes here
// firstParameterName and secondParameterName refer to
// the argument values for the first and second parameters
}
someFunction(1, 2)
函数的参数也可以是可变
func arithmeticMean(numbers: Double...) -> Double {
var total: Double = 0
for number in numbers {
total += number
}
return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8.25, 18.75)
// returns 10.0, which is the arithmetic mean of these three numbers
Override
Swift 重载方法需要在函数前面写override关键字,而OC是不需要的。