Re: UIKit and Swift
Re: UIKit and Swift
- Subject: Re: UIKit and Swift
- From: Roland King <email@hidden>
- Date: Mon, 16 Jun 2014 07:08:00 +0800
>
> Ah ok, figured it out; your code gave me a hint. If you have a String, the methods that are in NSString just work on it. If you want the extensions, you have to be explicit about NSString:
>
> drawable.name is a String?:
>
>
> // does not work, sizeWithFont not found
> if let name = drawable.name {
> var size = name.sizeWithFont(font)
> // etc...
> }
>
> // works fine
> if let name: NSString = drawable.name {
> var size = name.sizeWithFont(font)
> // etc...
> }
>
> (Yes, sizeWithFont is deprecated, I still need to switch it out with sizeWithAttributes)
>
> Thanks!!
>
I finally came across this in the 'Swift Interoperability in Depth" WWDC video where it says ..
Foundation NSString APIs are available on String
let fruits = "apple;banana;cherry".componentsSeparatedByString(";")
// inferred as String[]
Cast to NSString to access properties and methods on NSString categories
("Welcome to WWDC 2014" as NSString).myNSStringMethod()
it also suggests that if you find yourself doing that often for a given method, you write the extension in Swift.
_______________________________________________
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