Re: How to convert String.Index to UInt?
Re: How to convert String.Index to UInt?
- Subject: Re: How to convert String.Index to UInt?
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Mon, 18 Aug 2014 11:36:14 +0700
On 16 Aug 2014, at 18:43, Roland King <email@hidden> wrote:
> I was asking more about actually using the language to write applications, real ones, not just examples in a book but real apps which have to interface with the runtime we use, Cocoa. Do you find you are really using the full power of Swift, making powerful typesafe data structures, using generics and only dropping back to Cocoa for input/output and event handling? It has seemed to me, and I’m most certainly not trying to be rude, that you’ve had to spend a lot of time re-expressing ObjC/Cocoa constructs in Swift, and often they aren’t very pretty.
Step 1 is to learn how to convert things one does in Obj-C into Swift.
Step 2 would be to think in Swift and do things in a swifty way.
And you are right: I am currently at Step 1, and am far away from having mastered it.
> The example in this thread was quite illustrative in some ways. Swift strings and NSStrings are convertible to each other. Swift strings have a rangeOfString() method, NSStrings have a rangeOfString: method. But the results of those two methods are very different, one’s a Swift range, a generic, it’s typesafe, it’s iterable, it’s fine in Swift, it’s a super thing until you want to use it as it bears no relation to, and as far as I could tell just wasn’t practically convertible to, the NSRange the NSString method returned which you needed back in Cocoa to colour your attributed string. So the answer ended up being to bail out of Swift right at the top, at the last point of compatibility, between string and NSString, and then just drive objective-C from there onwards, using a different call syntax.
Yes, there is definitely some friction between ranges (or string indices) in Swift strings and NSString, where the latter is the only thing which can be used in Cocoa.
But what worries me more are limitations in Swift:
Obj-C was always praised for it's dynamic nature, which - so the argument went - lets you do things which are (nearly) impossible in other languages.
Swift has NSSelectorFromString, but one can do nothing with this Selector (except passing it to some Obj-C object).
The Book says: “The performSelector: method and related selector-invoking methods are not imported in Swift because they are inherently unsafe.”
What is the right answer:
- this a real limitation of Swift. Swift is severely limited.
- this a real limitation of Swift. But cases where one would use this feature in Obj-C are hard to find.
- there is a viable (and maybe even better, clearer, safer) way to do the same thing in Swift.
I really wish there would be a list of things Swift can not do (as compared to Obj-C), where each item either has a Swift way of accomplishing the same thing, or explains why one would never (or very rarely) need this.
Kind regards,
Gerriet.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden