The documentation certainly says that, Ken, but stick this code in a playground
and see that you can’t examine the characters via index no matter whether you
assume it to be String or NSString:
let whitespaceSet = NSCharacterSet.whitespaceAndNewlineCharacterSet()
let attrStr = NSAttributedString( string:" Fourscore and seven years ago
\n\n\n\t\t\t " )
let str = attrStr.string
var head = 0
let tooFar = attrStr.length
while head < tooFar {
if whitespaceSet.characterIsMember( str.characterAtIndex( head ) ) {
// Skip -- I did it this way so the error message received from the above
line will be clear
} else {
break;
}
++head
}
var headIx = str.startIndex
let tooFarIx = str.endIndex
while headIx < tooFarIx {
if whitespaceSet.characterIsMember( str[ headIx ] ) {
// Skip
} else {
break;
}
headIx = headIx.successor()
}
characterAtIndex() doesn’t work because it’s not available in Swift. If you
replace str.characterAtIndex( head ) with with str[ head ], you get the same
error as in the version below it that incorrectly assumes it’s a Swift string:
“Could not find overload of 'subscript' that accepts the supplied arguments.”
Now, I did just type this out on a computer running Xcode 6.2. At home I’m
using 6.3 beta, so it’s possible I’ll get home and find one of these versions
works as expected, even though I’m sure I tried both ways last night when I
first hit the roadblock…
I’m now guessing that maybe converting from NSString to String and examining
characters via one of the UTF views might possibly not involve a copy. But then
how do I decide which view I should be using...
--
Charles
On April 2, 2015 at 08:44:52, Ken Thomases ([email protected]) wrote:
On Apr 2, 2015, at 6:54 AM, Charles Jenkins <[email protected]> wrote:
> What would be nice is a way to count leading and trailing characters in place
> while the thing is still an NSAttributedString--without using
> NSAttributedString.string to convert to a Swift string in the first place.
NSAttributedString.string does not involve a conversion. The underlying string
is part of NSAttributedString's data model. The documentation for the method
explicitly says, "For performance reasons, this property returns the current
backing store of the attributed string object."
I don't know if there's a conversion to create a Swift string from that, but
you don't have to. I believe you can work with NSString in Swift.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]