24

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?

4
  • 3
    there is already NSFileManager API and C API, I can't see any reason to make new Swift API Commented Jun 10, 2014 at 23:52
  • 3
    @BryanChen I would expect idiomatic Swift to develop with a much less verbose interface than NSFileManager. Give me let fh = File.open("path"); while var line = fh.readline() { ... }; fh.close(). Commented Jun 11, 2014 at 0:08
  • 1
    swift is a very new language. if you wait for a year, I am pretty sure there will be lots good library for you to use Commented Jun 11, 2014 at 0:14
  • 2
    @BryanChen Yep, that's my hope. Was just a little surprised something as basic as File I/O wasn't already there. But given the presence of Foundation, it makes sense to wait and see what idioms develop. Commented Jun 11, 2014 at 0:17

5 Answers 5

22

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)) 
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I meant a Swift-oriented interface, not existing Foundation libraries.
10

If you just need to read the contents of a file into a string, a Swift 2.0 way to do it is this:

do { var dictionary = try String(contentsOfFile: "/usr/share/dict/words", encoding: NSUTF8StringEncoding) } catch { } 

Otherwise, the more involved methods mentioned above works.

Comments

6

The Swift standard library does not include this functionality. The standard library mainly contains data structures, low-level types and calls, and semi-built-in language features; I/O beyond print() and readLine() is considered out of scope. I don't really expect this to change in the near future, either.

However, Foundation contains file I/O calls, and the Swift Corelibs project is working hard to reimplement Foundation in pure Swift so it's available everywhere Swift is. The POSIX I/O calls available on every major operating system are also available in Swift, although they're much clumsier to use.

4 Comments

Seems as though Foundation will kind of be the standard (extended) library for Swift, no?
@theory That seems to be the long-term plan, yes.
So NSData, then, as suggested by @jean-le-moignan?
@theory Yes, I'd say the various init(contentsOfURL:) throws and writeToURL(_:) throws APIs are the best simple solution for reading and writing files from Swift.
5

Often in apps you'll be using UIDocument and other iCloud linked ways of saving files, but in the Swift blog Apple uses examples based on C and POSIX for opening and saving files. So they have in one example:

let fd = open("/tmp/scratch.txt", O_WRONLY|O_CREAT, 0o666) if fd < 0 { perror("could not open /tmp/scratch.txt") } else { let text = "Hello World" write(fd, text, text.characters.count) close(fd) } 

And it looks very Swift like, but whether you want to use this over and above the Cocoa framework I don't know.

2 Comments

I don't think that's at all Swift-like. Very C-like.
@theory I meant in its brevity more than anything, it looks almost like it fits with Swift whereas placed next to Objective-C the code would look alien
1

I think the only valid answer at this point is: There is no Swift-based File/IO library, so you just have to use the Objective-C-based Foundation classes for now. As @BryanChen says, maybe come back in a year to find a more fleshed-out Swift standard library.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.