0%

swift for in 循环和 while 循环

开始

  1. 系列
    1. cgfloat rounded
      1. (.towardZero)
      2. (.down)
      3. (.up)
      4. (.toNearestOrAwayFromZero)
      5. (.toNearestOrEven)
      6. (.awayFromZero)
    2. 这是什么形式
    3. for in with index
    4. optional string to int
    5. map、flatmap、compactmap
    6. enum
    7. 自定义print
  2. 问题
  3. 参考

系列

cgfloat rounded

(.towardZero)

x.rounded(.towardZero),直接去掉小数部分,保留整数部分。类似same thing with Int(x).

3.000  ->  3.0
3.001 -> 3.0
3.499 -> 3.0
3.500 -> 3.0
3.999 -> 3.0

-3.000 -> -3.0
-3.001 -> -3.0
-3.499 -> -3.0
-3.500 -> -3.0
-3.999 -> -3.0

(.down)

x.rounded(.down),变小。类似 floor(x)

3.000  ->  3.0
3.001 -> 3.0
3.499 -> 3.0
3.500 -> 3.0
3.999 -> 3.0

-3.000 -> -3.0
-3.001 -> -4.0
-3.499 -> -4.0
-3.500 -> -4.0
-3.999 -> -4.0

(.up)

x.rounded(.up),变大。类似 ceil(x)

3.000  ->  3.0
3.001 -> 4.0
3.499 -> 4.0
3.500 -> 4.0
3.999 -> 4.0

-3.000 -> -3.0
-3.001 -> -3.0
-3.499 -> -3.0
-3.500 -> -3.0
-3.999 -> -3.0

(.toNearestOrAwayFromZero)

x.rounded(.toNearestOrAwayFromZero),四舍五入。也就是默认 x.rounded()

3.000  ->  3.0
3.001 -> 3.0
3.499 -> 3.0
3.500 -> 4.0 ***
3.999 -> 4.0

-3.000 -> -3.0
-3.001 -> -3.0
-3.499 -> -3.0
-3.500 -> -4.0 ***
-3.999 -> -4.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
3.001 -> 3.0
3.499 -> 3.0
3.500 -> 4.0 ***
3.999 -> 4.0
4.500 -> 4.0 ***

-3.000 -> -3.0
-3.001 -> -3.0
-3.499 -> -3.0
-3.500 -> -4.0 ***
-3.999 -> -4.0
-4.500 -> -4.0 ***

(.awayFromZero)

x.rounded(.awayFromZero),远离0

3.000  ->  3.0
3.001 -> 4.0
3.499 -> 4.0
3.500 -> 4.0
3.999 -> 4.0

-3.000 -> -3.0
-3.001 -> -4.0
-3.499 -> -4.0
-3.500 -> -4.0
-3.999 -> -4.0

这是什么形式

// 错误
static var oneBookEntity: Book = { () -> Book in
let one = Book(context: PersistenceController.previewBook.container.viewContext)
return one
}
// 可以
static var oneBookEntity: Book = {
let one = Book(context: PersistenceController.previewBook.container.viewContext)
return one
}()

for in with index

for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}

for (index, element) in list.enumerated() {
print("Item \(index): \(element)")
}

optional string to int

let i = (optionalString == nil || Int(optionalString!) == nil) ? 0 : Int(optionalString!)!

func toInt(s: String?) -> Int {
var result = 0
if let str: String = s,
i = Int(str) {
result = i
}
return result
}

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...,
file: String = #file,
method: String = #function,
line: Int = #line) {
#if DEBUGLOG
print("\((file as NSString).lastPathComponent)[\(line)], \(method): \(message)")
#endif
}

问题

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