My company wants to be insure that if my Objective-C to Swift conversions fail in anyway, that the app can revert to using the older Objective-C code. By using a remotely controllable flag, the app can switch which code runs as, both are compiled into the app.
Essentially, I create a protocol that describes the original class, then both classes (with a "s" or "o" appended to them) conform to the protocol.
Protocol: Object Objective-C class: oObject Swift class: sObject
That said, I hit one issue that I just can't seem reason out. I create a Objective-C function that returns the appropriate class:
Class<Object> classObject(void) {
if (myFlag) {
return [sObject class];
} else {
return [oObject class];
}
}
Swift deals with this really well - I can create an initialized object using:
let object = classObject().init()
but I cannot find a way to do this in Objective-C:
Object *object = [[classSalesForceData() alloc] init];
fails with "No known class method for selector 'alloc'"
Is there a way to do this?
David
PS: my workaround is to return an allocated object:
Object *createObject(void) {
if (myFlag) {
return [sObject alloc];
} else {
return [oObject alloc];
}
}
Yeah, that is a bit weird. I’m not even sure I understand what Class<MyProtocol>
means in Objective-C. Remember that classes are objects, so does it mean that the class instances conform to the protocol? Or that the class itself conforms to the protocol? *shrug*
Regardless, I don’t think you need to solve that problem. Standard practice is to pass around Class
and then cast the result object to id<MyProtocol>
, and I don’t see any reason to deviate from that practice.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"