Read data from file


I have a file and want to read its data.


// Create a FileHandle instance

let file: NSFileHandle? = NSFileHandle(forReadingAtPath: "hello.swift")

if file != nil {
    // Read all the data
    let data = file?.readDataToEndOfFile()

    // Close the file

    // Convert our data to string
    let str = NSString(data: data!, encoding: NSUTF8StringEncoding)
else {
    print("Ooops! Something went wrong!")



Find answers here

Hi there! Ask Us a Question We are ready to help