Skip to content

Instantly share code, notes, and snippets.

@tjw
Last active December 2, 2015 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjw/e08865cfbb979605f27f to your computer and use it in GitHub Desktop.
Save tjw/e08865cfbb979605f27f to your computer and use it in GitHub Desktop.
Corruption when assigning to stored properties in classes using generics
import Cocoa
class A<T> : NSViewController {
var x:T? = nil
override init?(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
}
class B : A<NSString> {
override init?(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
}
let b = B(nibName:"bbb", bundle:NSBundle.mainBundle())!
print("b.nibName = \(b.nibName)")
b.x = "foo" as NSString
print("b.nibName = \(b.nibName)")
@tjw
Copy link
Author

tjw commented Dec 2, 2015

Updated to show more clearly that the write to the generic property overwrites an ivar on the superclass.

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