Avatar
Swift 5のString.UTF8View.withContiguousStorageIfAvailable(_:)、macOSだとStringの作り方によって使えたり使えなかったりする。
12:05 PM
$ xcrun --toolchain org.swift.5020181228a swift swift_oss_helper command enabled. Welcome to Apple Swift version 5.0-dev (LLVM c351ac7354, Clang aadec4ff83, Swift 5cfc2e7ba9). Type :help for assistance. 1> import Foundation 2. let url = URL(fileURLWithPath: "/etc/passwd") 3. let a = String(data: try Data(contentsOf: url), encoding: .utf8)?.utf8.withContiguousStorageIfAvailable { $0.count } 4. let b = try String(contentsOf: url, encoding: .utf8).utf8.withContiguousStorageIfAvailable { $0.count } url: URL = "file:///etc/passwd" a: Int? = 6804 b: Int? = nil 5> (edited)