Search code examples
iosobjective-cswift4nsdata

Swift 4 Data reading options


What is the correct translation of this Objective C code line to Swift 4?

NSData *mappedData =
  [NSData dataWithContentsOfURL:fileURL
                        options:NSDataReadingMappedAlways + NSDataReadingUncached
                          error:&error];

I tried this but it doesn't compile:

 Data(contentsOf: fileUrl, options: Data.ReadingOptions.dataReadingMapped | Data.ReadingOptions.uncached)

Solution

  • You can try

    do {
    
         // note it runs in current thread
    
        let data = try Data(contentsOf:fileURL, options: [.alwaysMapped , .uncached ] )
    
        print(data)
    
    }
    catch {
    
        print(error)
    }