swift - How to pass a class and method to create an instance of a class? -
this question has reply here:
swift class introspection & generics 6 answersi started learning apple swift can not deal problem, have methods help objective-c please rewrite on apple swift
- (void)setclass:(class)aclass { nsobject *object = [[aclass alloc] init]; }
call method (user inherited nsobject):
[self setclass:[user class]];
how repeat such actions on apple swift? give thanks you!
here article instantiating classes name in swift. problem solved creating objective-c class method
+ (id)create:(nsstring *)classname { homecoming [nsclassfromstring(classname) new]; }
and calling swift. source code on github: https://github.com/ijoshsmith/swift-factory
update:
here simpler solution:
var clazz: nsobject.type = testobject.self var instance : nsobject = clazz() if allow testobject = instance as? testobject { println("yes!") }
of course, class must subclass of nsobject.
your function be:
func setclass (myclass: nsobject.type){ var object = myclass() }
swift
No comments:
Post a Comment