以下内容为swift4.0中的特性
Struct
- 值类型(内存分配在栈上,赋值或者函数的参数传递时通过拷贝来实现)
- Copy-on-write: 通过写时复制的方式提升内存的使用效率。在值需要改变的时候才执行拷贝操作。
- Mutating: 使用mutating来标识需要使用copy-on-write特性
- 无法继承
- 不可修改的struct使用let来声明
- 支持函数式编程: swift函数式编程
Protocol
是一组方法的集合,不能存储数据。可以protocol实现swift中的多重继承
protocol优势
1.使API更加灵活
2.通过delegate在View和Controller之间实现关联和结构化通信
3.强制行为,比如Dictionary的key必须遵循hashable协议
4.不同类实现相同的功能,比如String, Array可以执行集合类型的方法。
5.实现多重继承
包含可选的方法protocol
- 通常情况下,protocol中定义的方法都是需要实现的。
- 如果有可以选择实现的方法,需要在方法前使用optional来声明,有可选方法的protocol需要使用@objc来标明。
- 如果一个类实现带有可选方法的protocol,这个类一定是NSObject的子类
定义protocol
protocol SomeProtocol : InheritedProtocol1, InheritedProtocol2 {
var someProperty: Int { get set }
func aMethod(arg1: Double, anotherArgument: String) -> SomeType
mutating func changeIt()
init(arg: Type)
}
- 任何实现了SomeProtocol的数据结构必须要实现InheritedProtocol1和InheritedProtocol2
- 如果protocol中定义了property,需要指明get和set方法实现哪个或者都需要实现
- 对于会修改receiver的方法,需要在方法前使用mutating关键字来修饰
- 如果限制某些protocol只能由class来实现,需要在protocol定义时添加class作为父类
protocol SomeProtocol : class, InheritedProtocol1, InheritedProtocol2 {
var someProperty: Int { get set }
func aMethod(arg1: Double, anotherArgument: String) -> SomeType
mutating func changeIt()
init(arg: Type)
}
- protocol中也可以指明需要实现初始化方法
实现protocol
- class, enum, struct都可以实现protocol
class SomeClass : SuperclassOfSomeClass, SomeProtocol, AnotherProtocol {
// implementation of SomeClass here
// which must include all the properties and methods in SomeProtocol & AnotherProtocol
}
enum SomeEnum : SomeProtocol, AnotherProtocol {
// implementation of SomeEnum here
// which must include all the properties and methods in SomeProtocol & AnotherProtocol
}
struct SomeStruct : SomeProtocol, AnotherProtocol {
// implementation of SomeStruct here
// which must include all the properties and methods in SomeProtocol & AnotherProtocol
}
- class实现的protocol中包含init方法时,需要在init方法前添加required关键字,用于防止该类的子类在继承时忘记实现该方法。
class SomeClass : SuperclassOfSomeClass, SomeProtocol, AnotherProtocol {
// implementation of SomeClass here, including ...
required init(...)
}
- class,struct和enum可以通过extension来实现protocol
extension Something : SomeProtocol {
// implementation of SomeProtocol here
// no stored properties though
}
String
字符的集合。和Objc的NSStrinig不同,使用String.Index表示字符在字符串中的索引。
String.Index使用举例如下
let pizzaJoint = “café pesto”
let firstCharacterIndex = pizzaJoint.startIndex // of type String.Index
let fourthCharacterIndex = pizzaJoint.index(firstCharacterIndex, offsetBy: 3) let fourthCharacter = pizzaJoint[fourthCharacterIndex] // é
if let firstSpace = pizzaJoint.index(of: “ “) { // returns nil if “ ” not found let
secondWordIndex = pizzaJoint.index(firstSpace, offsetBy: 1)
let secondWord = pizzaJoint[secondWordIndex..<pizzaJoint.endIndex]
}
NSAttributedString
NSAttibutedString的创建和使用举例
let attributes: [NSAttributedStringKey : Any] = [ // note: type cannot be inferred here
.strokeColor : UIColor.orange,
.strokeWidth : 5.0 // negative number here would mean fill (positive means outline)
]
let attribtext = NSAttributedString(string: “Flips: 0”, attributes: attributes)
flipCountLabel.attributedText = attribtext // UIButton has attributedTitle
函数类型
函数类型和其他数据类型一样,用于定义变量。
函数声明
func 函数名(行参列表) -> 返回值 {代码实现}
闭包
闭包通常作为函数的参数
闭包使用举例
let primes = [2.0, 3.0, 5.0, 7.0, 11.0]
let negativePrimes = primes.map({ -$0 }) // [-2.0, -3.0, -5.0, -7.0, -11.0]
let invertedPrimes = primes.map() { 1.0/$0 } // [0.5, 0.333, 0.2, etc.]
let primeStrings = primes.map { String($0) } // [“2.0”,”3.0”,”5.0”,”7.0”,”11.0”]
如果函数最后一个参数是闭包,可以将{}部分放到()的外面,如上面的语句3;如果函数只有一个闭包参数,可以将()省略,如上面的语句4。
闭包实现property的延时加载
var someProperty: Type = {
// construct the value of someProperty here
return <the constructed value>
}()