Skip to content

Instantly share code, notes, and snippets.

@0xced
Last active November 1, 2023 01:41
Show Gist options
  • Save 0xced/45daf79b62ad6a20be1c to your computer and use it in GitHub Desktop.
Save 0xced/45daf79b62ad6a20be1c to your computer and use it in GitHub Desktop.
Reverse engineered implementation of -[UIClassSwapper initWithCoder:]
@implementation UIClassSwapper
- (instancetype) initWithCoder:(UINibDecoder *)decoder
{
NSString *className = [decoder decodeObjectForKey:@"UIClassName"];
NSString *originalClassName = [decoder decodeObjectForKey:@"UIOriginalClassName"];
Class class = NSClassFromString(className);
Class originalClass = NSClassFromString(originalClassName);
if (!class) {
NSLog(@"Unknown class %@ in Interface Builder file.\n", className);
}
id object = [(class ?: originalClass) alloc];
[decoder replaceObject:self withObject:object];
if (originalClass != [UICustomObject class]) {
self = [object initWithCoder:decoder];
}
else {
self = [object init];
}
return [self autorelease];
}
@end
@getaaron
Copy link

@0xced Thanks a ton for posting this. It was very helpful.

@mikayelaghasyan
Copy link

@0xced Do you know of any way to make the custom object's UIOriginalClassName something other than UICustomObject and thus, have it initialized with initWithCoder: initializer?
Thanks beforehand!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment