Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCode 6.1 NSString.boundingRectWithSize Compile Error

Tags:

xcode

ios

swift

After I updated Xcode to 6.1 I get a couple of Compiler errors for my existing project.

I defined attributes like

let styleAttriutes = [
  NSFontAttributeName : UIFont(...),
  NSForgeroundColorAttributeName : UIColor.blackColor()
]

Error: "Could not find an overload for 'init' that accepts the supplied arguments"

So I declared the inferred Dictionary explicit adding : [NSString : Any] since all the key constants are of type NSString and we have different values: UIFont / UIColor and AnyObject is also not allowed.

let styleAttriutes : [NSString : Any] = [ ...

Now the compiler is happy in this point.

The defined attribues are used in

text.boundingRectWithSize(size: CGSize, options: NSStringDrawingOptions, attributes: [NSObject : AnyObject!], context: NSStringDrawingContext!)

Since attributes of type [NSObject:AnyObject!] are expected but I hand over attribues of type [NSString : Any] the compiler complains:

Error: "'NSString' is not identical to 'NSObject'"

I tried to cast it using attributes as [NSObject : AnyObject] but then I get runtime error.

Fatal Error: "Can't unsafeBitCast between types of differenz sizes".

Any suggestions?

like image 502
David Avatar asked May 20 '26 14:05

David


1 Answers

I guess you're using UIFont's init method which returns an optional (i.e. can return nil) since Xcode 6.1.

So try replacing UIFont(...) with UIFont(...)!.

like image 79
fluidsonic Avatar answered May 23 '26 03:05

fluidsonic