I am looking for Swift I / O for short presentations at my university next week. I really did not find anything about this.
Here are the operations I found:
let location = "/Users/test/test.txt" var str = "Hello, playground" //write str.writeToFile( location, atomically: false, encoding: NSUTF8StringEncoding, error: nil) //read let filecontent = String(contentsOfFile: location, encoding: NSUTF8StringEncoding, error: nil) //binary read let test = NSData(contentsOfFile: location) //if file exists NSFileManager().fileExistsAtPath(location) //find Files in the App Bundle with suffix test and prefix txt var path= NSBundle.mainBundle().pathForResource("test", ofType: "txt") //path for dynamic user names if let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] { let path = dirs[0].stringByAppendingPathComponent( "file.txt") //printout: /Users/test/Documents/file.txt
Is there an operation with which I can read single characters?
Can I add text to a file? Can I write / read only a line?
Are there any other file operations I need to know there?
source share