[swift-users] swift-users Digest, Vol 6, Issue 28
Ken Burgett
kenb at iotone.io
Mon May 30 12:43:28 CDT 2016
On 2016-05-30 10:00, swift-users-request at swift.org wrote:
> Send swift-users mailing list submissions to
> swift-users at swift.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://lists.swift.org/mailman/listinfo/swift-users
> or, via email, send a message with subject or body 'help' to
> swift-users-request at swift.org
>
> You can reach the person managing the list at
> swift-users-owner at swift.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of swift-users digest..."
>
>
> Today's Topics:
>
> 1. Re: Simple text file I/O with Swift 3 (Quinn "The Eskimo!")
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 30 May 2016 09:11:41 +0100
> From: "Quinn \"The Eskimo!\"" <eskimo1 at apple.com>
> To: Swift Users List <swift-users at swift.org>
> Subject: Re: [swift-users] Simple text file I/O with Swift 3
> Message-ID: <D94A5001-BE92-44FB-ADB8-CE0D68E62323 at apple.com>
> Content-Type: text/plain; charset=us-ascii
>
>
> On 28 May 2016, at 19:05, Ken Burgett via swift-users
> <swift-users at swift.org> wrote:
>
>> print(buf)
>
>
> The trick here is to replace the above line with:
>
> print(String(validatingUTF8: buf))
>
> `fgets` sets up `buf` to hold a C string, so you have to convert it to
> a Swift string. How do you do this depends on the encoding of the
> bytes. If you expect the C string to be UTF-8, then
> `String(validatingUTF8:)` is the way to go.
>
> IMPORTANT: This conversion can fail, which is why the above will print
> a bunch of optional strings, and you will have to decide what to your
> program should do when it does.
>
> * * *
>
> btw This question came up recently. See the thread for other
> suggestions about how to handle it.
>
> <http://article.gmane.org/gmane.comp.lang.swift.user/1943>
>
> Share and Enjoy
> --
> Quinn "The Eskimo!"
> <http://www.apple.com/developer/>
> Apple Developer Relations, Developer Technical Support, Core
> OS/Hardware
>
>
>
>
> ------------------------------
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
> End of swift-users Digest, Vol 6, Issue 28
> ******************************************
Hi Quinn,
Thanks for the tip on print(String(validatingUTF8: buf)), that does
reproduce my input text, line for line, EXCEPT for wrapping every line
in "Optional(line-of-text-with-terminator)", for example
"Optional("import Glibc\n")".
So, how does wrapping a line of UTF8 text in another character string
"Optional()" help me print the text? Is Optional() some kind of
function? If so, how is it intended to be used?
--
Ken Burgett
Principal Software Engineer
Email: kenb at iotone.io
Office: 530.693.4449
Mobile: 831.332.6846
URL: www.iotone.co
More information about the swift-users
mailing list