Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open a file in Swift?

Tags:

file

io

swift

I have yet to see documentation of a Swift standard library, where I would expect to find something like a File class or struct with an interface for opening, reading, and writing files. All the usual stuff you expect for File IO. Is there one, or are to depend on NSFileManager and its ilk?

like image 257
theory Avatar asked Jun 10 '14 23:06

theory


People also ask

How do I open a file in IOS?

Browse and open files and folders Tap Browse at the bottom of the screen, then tap an item on the Browse screen. If you don't see the Browse screen, tap Browse again. To view recently opened files, tap Recents at the bottom of the screen. To open a file, location, or folder, tap it.

How do I open the file manager in IOS Swift?

Do a print(path. absoluteString) and then $ open path in Terminal, to open the file or folder at path. Keep in mind that the directory (on your Mac) where a Simulator's files are stored can change between running your app.


1 Answers

Here's a way to do it if the file is in your iOS project (hoping this is your situation):

var filePath = Bundle.main.path(forResource: "theFile", ofType: "txt")
var data     = Data(contentsOf: URL(fileURLWithPath: filePath))
like image 63
Jean Le Moignan Avatar answered Oct 21 '22 11:10

Jean Le Moignan