Skip to content

Instantly share code, notes, and snippets.

@jerryhjones
Last active August 29, 2015 14:02
Show Gist options
  • Save jerryhjones/bb16672136dcb2089a9f to your computer and use it in GitHub Desktop.
Save jerryhjones/bb16672136dcb2089a9f to your computer and use it in GitHub Desktop.

#Broken Rendering Bad swift code that (weirdly) compiles fine, seems to break IB live rendering.

Zipped framework and application examples below.

http://cl.ly/1l1a353F2b0P

//
// 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)")
@mpurland
Copy link

mpurland commented Jun 9, 2014

You can do this here:

bar = Float(soConfused())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment