> 
> 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 (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to