@ WWDC 19
[B, E, A, R] μμ [B, I, R, D] λ₯Ό λ§λλ €λ©΄
- 곡ν΅λλ μμλ₯Ό μ°Ύλλ€. [B, R]
- [B, E, A, R]μμ 곡ν΅λμ§ μλ μμ μ κ±° -> [B, R]
- [B, I, R, D]λ‘ λ§λ€κΈ° μν΄ νμν μμ μ½μ -> [B, R] -> [B, I, R, D]
let diff = bird.difference(from: bear)
let newBird = bear.applying(diff)
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 { }
μ°μμ±μ 보μ₯νμ§ μλ λ€λ₯Έ λ²νΌ νμ μ λν΄μλ μ±νν μ μλ λ κ°μ νλ‘ν μ½μ μκ°νλ€.
let compressed = try data.compressed(using .lzfse)
public enum CompressionAlgorithm: Int {
case lzfse
case lz4
case lzma
case zlib
}
λ°μ΄ν° μμΆμ μμ κ°μ apiλ₯Ό μ΄μ©ν΄μ μ½κ² μ²λ¦¬ κ°λ₯νλ€.
-
UnitDuration
- Added
milliseconds
,microseconds
,nanoseconds
, andpicoseconds
- Added
-
UnitFrequency
- Added
framePerSecond
- Added
-
UnitInformationStorage
bits
,bytes
,nibbles
for common usage- SI- and binary-prefixed units (
kilo
,kibi
, ...,yotta
,yobi
) - Format with
MeasurementFormatter
andByteCountFormatter
let formatter = RelativeDateTimeFormatter()
let dateString = formatter.localizedString(for: aDate, relativeTo: now)
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)
λͺ¨λ μμ μ΄ λλκ³ λ λ€μ μ μ₯μ νκ³ μΆλ€λ©΄
// μλͺ»λ μμ
if (queue.operationCount == 0) {
save()
}
// μ μλνλ μμ
queue.addBarrierBlock {
save()
}
μ΄ κ²½μ° λ―Έλ¦¬ μ€μΌμ₯΄ λ μμ λ€μ΄ μλ£λκ³ λ ν save()κ° μ€νλ¨μ 보μ₯νλ€.
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
}
- Multiple volumes
- Use
FileManager.SearchPathDirectory.itemReplacementDirectory
- Use
- Disappearing volumes
- Use
Data.ReadingOptions.mappedIfSafe
- Use
- Slower file system operations
- Defer access to non-main thread
- Varying capabilities
- Test capabilites with
URLResourceKey
, e.g.volumeSupportsFileCloningKey
- Handle errors
- Test capabilites with
// 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 κ°λ₯
-
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 }
- Use
DataProtocol
instead of[UInt8]
- Format dates and lists with
Formatter
- Use
OperationQueue
's barrier and progress reporting