Skip to content

Latest commit

Β 

History

History
209 lines (135 loc) Β· 4.18 KB

AdvancesInFoundation.md

File metadata and controls

209 lines (135 loc) Β· 4.18 KB

@ WWDC 19

Ordered Collection Diffing

[B, E, A, R] μ—μ„œ [B, I, R, D] λ₯Ό λ§Œλ“œλ €λ©΄

  1. κ³΅ν†΅λ˜λŠ” μš”μ†Œλ₯Ό μ°ΎλŠ”λ‹€. [B, R]
  2. [B, E, A, R]μ—μ„œ κ³΅ν†΅λ˜μ§€ μ•ŠλŠ” μš”μ†Œ 제거 -> [B, R]
  3. [B, I, R, D]둜 λ§Œλ“€κΈ° μœ„ν•΄ ν•„μš”ν•œ μš”μ†Œ μ‚½μž… -> [B, R] -> [B, I, R, D]
let diff = bird.difference(from: bear)
let newBird = bear.applying(diff)

Data

Contiguity

DataλŠ” μ—°μ†μ μž„μ΄ 보μž₯λœλ‹€.

public protocol ContiguousBytes {
  func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -
  > R
}

ContiguousBytes ν”„λ‘œν† μ½œμ„ μ±„νƒν•¨μœΌλ‘œμ¨ μ°¨ν›„ flattening을 κ±±μ •ν•  ν•„μš”κ°€ 없어진닀.

public protocol DataProtocol: RandomAccessCollecton where Element == UInt8, ... { }

public protocol MutableDataProtocol: DataProtocol, MutableCollection, RangeReplaceableCollection { }

연속성을 보μž₯ν•˜μ§€ μ•ŠλŠ” λ‹€λ₯Έ 버퍼 νƒ€μž…μ— λŒ€ν•΄μ„œλŠ” 채택할 수 μžˆλŠ” 두 개의 ν”„λ‘œν† μ½œμ„ μ†Œκ°œν•œλ‹€.

Compression

let compressed = try data.compressed(using .lzfse)

public enum CompressionAlgorithm: Int {
  case lzfse
  case lz4
  case lzma
  case zlib
}

데이터 압좕은 μœ„μ™€ 같은 apiλ₯Ό μ΄μš©ν•΄μ„œ μ‰½κ²Œ 처리 κ°€λŠ₯ν•˜λ‹€.

Units

  • UnitDuration

    • Added milliseconds, microseconds, nanoseconds, and picoseconds
  • UnitFrequency

    • Added framePerSecond
  • UnitInformationStorage

    • bits, bytes, nibbles for common usage
    • SI- and binary-prefixed units (kilo, kibi, ..., yotta, yobi)
    • Format with MeasurementFormatter and ByteCountFormatter

Relative Date Formatter

let formatter = RelativeDateTimeFormatter()
let dateString = formatter.localizedString(for: aDate, relativeTo: now)

List Formatter

let string = ListFormatter.localizedString(byJoining: ["🐢", "🐷", "πŸ¦„"])
let listFormatter = ListFormatter()
let dateFormatter = DateFormatter()

listFormatter.itemFormatter = dateFormatter
let string = listFormatter.string(from: dates)

// en_US: "8/15/19, 9/13/19, and 2/1/20"
// en_ES: "15/8/19, 13/9/19 y 1/2/20"
let listFormatter = ListFormatter()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium

listFormatter.itemFormatter = dateFormatter
let string = listFormatter.string(from: dates)

Operation Queue

λͺ¨λ“  μž‘μ—…μ΄ λλ‚˜κ³  λ‚œ λ‹€μŒ μ €μž₯을 ν•˜κ³  μ‹Άλ‹€λ©΄

// 잘λͺ»λœ μ˜ˆμ‹œ
if (queue.operationCount == 0) {
  save()
}
// 잘 μž‘λ™ν•˜λŠ” μ˜ˆμ‹œ
queue.addBarrierBlock {
  save()
}

이 경우 미리 μŠ€μΌ€μ₯΄ 된 μž‘μ—…λ“€μ΄ μ™„λ£Œλ˜κ³  λ‚œ ν›„ save()κ°€ 싀행됨을 보μž₯ν•œλ‹€.

Progress reporting

let queue = OperationQueue()
queue.progress.totalUnitCount = 3

queue.addOperation {
  task1()									// Finished task: 1 / 3
}

queue.addOperation {
  task2()									// Finished task: 2 / 3
}

queue.addOperation {
  task3()									// Finished task: 3 / 3
}

Be Prepared for USB and SMB on iOS

  • Multiple volumes
    • Use FileManager.SearchPathDirectory.itemReplacementDirectory
  • Disappearing volumes
    • Use Data.ReadingOptions.mappedIfSafe
  • Slower file system operations
    • Defer access to non-main thread
  • Varying capabilities
    • Test capabilites with URLResourceKey, e.g. volumeSupportsFileCloningKey
    • Handle errors

Swift Update

Scanner

// Swift 4
var nameNSString: NSString?
if scanner.scanUpToCharacters(from: .newlines, into: &nameNSString) {
  let name = nameNSString! as String
}

// Swift 5.1
let nameString = scanner.scanUpToCharacters(from: .newlines)
let matchedString = scanner.scanString(string: "hi, πŸ‘©β€πŸ’»") // emoji κ°€λŠ₯

FileHandle

  • Error-based API

    let fileHandle = FileHandle()
    let data = try fileHandle.readToEnd()
  • Works with DataProtocol

    extension FileHandle {
      public func write<T: DataProtocol>(contentsOf data: T) throws
    }

Try It

  • Use DataProtocol instead of [UInt8]
  • Format dates and lists with Formatter
  • Use OperationQueue's barrier and progress reporting