3

This piece of code bellow used to work fine in Xcode 6 beta 5:

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'

Cannot convert the expression's type '$T6' to type 'UIFont'

When I split 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'

Cannot convert the expression's type '[NSString : UIFont]' to type 'CGSize'

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

Community
  • 1
  • 1
Hristo
  • 6,382
  • 4
  • 24
  • 38

2 Answers2

6

Several method signatures with optional and optional properties have been fixed in beta 7, by converting implicitly unwrapped optionals to explicit optionals.

In your case, I presume that the text property was declared as String! (implicitly unwrapped), whereas now it is a String? instead. As such, you have to unwrap it, either implicitly:

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

or better using optional binding:

    if let text = self.text {
        let size = text.sizeWithAttributes(dict)
    }
Antonio
  • 71,651
  • 11
  • 148
  • 165
  • Unwrapping made it work. Any idea what are these gibberish error messages though? – Hristo Sep 05 '14 at 13:59
  • Well... swift error messages have never been useful to figure out what the problem is. For example it happens it says a class doesn't conform to a protocol, but it does, or that a class doesn't a certain property, etc. I think there's still a lot of work still left on that subject that I hope will be fixed soon. – Antonio Sep 05 '14 at 14:04
  • 1
    As a general rule, when the error message doesn't make any sense, then it an erroneous usage of something else - but in several cases it's about optionals. But it can also be protocol without the @obj attribute, class properties initialized with references to other properties, usage of generics in bridged classes, etc. – Antonio Sep 05 '14 at 14:08
0

Your fitText function works just fine for me.

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

  1. Double check under Xcode > Preferences > Location that the latest command line tools are selected
  2. Delete build and DerivedData folders
  3. Restart Xcode
  4. Build
csch
  • 1,455
  • 14
  • 12