首发: cclogpython
Hipo 2.0 重写从Swift 1的版本写到2的版本,后续Hipo功能稳定,更新慢了不少……,Swift自己却在长足的发展,5.0都已经发布了,本文对Swift 3.0 到Swift 5.1 的更新点作个总结。git
为了方便阅读,准备重新到旧的总结。github
下面全部的东西,都是来自hackingwithswift.com。express
Swift 5.1的更新比较迟,单独成篇Swift 5.1的变化。json
Swift 5.0 最重要的天然是ABI Stability, 对此能够看这篇 Swift ABI 稳定对咱们到底意味着什么 。swift
固然还有其余的更新。api
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]swift.org/documentati…)方法的部分是:
x.distance(to: y)
, i.successor()
print(x)
,x.sort()
,x.append(y)
ed
(一般是,不修改原数据,而是返回新的),有修改的使用如今时ing
。from
。