Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

swift read file

let file = "file.txt" //this is the file. we will write to and read from it

let text = "some text" //just a text

if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {

    let fileURL = dir.appendingPathComponent(file)

    //writing
    do {
        try text.write(to: fileURL, atomically: false, encoding: .utf8)
    }
    catch {/* error handling here */}

    //reading
    do {
        let text2 = try String(contentsOf: fileURL, encoding: .utf8)
    }
    catch {/* error handling here */}
}
Comment

PREVIOUS NEXT
Code Example
Swift :: append new element to dictionary in swift 
Swift :: how to dismiss keyboard in swift 
Swift :: get keyboard height swift 
Swift :: swift 5 only the day number from date 
Swift :: button color swiftui 
Swift :: disable swipe to delete swift 
Swift :: swift initialize a view 
Swift :: tuple swift 
Swift :: how do i have countdown timer in swift stackoverflow 
Swift :: NumberFormatter swift 
Swift :: swift hello world 
Swift :: swift array contains 
Swift :: change individual element alignment swiftui 
Swift :: swift 5 get first character of string 
Swift :: how to set the font of text in swiftui 
Swift :: if else if and else statments in swift language 
Swift :: Swift Create an Empty Set 
Swift :: swift uilabel dynamic height based on text length 
Swift :: how to make month format in swift 
Swift :: latex tall parentheses 
Swift :: swift array map example 
Swift :: swift pass any closer to the function 
Swift :: Swift for Loop with where Clause 
Swift :: swift print struct name 
Swift :: store multiple items in one core data record 
Swift :: swift string interpolation 
Swift :: Swift while Loop to Display Game Level 
Swift :: Typealias for built-in types 
Swift :: Swift Labeled Statement with continue 
Swift :: swift complete print function syntax 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =