> On Dec 29, 2015, at 1:22 PM, Jonas Fredriksson via swift-users 
> <[email protected]> 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
_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to