Skip to content

Instantly share code, notes, and snippets.

@markd2
Created August 13, 2012 15:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markd2/3341802 to your computer and use it in GitHub Desktop.
Save markd2/3341802 to your computer and use it in GitHub Desktop.
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