[swift-users] Pure Swift, cross platform way to open, read/write files?

Jens Alfke jens at mooseyard.com
Wed Dec 9 11:36:01 CST 2015

> On Dec 9, 2015, at 6:40 AM, Perry E. Metzger via swift-users <swift-users at swift.org> wrote:
> This will also require a bit of design to look "Swifty”.

Yes, amen. The thought of a Swift bridge to the standard C library gives me queasy thoughts of PHP, whose core APIs are a horrible pidgin translation of C.

An I/O library should begin with an abstract stream API, which can then be given a concrete file-descriptor-based implementation that can be used for files, sockets, etc. (Although the fact that it’s based on file descriptors should be hidden.)

IMHO one of the biggest design problems of Foundation is that it has very weak support for streams. NSStream is very difficult to subclass, and most I/O doesn’t even use it, so there are historical remnants like NSFileHandle and high-level alternatives like -[NSData initWithContentsOfFile:] that everyone ends up using.

> A lower layer is also needed -- simple Swifty wrappers for the Unix
> system calls so that Swift implementations can get at them without
> having to use unsafe constructs. open(2), read(2), write(2), lseek(2)
> and close(2) would provide about 75% of what one needs for the higher
> layer stuff (and everything one would want for stdio) with quite
> minimal hacking.

I agree, but I foresee problems in the future when Swift is ported to Windows. :/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151209/2eb159c9/attachment.html>

More information about the swift-users mailing list