Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Trying to discern a mutable dictionary from an immutable one.
#import <Foundation/Foundation.h>
// clang -g -framework Foundation -o mutant mutant.m
int main (void) {
NSMutableDictionary *mutable = [[NSMutableDictionary alloc] init];
NSDictionary *immutable = [[NSDictionary alloc] init];
NSLog (@"mutable responds to set object for key: %d",
[mutable respondsToSelector: @selector(setObject:forKey:)]);
NSLog (@"immutable responds to set object for key: %d",
[immutable respondsToSelector: @selector(setObject:forKey:)]);
NSLog (@"mutable is kind of class NSDictionary: %d",
[mutable isKindOfClass: [NSDictionary class]]);
NSLog (@"immutable is kind of class NSDictionary: %d",
[immutable isKindOfClass: [NSDictionary class]]);
NSLog (@"mutable is kind of class NSMutableDictionary: %d",
[mutable isKindOfClass: [NSMutableDictionary class]]);
NSLog (@"immutable is kind of class NSMutableDictionary: %d",
[immutable isKindOfClass: [NSMutableDictionary class]]);
NSLog (@"mutable is member of class NSDictionary: %d",
[mutable isMemberOfClass: [NSDictionary class]]);
NSLog (@"immutable is member of class NSDictionary: %d",
[immutable isMemberOfClass: [NSDictionary class]]);
NSLog (@"mutable is member of class NSMutableDictionary: %d",
[mutable isMemberOfClass: [NSMutableDictionary class]]);
NSLog (@"immutable is member of class NSMutableDictionary: %d",
[immutable isMemberOfClass: [NSMutableDictionary class]]);
NSLog (@"mutable is of class %@", [mutable class]);
NSLog (@"immutable is of class %@", [immutable class]);
// Reach into the guts. Please don't do this in production code.
typedef struct RuntimeBaseHackBlorf {
uintptr_t _cfisa;
uint8_t _cfinfo[4];
} RuntimeBaseHackBlorf;
#define BIT_SIX (1 << 6)
#define BIT_SIX_SET(x) (((RuntimeBaseHackBlorf *)(x))->_cfinfo[0] & BIT_SIX)
NSLog (@"mutable has bit 6 set: %d", BIT_SIX_SET(mutable));
NSLog (@"immutable has bit 6 set: %d", BIT_SIX_SET(immutable));
// Check another Basic Hash
NSMutableSet *mutableSet = [NSMutableSet set];
NSSet *immutableSet = [NSSet set];
NSLog (@"mutable set has bit 6 set: %d", BIT_SIX_SET(mutableSet));
NSLog (@"immutable set has bit 6 set: %d", BIT_SIX_SET(immutableSet));
return 0;
} // main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment