Swift compile error, subclassing NSValue, using super.init(nonretainedObject:) -
this code
class id<t: anyobject> : nsvalue { init(baseobject: t) { super.init(nonretainedobject: baseobject) } }
gives compiler error:
error: must call designated initializer of superclass 'nsvalue' super.init(nonretainedobject: baseobject) ^
how rid of this?
things thought of
i thought error might because nsvalue
initializer has anyobject?
type (note well: postfix ?
). tried various flavors of casting , [?!]
postfixing in places, , fixed nothing.
also, presumably nsvalue(nonretainedobject:)
must call designated initializer, right?
nsvalue(nonretainedobject:)
isn't designated initializer. initializer listed in nsvalue reference (and hence designated initializer) nsvalue(value:cconstvoidpointer, withobjctype type:cstring)
the other constructors convenience constructors derived class helper methods.
you might try:
init(baseobject: t) { super.init(bytes:&baseobject, withobjctype:"^v") }
"^v" type string returned nsvalue created valuewithnonretained....
unfortunately, i'm not coming appropriate way pass baseobject
cconstvoidpointer.
barring that, best thing can come wrap nsvalue instead of subclassing it:
class id<t:anyobject> { let wrapped:nsvalue init(baseobject:t) { wrapped = nsvalue(nonretainedobject:baseobject) } }
finally got work, it's little bit ugly, add convenience constructor wraps nonretainedobject constructor , use in subclass:
extension nsvalue { convenience init<t:anyobject>(unretained:t) { self.init(nonretainedobject:unretained) } } class id<t>:nsvalue { convenience init<t:anyobject>(unretained:t) { self.init(unretained:unretained) } }
depending on you're trying category alone might sufficient?
Comments
Post a Comment