#Broken Rendering Bad swift code that (weirdly) compiles fine, seems to break IB live rendering.
Zipped framework and application examples below.
#Broken Rendering Bad swift code that (weirdly) compiles fine, seems to break IB live rendering.
Zipped framework and application examples below.
| // | |
| // AppDelegate.swift | |
| // BrokenRenderingApp | |
| // | |
| // Created by Jerry Jones on 6/7/14. | |
| // Copyright (c) 2014 Spaceman Labs, Inc. All rights reserved. | |
| // | |
| import UIKit | |
| @UIApplicationMain | |
| class AppDelegate: UIResponder, UIApplicationDelegate { | |
| var window: UIWindow? | |
| func soConfused() -> CGFloat | |
| { | |
| return 0.0 | |
| } | |
| func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { | |
| // Unlike the playground, this code all executes fine | |
| var foo: CGFloat = 0.0 | |
| var bar: Float = 0.0 | |
| foo = soConfused() | |
| bar = soConfused() | |
| println("test \(foo)") | |
| println("test \(bar)") | |
| return true | |
| } | |
| } |
| // | |
| // BrokenView.swift | |
| // BrokenRenderingFramework | |
| // | |
| // Created by Jerry Jones on 6/7/14. | |
| // Copyright (c) 2014 Spaceman Labs, Inc. All rights reserved. | |
| // | |
| import UIKit | |
| @IBDesignable | |
| class BrokenView: UIView { | |
| @IBInspectable var borderColor: UIColor = UIColor.blueColor() | |
| init(frame: CGRect) { | |
| super.init(frame: frame) | |
| // Initialization code | |
| } | |
| override func drawRect(rect: CGRect) | |
| { | |
| let context = UIGraphicsGetCurrentContext() | |
| var frame = self.frame | |
| CGContextSetLineWidth(context, 10.0) | |
| CGRectInset(frame, 5.0, 5.0) | |
| borderColor.set() | |
| UIRectFrame(frame) | |
| // This same code compiles fine, but breaks live rendering in IB | |
| var foo: CGFloat = 0.0 | |
| var bar: Float = 0.0 | |
| foo = soConfused() | |
| bar = soConfused() | |
| println("test \(foo)") | |
| println("test \(bar)") | |
| } | |
| func soConfused() -> Float | |
| { | |
| return 0.0 | |
| } | |
| } |
| // Playground - noun: a place where people can play | |
| import UIKit | |
| func soConfused() -> CGFloat | |
| { | |
| return 0.0 | |
| } | |
| var foo: CGFloat = 0.0 | |
| var bar: Float = 0.0 | |
| foo = soConfused() | |
| bar = soConfused() // Fails here: 'NSNumber' is not a subtype of 'Float' | |
| println("test \(foo)") | |
| println("test \(bar)") |
You can do this here:
bar = Float(soConfused())