NSString.sizeWithAttributes () in Swift beta7

This piece of code used to work in Xcode 6 beta 5 works great:

func fitText(){
    let size = (self.text as NSString).sizeWithAttributes([NSFontAttributeName:self.font]) //Errors here
    self.frame.size = size
}

      

Now it gives the following errors on the second line:

'UIFont' is not a subtype of "NSDictionary"

Unable to convert expression type '$ T6' to type 'UIFont'

When I broke it into

let dict = [NSFontAttributeName:self.font]
let size = (self.text as NSString).sizeWithAttributes(dict) //Even stranger errors go here

      

xcode says:

'UIFont' is not a subtype of "NSDictionary"

Unable to convert expression type '[NSString: UIFont]' to type 'CGSize'

What changed from swift in beta 7 or 6 that it breaks the code?

+3


source to share


2 answers


Several method signatures with additional and optional properties were fixed in Beta 7 by converting implicitly expanded options to explicit options.

In your case, I am assuming the property text

was declared as String!

(implicitly expanded), whereas now it is String?

. So you have to expand it, implicitly:

let size = self.text!.sizeWithAttributes(dict)

      



or better, using optional binding:

    if let text = self.text {
        let size = text.sizeWithAttributes(dict)
    }

      

+6


source


Your function fitText

works fine for me.

In case it helps, here are a few things I usually do when I launch a new version of Xcode6 beta for the first time after installing it:



  • Double check under Xcode> Preferences> Location where latest command line tools are selected
  • Delete assembly and folders DerivedData li>
  • Restart Xcode
  • Build
0


source







All Articles