Re: How to enumerate by code points in Swift beta4?
Re: How to enumerate by code points in Swift beta4?
- Subject: Re: How to enumerate by code points in Swift beta4?
- From: Roland King <email@hidden>
- Date: Wed, 23 Jul 2014 17:19:33 +0800
On 23 Jul 2014, at 5:03 pm, Quincey Morris < email@hidden> wrote:
On Jul 22, 2014, at 23:26 , Roland King < email@hidden> wrote: Running that in a playground (playgrounds are working better in Beta 4) gives me those 3 UTF-16 code points. No, what you get in UTF-16 are code *units* — the 16-bit values that make up the UTF-16 sequence. (Code units are 16-bit values in UTF-16, 8-bit values in UTF-8 and 32-bit values in UTF-32.)
Apologies - Unicode is one of those places I know that terminology is important, and I just don’t know it.
That means that “Character” in Swift doesn’t mean anything simple — not anything that can be represented as a single numeric value, and certainly not a code point. For mainstream string usage, that’s good news — no more messing around with ‘rangeOfComposedCharacters’, but if you actually want the code points, it’s a retrogression.
That’s what I thought first.
I suppose the logical way to get code points would be to enumerate ‘ss.utf32’ (code units and code points are numerically identical in that case), but no such property seems to exist in Swift. Perhaps there is something else that gives code points, but we just don’t know what it is.
Well again the wonderful swift book gives us string.unicodeScalars, which instead of failing to define myself I’ll just copy from the book.
“This property is of type UnicodeScalarView, which is a collection of values of type UnicodeScalar. A Unicode scalar is any 21-bit Unicode code point that is not a lead surrogate or trail surrogate code point.
Each UnicodeScalar has a value property that returns the scalar’s 21-bit value, represented within a UInt32 value:”
Indeed using that instead of the utf16 property gives the output Gerriet wanted in his first and last cases.
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden