Overriding uiviewcontroller's view property with Swift

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Overriding uiviewcontroller's view property with Swift

Zipette
Hello,
I want to override my uiviewcontroller's view property. I found the right way to do it in ObjectiveC here http://travisjeffery.com/b/2012/12/overriding-uiviewcontrollers-view-property-done-right/ but how can I traduct this code in Swift ?

here is the code I would want to traduce in Swift :
// ViewController.h @interface ViewController : UIViewController @property (strong, nonatomic) UIScrollView *view; @end // ViewController.m @implementation ViewController @dynamic view; - (void)loadView { self.view = [[UIScrollView alloc] initWithFrame:UIScreen.mainScreen.applicationFrame]; }  
@end

Thanks a lot for your help
Vins

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/158964fc-31a6-48d2-80a1-72b849814751%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Overriding uiviewcontroller's view property with Swift

Jens Alfke
That Obj-C code doesn't look right. There's no need to override the 'view' property at all, since the superclass already defines it. Just set its value in the -loadView method. 

What Travis's example seems to do is exploit behavior that, at least in the current Obj-C compiler, lets you alter the class of an inherited property. I guess this lets you avoid type-casts, but IMHO it's a hack — I've been coding in Obj-C and Cocoa since 2000 and I've never seen anyone do this before; I have my doubts whether the language design even intended for this to be legal.

If you really want to do this in Swift, the equivalent of @dynamic is @NSManaged, but according to Apple's docs this is only for use in CoreData NSManagedObject subclasses, so I don't know if it works outside of that:

Like the @dynamic attribute in Objective-C, the @NSManaged attribute informs the Swift compiler that the storage and implementation of a property will be provided at runtime. However, unlike @dynamic, the @NSManaged attribute is available only for Core Data support.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/70C53352-C874-4B4D-ADDC-258984B4F423%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.