[swift-users] uppdate and read files on ubuntu
Jens Alfke
jens at mooseyard.com
Tue Dec 29 15:45:03 CST 2015
> On Dec 29, 2015, at 1:22 PM, Jonas Fredriksson via swift-users <swift-users at swift.org> wrote:
>
> second, to read a file I tried fopen from Glibc but i requires "UnsafePointer<Int8>". how do I declare that, ie var file: UnsafePointer<Int8>?
You probably wouldn’t declare a variable of that type. Instead just pass a String for that parameter — the compiler will convert to a C string for you.
For now the best documentation comes from the Using Swift With Cocoa And Objective-C book, which has a section on plain C APIs:
Constant Pointers
When a function is declared as taking a UnsafePointer<Type> argument, it can accept any of the following:
...
* A String value, if Type is Int8 or UInt8. The string will automatically be converted to UTF8 in a buffer that lasts for the duration of the call.
Whether that’s appropriate for fopen() depends on whether the Linux filesystem APIs use UTF-8 encoding. (Sorry, I don’t know Linux well enough to say.) If they don’t, you’ll have trouble with filenames containing non-ASCII characters; in that case you’d need to write a function to encode the String into a byte array in the right encoding, and then pass that to fopen().
—Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151229/bfe22258/attachment.html>
More information about the swift-users
mailing list