Swift 5.1的更新比较迟,单独成篇Swift 5.1的变化。
Swift 5.0 最重要的自然是ABI Stability, 对此可以看这篇 Swift ABI 稳定对我们到底意味着什么 。
当然还有其他的更新。
Result
类型SE-0235提议的实现。用来在复杂对象中的错误处理。
Result
类型有两个带泛型的枚举成员success
和failure
,而且failure
的泛型必须遵循Swift的Error
类型。
常规的使用
enum NetworkError: Error {
case badURL
}
import Foundation
func fetchUnreadCount1(from urlString: String, completionHandler: @escaping (Result<Int, NetworkError>) -> Void) {
guard let url = URL(string: urlString) else {
completionHandler(.failure(.badURL))
return
}
// complicated networking code here
print("Fetching \(url.absoluteString)...")
completionHandler(.success(5))
}
fetchUnreadCount1(from: "https://www.hackingwithswift.com") { result in
switch result {
case .success(let count):
print("\(count) unread messages.")
case .failure(let error):
print(error.localizedDescription)
}
}
首先,Result
有个get()
方法,要么返回成功值,要么抛出错误。那么可以这么使用。
fetchUnreadCount1(from: "https://www.hackingwithswift.com") { result in
if let count = try? result.get() {
print("\(count) unread messages.")
}
}
再次,Result
可以接受一个闭包来初始化,如果闭包成功返回,就会把它放到success
的一边,如果抛出错误,就放到failure
的一边。
let result = Result { try String(contentsOfFile: someFile) }
最后,你可以使用你自己的错误枚举,但是Swift官方建议,你说用Swift.Error
来作为Error
的参数。
“it’s expected that most uses of Result will use Swift.Error as the Error type argument.”
SE-0200 引入了,使用#
来包裹的Raw字符串,里面的字符不会做处理,特别是一些转义字符。
差值需要这样做
let answer = 42
let dontpanic = #"The answer to life, the universe, and everything is \#(answer)."#
这个对于正则的特别好用
let regex1 = "\\\\[A-Z]+[A-Za-z]+\\.[a-z]+"
let regex2 = #"\\[A-Z]+[A-Za-z]+\.[a-z]+"#
SE-0228提案改进了Swift的字符串插值,让其更高效和自由。
struct User {
var name: String
var age: Int
}
extension String.StringInterpolation {
mutating func appendInterpolation(_ value: User) {
appendInterpolation("My name is \(value.name) and I'm \(value.age)")
}
}
let user = User(name: "Guybrush Threepwood", age: 33)
print("User details: \(user)")
// User details: My name is Guybrush Threepwood and I'm 33,
// TODO: 更多使用,需要多研究
SE-0216 增加了@dynamicCallable
属性,来支持方法的动态调用,类似@dynamicMemberLookup
。
你可以将
struct RandomNumberGenerator {
func generate(numberOfZeroes: Int) -> Double {
let maximum = pow(10, Double(numberOfZeroes))
return Double.random(in: 0...maximum)
}
}
转变为
@dynamicCallable
struct RandomNumberGenerator {
func dynamicallyCall(withKeywordArguments args: KeyValuePairs<String, Int>) -> Double {
let numberOfZeroes = Double(args.first?.value ?? 0)
let maximum = pow(10, numberOfZeroes)
return Double.random(in: 0...maximum)
}
}
let random = RandomNumberGenerator()
let result = random(numberOfZeroes: 0)
@dynamicCallable
参数 withArguments
,你可以使用任何遵循ExpressibleByArrayLiteral
的类型,例如 数组,数组切片,set等withKeywordArguments
,使用任何遵循ExpressibleByDictionaryLiteral
的类型,例如,字典,和key value 对,更多KeyValuePairs
可以的看这里,什么是KeyValuePairs?withKeywordArguments
而不是withArguments
,你仍然按照无参数标签的方式使用,只是key是空字符串。withKeywordArguments
或者withArguments
标记为抛出错误,调用类型也会抛出错误。@dynamicCallable
SE_0192的实现。
有时候枚举的switch中使用default
来防治出错,但不会真正的使用,但是如果未来加了新的case
,那些处理地方就会遗漏。现在可以添加@unknkow
来出触发Xcode的提示。
func showNew(error: PasswordError) {
switch error {
case .short:
print("Your password was too short.")
case .obvious:
print("Your password was too obvious.")
@unknown default:
print("Your password wasn't suitable.")
}
}
这样,如果如果代码中,没有处理干净PasswordError (switch
block is no longer exhaustive),就会告警.
try?
抹平嵌套可选struct User {
var id: Int
init?(id: Int) {
if id < 1 {
return nil
}
self.id = id
}
func getMessages() throws -> String {
// complicated code here
return "No messages"
}
}
let user = User(id: 1)
let messages = try? user?.getMessages()
上面的例子中,Swift 4.2以及之前的,message
会是 String??
, 这样就不太合理,Swift 5中,就能返回抹平的String?
SE-0225添加了, isMultiple(of:)
来检查整数是否为偶数, 和if rowNumber % 2 == 0
效果一样。
let rowNumber = 4
if rowNumber.isMultiple(of: 2) {
print("Even")
} else {
print("Odd")
}
compactMapValues()
方法SE-0218,为字典添加了compactMapValues()
方法,这个就像结合了,数组compactMap()
方法(遍历成员,判断可选的值,然后丢弃nil成员)和字典的mapValues()
方法(只转换字典的value)。
let times = [
"Hudson": "38",
"Clarke": "42",
"Robinson": "35",
"Hartis": "DNF"
]
let finishers1 = times.compactMapValues { Int($0) }
let finishers2 = times.compactMapValues(Int.init)
let people6 = [
"Paul": 38,
"Sophie": 8,
"Charlotte": 5,
"William": nil
]
let knownAges = people6.compactMapValues { $0 }
print("compactMapValues, \(finishers1), \(finishers2),\(knownAges)")
// compactMapValues, ["Clarke": 42, "Robinson": 35, "Hudson": 38], ["Robinson": 35, "Clarke": 42, "Hudson": 38],["Charlotte": 5, "Sophie": 8, "Paul": 38]
SE-0220, 引入了count(where:)
函数,来计算遵循Sequence
列表中满足条件成员的个数。
let scores = [100, 80, 85]
let passCount = scores.count { $0 >= 85 }
let pythons = ["Eric Idle", "Graham Chapman", "John Cleese", "Michael Palin", "Terry Gilliam", "Terry Jones"]
let terryCount = pythons.count { $0.hasPrefix("Terry") }
这个功能因为性能问题,被撤回了。
CaseIterable
协议SE-0194提议的实现,Swift4.2 增加了CaseIterable
协议,能够给枚举的allCases
属性自动产生所有的枚举的数组。
enum Pasta: CaseIterable {
case cannelloni, fusilli, linguine, tagliatelle
}
for shape in Pasta.allCases {
print("I like eating \(shape).")
}
当然还可以自行实现
enum Car: CaseIterable {
static var allCases: [Car] {
return [.ford, .toyota, .jaguar, .bmw, .porsche(convertible: false), .porsche(convertible: true)]
}
case ford, toyota, jaguar, bmw
case porsche(convertible: Bool)
}
SE-0196提议的实现。Swift 4.2提供这两个提示,来让Xcode在编译时候作出提示
#warning
,警告,主要为了提示后续需要处理,Xcode可以编译通过#error
, 常用在Library中,强制提示,需要修复,否则不会编译通过。func encrypt(_ string: String, with password: String) -> String {
#warning("This is terrible method of encryption")
return password + String(string.reversed()) + password
}
struct Configuration {
var apiKey: String {
#error("Please enter your API key below then delete this line.")
return "Enter your key here"
}
}
还可以和#if
配合使用。
#if os(macOS)
#error("MyLibrary is not supported on macOS.")
#endif
SE-0195提议的实现。Swift 4.2提供了@dynamicMemberLookup
的属性,和subscript(dynamicMember:)
陪着使用,实现动态的属性的取值。
@dynamicMemberLookup
struct Person5 {
subscript(dynamicMember member: String) -> String {
let properties = ["name": "Tylor Swift", "city" : "Nashville"]
return properties[member, default: ""]
}
}
let person5 = Person5()
print("person5.name: \(person5.name)")
print("person5.city: \(person5.city)")
print("person5.favoriteIceCream: \(person5.favoriteIceCream)")
// person5.name: Tylor Swift
// person5.city: Nashville
// person5.favoriteIceCream:
当然也有类似多态的用法。
@dynamicMemberLookup
struct Person5 {
subscript(dynamicMember member: String) -> String {
let properties = ["name": "Tylor Swift", "city" : "Nashville"]
return properties[member, default: ""]
}
subscript(dynamicMember member: String) -> Int {
let properties = ["age": 26, "height": 178]
return properties[member, default: 0]
}
}
let person5 = Person5()
print("person5.age: \(person5.age)")
let age: Int = person5.age
print("person5.age2: \(age)")
// person5.age:
// person5.age2: 26
注意你需要指定明确指定类型,Swift才能正确使用。
而且如果已经有存在属性,动态属性将不会生效
struct Singer {
public var name = "Justin Bieber"
subscript(dynamicMember member: String) -> String {
return "Taylor Swift"
}
}
let singer = Singer()
print(singer.name)
// Justin Bieber
@dynamicMemberLookup
可以用在协议,结构体,枚举,类,甚至标注为@objc
的类,以及它们的继承者。
例如,陪着协议的使用,你可以这样用
@dynamicMemberLookup
protocol Subscripting { }
extension Subscripting {
subscript(dynamicMember member: String) -> String {
return "This is coming from the subscript"
}
}
extension String: Subscripting { }
let str = "Hello, Swift"
print(str.username)
Chris Lattner提议中的例子很有意义,
@dynamicMemberLookup
enum JSON {
case intValue(Int)
case stringValue(String)
case arrayValue(Array<JSON>)
case dictionaryValue(Dictionary<String, JSON>)
var stringValue: String? {
if case .stringValue(let str) = self {
return str
}
return nil
}
subscript(index: Int) -> JSON? {
if case .arrayValue(let arr) = self {
return index < arr.count ? arr[index] : nil
}
return nil
}
subscript(key: String) -> JSON? {
if case .dictionaryValue(let dict) = self {
return dict[key]
}
return nil
}
subscript(dynamicMember member: String) -> JSON? {
if case .dictionaryValue(let dict) = self {
return dict[member]
}
return nil
}
}
正常使用
let json = JSON.stringValue("Example")
json[0]?["name"]?["first"]?.stringValue
如果用上述的写法
json[0]?.name?.first?.stringValue
Swift 4.1引入了有条件地遵循协议
extension Array: Purchaseable where Element: Purchaseable {
func buy() {
for item in self {
item.buy()
}
}
}
但是在Swift 4.1中,如果你要确定对象是否遵循某个协议,会报错。Swift 4.2 修复了这个问题
let items: Any = [Book(), Book(), Book()]
if let books = items as? Purchaseable {
books.buy()
}
还有,Swift 内置的类型,可选,数组,字典,区间,如果它们的成员遵循Hashable
,那么它们也会自动遵循Hashable
。
shuffling
SE-0202提议的实现。Swift 4.2提供了原生的随机数方法。意味着你不需要使用arc4random_uniform()
或者GameplayKit来实现了。
let randomInt = Int.random(in: 1..<5)
let randomFloat = Float.random(in: 1..<10)
let randomDouble = Double.random(in: 1...100)
let randomCGFloat = CGFloat.random(in: 1...1000)
let randomBool = Bool.random()
SE-0202同样还提议了shuffle()
和shuffled()
var albums = ["Red", "1989", "Reputation"]
// shuffle in place
albums.shuffle()
// get a shuffled array back
let shuffled = albums.shuffled()
还有randomElement()
方法。
if let random = albums.randomElement() {
print("The random album is \(random).")
}
SE-0206的实现,让你更简单的为自建类型使用Hashable
协议。
Swift 4.1 能够为遵循Hashable
协议的类型自动生成hash值。但是如果你需要自行实现仍然需要写不少代码。
Swift 4.2 引入了Hasher
结构,提供了随机种子,和通用的hash函数来简化过程
struct iPad: Hashable {
var serialNumber: String
var capacity: Int
func hash(into hasher: inout Hasher) {
hasher.combine(serialNumber)
}
}
let first = iPad(serialNumber: "12345", capacity: 256)
let second = iPad(serialNumber: "54321", capacity: 512)
var hasher = Hasher()
hasher.combine(first)
hasher.combine(second)
let hash = hasher.finalize()
SE-0207的实现,提供了allSatisfy()
方法来检测数组中所有的元素是否都满足条件。
let scores = [85, 88, 95, 92]
let passed = scores.allSatisfy { $0 >= 85 }
SE-0197提供一个全新的removeAll(where:)
方法,以此来提供一个更高效,会操作原数据的类似filter
的方法。
var pythons = ["John", "Michael", "Graham", "Terry", "Eric", "Terry"]
pythons.removeAll { $0.hasPrefix("Terry") }
print(pythons)
SE-0199提供了,对Bool
的 toggle()
方法,类似
extension Bool {
mutating func toggle() {
self = !self
}
}
Swift 4.2 你可以这样
var loggedIn = false
loggedIn.toggle()
Equatable
和Hashable
协议类和结构体做可比较,需要自己手动实现。
struct Person: Equatable {
var firstName: String
var lastName: String
var age: Int
var city: String
static func ==(lhs: Person, rhs: Person) -> Bool {
return lhs.firstName == rhs.firstName && lhs.lastName == rhs.lastName && lhs.age == rhs.age && lhs.city == rhs.city
}
}
let person1 = Person(firstName: "hicc", lastName: "w", age: 20, city: "shenzhen")
let person2 = Person(firstName: "hicc", lastName: "w", age: 20, city: "shenzhen")
print("person1 1 == person2 : \(person1 == person2)")
// person1 1 == person2 : true
Swift 4.1 提供了Equatable
的协议,它会自动的生成==
方法。
当然你还是可以自己实现==
方法(例如,业务有id之类的属性)。
还有之前实现一个对象的hash值也是一件麻烦的事情,你可能需要手动实现类似:
var hashValue: Int {
return firstName.hashValue ^ lastName.hashValue &* 16777619
}
Swift 4.1 提供了Hashable
的协议,可以自动生成hashValue
,你也还是可以自行实现。
struct Person2: Equatable, Hashable {
var firstName: String
var lastName: String
var age: Int
var city: String
}
let person11 = Person2(firstName: "hicc", lastName: "w", age: 20, city: "shenzhen")
let person22 = Person2(firstName: "hicc", lastName: "w", age: 20, city: "shenzhen")
print("person11 1 == person22 : \(person11 == person22), \(person11.hashValue)")
// person11 1 == person22 : true, 5419288582170212869
Codable
协议,Key值转化策略Swift 4提供了很方便的Codable
协议,但是它使用下划线snake_case而不是驼峰式的方式来转化Key,不太自由。
Swift 4.1 中针对这种情况,提供了keyDecodingStrategy
,以及keyEncodingStrategy
属性(默认.useDefaultKeys
)来解决这些问题。
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
let macs = try decoder.decode([Mac].self, from: jsonData)
print(macs)
} catch {
print(error.localizedDescription)
}
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
let encoded = try encoder.encode(someObject)
Swift 4.1 实现了SE-0143的提议,容许你类型在某下情况下才遵循某个协议。
extension Array: Purchaseable where Element: Purchaseable {
func buy() {
for item in self {
item.buy()
}
}
}
这样会让你的代码,更加的安全。如下代码Swift中会拒绝编译,因为其未遵循Coodable
协议.
import Foundation
struct Person {
var name = "Taylor"
}
var people = [Person()]
var encoder = JSONEncoder()
try encoder.encode(people)
Swift 4.1实现了SE-0157提议,在递归协议中,关联类型可以被定义它的协议所限制。
protocol Employee {
associatedtype Manager: Employee
var manager: Manager? { get set }
}
// TODO: 现在感受不太清楚,后续有深入了解在补充。
canImport
函数SE-0075提议的实现。Swift 4.1引入了canImport
函数,让你可以检查某个模块能否被导入。
#if canImport(SpriteKit)
// this will be true for iOS, macOS, tvOS, and watchOS
#else
// this will be true for other platforms, such as Linux
#endif
之前还有类似的方法
#if !os(Linux)
// Matches macOS, iOS, watchOS, tvOS, and any other future platforms
#endif
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
// Matches only Apple platforms, but needs to be kept up to date as new platforms are added
#endif
targetEnvironment
函数SE-0190提议的实现,Swift 4.1 提供了targetEnvironment
函数,来检测是模拟器还是真实的硬件。
#if targetEnvironment(simulator)
// code for the simulator here
#else
// code for real devices here
#endif
flatMap之前一个很有用的作用是能够过滤数组中为nil
的元素,Swift 4.2重命名为指意明确,更强大的compactMap
let array = ["1", "2", "Fish"]
let numbers = array.compactMap { Int($0) }
// [1, 2]
Coodable
协议Swift 4之前使用NSCoding
来做encoding和decoding的事情,但是需要一些模版代码,也容易出错,Swift 4中 Coodable
协议就是为这个而存在。
使用起来简单到不可思议。
struct Language: Codable {
var name: String
var version: Int
}
let swift = Language(name: "Swift", version: 4)
完整的使用
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(swift) {
if let json = String(data: encoded, encoding: .utf8) {
print("swift strng\(json)")
}
let decoder = JSONDecoder()
if let decoded = try? decoder.decode(Language.self, from: encoded) {
print("Swift name: \(decoded.name)")
}
}
跨越多行的字符串可以使用"""
来包裹。
let quotation = """
The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""
keypaths
keypaths
是指对属性的引用而不去真正读取属性的值。
struct Crew {
var name: String
var rank: String
}
struct Starship {
var name: String
var maxWarp: Double
var captain: Crew
}
let janeway = Crew(name: "Kathryn Janeway", rank: "Captain")
let voyager = Starship(name: "Voyager", maxWarp: 9.975, captain: janeway)
let nameKeyPath = \Starship.name
let maxWarpKeyPath = \Starship.maxWarp
let captainName = \Starship.captain.name
let starshipName = voyager[keyPath: nameKeyPath]
let starshipMaxWarp = voyager[keyPath: maxWarpKeyPath]
let starshipCaptain = voyager[keyPath: captainName]
print("starshipName \(starshipName),\(starshipCaptain)")
// starshipName Voyager, Kathryn Janeway
Swift 4改进了字典的诸多函数。
filter
返回的是个字典map
返回的仍然是数组mapValues
,返回的则是字典grouping
初始化方法,可以将数组处理成字典default
赋值和取值会比较方便。let cities = ["Shanghai": 24_256_800, "Karachi": 23_500_000, "Beijing": 21_516_000, "Seoul": 9_995_000];
let massiveCities = cities.filter { $0.value > 10_000_000 }
let populations = cities.map { $0.value * 2 }
let roundedCities = cities.mapValues { "\($0 / 1_000_000) million people" }
let groupedCities = Dictionary(grouping: cities.keys) { $0.first! }
let groupedCities2 = Dictionary(grouping: cities.keys) { $0.count }
var favoriteTVShows = ["Red Dwarf", "Blackadder", "Fawlty Towers", "Red Dwarf"]
var favoriteCounts = [String: Int]()
for show in favoriteTVShows {
favoriteCounts[show, default: 0] += 1
}
print("dic\(massiveCities),\n\(populations),\n\(roundedCities),\n\(groupedCities),\n\(groupedCities2),\n\(favoriteCounts)")
// dic["Shanghai": 24256800, "Beijing": 21516000, "Karachi": 23500000],
// [43032000, 47000000, 19990000, 48513600],
///["Beijing": "21 million people", "Karachi": "23 million people", "Seoul": "9 million people", "Shanghai": "24 million people"],
// ["S": ["Seoul", "Shanghai"], "B": ["Beijing"], "K": ["Karachi"]],
// [8: ["Shanghai"], 5: ["Seoul"], 7: ["Beijing", "Karachi"]],
// ["Blackadder": 1, "Fawlty Towers": 1, "Red Dwarf": 2]
字符串是Collection类型,这样就有了诸多便利的方法。
let quote = "It is a truth universally acknowledged that new Swift versions bring new features."
let reversed = quote.reversed()
for letter in quote {
print(letter)
}
Swift 4 支持了单侧区间, 缺失的一边为0或者为集合的尽头
let characters = ["Dr Horrible", "Captain Hammer", "Penny", "Bad Horse", "Moist"]
let bigParts = characters[..<3]
let smallParts = characters[3...]
print(bigParts)
print(smallParts)
// ["Dr Horrible", "Captain Hammer", "Penny"]
// ["Bad Horse", "Moist"]
Swift支持对扩展做限制。
extension Collection where Iterator.Element: Comparable {
func lessThanFirst() -> [Iterator.Element] {
guard let first = self.first else { return [] }
return self.filter { $0 < first }
}
}
let items = [5, 6, 10, 4, 110, 3].lessThanFirst()
print(items)
extension Array where Element: Comparable {
func lessThanFirst() -> [Element] {
guard let first = self.first else { return [] }
return self.filter { $0 < first }
}
}
let items = [5, 6, 10, 4, 110, 3].lessThanFirst()
print(items)
上述3.0的对扩展的限制都是通过协议实现。Swift 3.1 支持使用类型来限制。
extension Array where Element == Int {
func lessThanFirst() -> [Int] {
guard let first = self.first else { return [] }
return self.filter { $0 < first }
}
}
let items = [5, 6, 10, 4, 110, 3].lessThanFirst()
print(items)
Swift 3.1支持了嵌套类型中使用泛型。
struct Message<T> {
struct Attachment {
var contents: T
}
var title: T
var attachment: Attachment
}
prefix(while:)
, drop(while:)
两个方法prefix(while:)
: 遍历所有元素,直到遇到不满足条件的元素 ,并且返回满足的元素drop(while:)
: 就是返回 prefix(while:)
相反的就好。let names = ["Michael Jackson", "Michael Jordan", "Michael Caine", "Taylor Swift", "Adele Adkins", "Michael Douglas"]
let prefixed = names.prefix { $0.hasPrefix("Michael") }
print(prefixed)
let dropped = names.drop { $0.hasPrefix("Michael") }
print(dropped)
Swift特点是函数可以分别制定参数标签(argument label)和参数名称(parameter name)
func someFunction(argumentLabel parameterName: Int) {
}
// 使用必须带上参数标签
someFunction(argumentLabel: 1)
// 如果不指定,参数名称可以作为菜参数标签
func someFunction(firstParameterName: Int, secondParameterName: Int) {
}
someFunction(firstParameterName: 1, secondParameterName: 2)
如果你不想使用参数标签,可以使用_
代替
func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
}
someFunction(1, secondParameterName: 2)
主要是一些内置对象,以及和平台相关的精简,让代码更加易读。
// Swift 2.2
let blue = UIColor.blueColor()
let min = numbers.minElement()
attributedString.appendAttributedString(anotherString)
names.insert("Jane", atIndex: 0)
UIDevice.currentDevice()
// Swift 3
let blue = UIColor.blue
let min = numbers.min()
attributedString.append(anotherString)
names.insert("Jane", at: 0)
UIDevice.current
以及
// 第一行是Swift 2.2
// 迪尔汗是Swift 3
" Hello ".stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())
" Hello ".trimmingCharacters(in: .whitespacesAndNewlines)
"Taylor".containsString("ayl")
"Taylor".contains("ayl")
"1,2,3,4,5".componentsSeparatedByString(",")
"1,2,3,4,5".components(separatedBy: ",")
myPath.stringByAppendingPathComponent("file.txt")
myPath.appendingPathComponent("file.txt")
"Hello, world".stringByReplacingOccurrencesOfString("Hello", withString: "Goodbye")
"Hello, world".replacingOccurrences(of: "Hello", with: "Goodbye")
"Hello, world".substringFromIndex(7)
"Hello, world".substring(from: 7)
"Hello, world".capitalizedString
"Hello, world".capitalized
以及, lowercaseString
-> lowercased()
,uppercaseString
->uppercased()
dismissViewControllerAnimated(true, completion: nil)
dismiss(animated: true, completion: nil)
dismiss(animated: true)
prepareForSegue()
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?)
正如标题说的,一方面这是Swift推荐的用法,另外就是内置对象的变化
UIInterfaceOrientationMask.Portrait // old
UIInterfaceOrientationMask.portrait // new
NSTextAlignment.Left // old
NSTextAlignment.left // new
SKBlendMode.Replace // old
SKBlendMode.replace // new
还有就是Swift可选类型是通过枚举来实现的
enum Optional {
case None
case Some(Wrapped)
}
如果使用.Some
来处理可选,也需要更改
for case let .some(datum) in data {
print(datum)
}
for case let datum? in data {
print(datum)
}
大体来说就是让C函数使用更加的Swift
// Swift 2.2
let ctx = UIGraphicsGetCurrentContext()
let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512)
CGContextSetFillColorWithColor(ctx, UIColor.redColor().CGColor)
CGContextSetStrokeColorWithColor(ctx, UIColor.blackColor().CGColor)
CGContextSetLineWidth(ctx, 10)
CGContextAddRect(ctx, rectangle)
CGContextDrawPath(ctx, .FillStroke)
UIGraphicsEndImageContext()
// Swift 3
if let ctx = UIGraphicsGetCurrentContext() {
let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512)
ctx.setFillColor(UIColor.red.cgColor)
ctx.setStrokeColor(UIColor.black.cgColor)
ctx.setLineWidth(10)
ctx.addRect(rectangle)
ctx.drawPath(using: .fillStroke)
UIGraphicsEndImageContext()
}
以及
// 第一行是Swift 2.2
// 第二行是Swift 3
CGAffineTransformIdentity
CGAffineTransform.identity
CGAffineTransformMakeScale(2, 2)
CGAffineTransform(scaleX: 2, y: 2)
CGAffineTransformMakeTranslation(128, 128)
CGAffineTransform(translationX: 128, y: 128)
CGAffineTransformMakeRotation(CGFloat(M_PI))
CGAffineTransform(rotationAngle: CGFloat(M_PI))
这部分属于Swift更加语义化的改进,到现在5.1的时候一直在改进,目前官网最近的规范Swift.org - API Design Guidelines方法的部分是:
x.distance(to: y)
, i.successor()
print(x)
,x.sort()
,x.append(y)
ed
(通常是,不修改原数据,而是返回新的),有修改的使用现在时ing
。from
。