开始
系列
cgfloat rounded
(.towardZero)
x.rounded(.towardZero),直接去掉小数部分,保留整数部分。类似same thing with Int(x).
3.000 -> 3.0 |
(.down)
x.rounded(.down),变小。类似 floor(x)
3.000 -> 3.0 |
(.up)
x.rounded(.up),变大。类似 ceil(x)
3.000 -> 3.0 |
(.toNearestOrAwayFromZero)
x.rounded(.toNearestOrAwayFromZero),四舍五入。也就是默认 x.rounded()
3.000 -> 3.0 |
(.toNearestOrEven)
x.rounded(.toNearestOrEven)
This is similar to toNearestOrAwayFromZero, except now the .5 values get rounded to the even whole number.
3.000 -> 3.0 |
(.awayFromZero)
x.rounded(.awayFromZero),远离0
3.000 -> 3.0 |
这是什么形式
// 错误 |
for in with index
for index in 1...5 { |
optional string to int
let i = (optionalString == nil || Int(optionalString!) == nil) ? 0 : Int(optionalString!)! |
map、flatmap、compactmap
map 生成新的 array
flatmap 嵌套的 array 展开
enum
In Swift 4.2, the CaseIterable protocol can be used for an enum with rawValues, but the string should match against the enum case labels:
enum MyCode : String, CaseIterable {
case one = “uno”
case two = “dos”
case three = “tres”
static func withLabel(_ label: String) -> MyCode? {
return self.allCases.first{ “($0)” == label }
}
}
usage:
print(MyCode.withLabel(“one”)) // Optional(MyCode.one)
print(MyCode(rawValue: “uno”)) // Optional(MyCode.one)
自定义print
目前 swift 里已经没有宏定义了,可以在项目设置里添加下
Target→Build Settings→Other swift Flags 下面的 Debug 添加 -D DEBUGLOG
func printX(_ message: Any..., |
问题
import Foundation 是否必要
If you want to work with Strings, Dates, etc you need to import Foundation.
The Foundation framework provides a base layer of functionality for apps and frameworks, including data storage and persistence, text processing, date and time calculations, sorting and filtering, and networking.
参考
Swift - 第三方加密库CryptoSwift使用详解3(AES加密与解密)
Why Swift Enums with Associated Values Cannot Have a Raw Value
Advanced and Practical Enum usage in Swift超详细教程
How to round CGFloat
Double-Float+Rounding.swift