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

Greg Skluzacek swftcdnnj at gmail.com
Tue Dec 8 22:45:27 CST 2015


Thanks for that blog post... perhaps I wasn't clear in what my actual
question was. Let me restate:

Obviously I can use Glibc on Linux to open a file and read it. And since
there is no Glibc on OS X, is there some other way that would be both CROSS
PLATFORM and done purely in swift instead of relying on some external C
library?

I've taken a cursory look at some of the early swift resources and didn't
see anything that mentioned how to open a file using JUST Native Swift
code... One would surely think that a programing language as nifty and cool
as Swift would have a easy way to read files. Maybe its obvious and I just
am missing it.

But at the same time I get your point just use #if #else #endif to
conditional import / call functions... but coding like that doesn't seem
vary fun... reminds me of looking a C code that was meant to be compiled on
different processor architectures. :-(

--Greg



On Mon, Dec 7, 2015 at 9:51 PM, Kwame Bryan <kwame.bryan at gmail.com> wrote:

> Hi Greg, you’ll need to do something like the following.
>
> #if os(Linux)
>     import Glibc
> #else
>     import Darwin
> #endif
>
> Also, take a look at this blog post.
> http://blog.krzyzanowskim.com/2015/12/04/swift-package-manager-and-linux-compatible/ Hope
> this helps.
>
> Kwame
>
> On Dec 7, 2015, at 10:47 PM, Greg Skluzacek via swift-users <
> swift-users at swift.org> wrote:
>
> Hi new to Swift and I am really excited that Apple has open sourced it! I
> use macs at home an Linux at work, so now I may actually be able to develop
> in one language on both platforms (and not use Java).
>
> That being said, is there a way open a file for either reading line by
> line and writing line by line that is done purely in Swift and is cross
> platform.
>
> I did go through the Getting Started page and saw that one was able to
> import Glibc on Linux which implements the C fopen, etc., but when I tried
> to import it in the REPL on OS X, I got the error:
>
> repl.swift:1:8: error: no such module 'Glibc'
> import Glibc
>        ^
>
> swift --version returns:
>
> Chateau-Louise:/ gskluzacek$ swift --version
> Apple Swift version 2.1 (swiftlang-700.1.101.6 clang-700.1.76)
> Target: x86_64-apple-darwin14.5.0
> Chateau-Louise:/ gskluzacek$
>
> Thanks,
> -- Greg
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151208/7bbb3205/attachment.html>


More information about the swift-users mailing list