[swift-users] unsafeBitCast to Unimplemented Class

Saagar Jha saagar at saagarjha.com
Fri Feb 3 23:17:59 CST 2017


Hello,

I’m having an issue migrating some old Objective-C code that looks like this:

@implementation Foo

- (void)load {
	// Swizzle one of Bar’s methods to call Foo’s baz method
}

- (void)baz {
	[self baz];
	if ([self isKindOfClass:NSClassFromString(@“Bar”)]) {
		Bar *bar = (Bar *)self; // I can’t migrate this
		// work with bar
	}
}

@end

I’m trying to cast self to a Bar at runtime, and use it to call Bar’s methods. Sounds like an easy to task for unsafeBitCast, right? The issue is that I don’t have access to the implementation of Bar’s class at compile time (this is a plugin, so it’s loaded by another application which contains Bar). In Objective-C I can create a header and stick a dummy interface for Bar in it; the cast will work if Bar exists at runtime. However, in Swift, unsafeBitCast requires me to use Bar.self, which does not exist. Is there any way to get this cast to work?

Thanks,
Saagar Jha

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170203/b424de0a/attachment.html>


More information about the swift-users mailing list