Instance Property インスタンスプロパティ

resourceBytes

The URL’s resource data, as an asynchronous sequence of bytes.

Declaration 宣言

var resourceBytes: URL.AsyncBytes { get }

Discussion 議論

Use this property with Swift’s for-await-in syntax, to read the contents of a URL byte-by-byte, like this:


guard let url = URL(string: "https://www.example.com") else {
    return
}
do {
    // Read each byte of the data as it becomes available.
    for try await byte in url.resourceBytes
    {
        // Do something with byte.
    }
} catch {
    print ("Error: \(error)")
}

To wait for the entire resource to load, use the URLSession method data(from:delegate:) with the await keyword. URLSession also offers methods to upload data to a URL endpoint and download a URL’s contents to a file.

See Also 参照

Loading URL Contents Asynchronously