Does -[NSObject autoContentAccessingProxy] work at all? - objective-c

I am trying to make use of -[NSObject autoContentAccessingProxy] as described at http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/autoContentAccessingProxy.
The object I am trying to proxy implements the NSDiscardableContent protocol and -autoContentAccessingProxy successfully returns a non-nil value.
If, however, I try to send a message to the proxy, I always get an NSInvalidArgumentException with a reason of "*** -[NSProxy methodSignatureForSelector:] called!".
I understand that if I was writing my own NSProxy-based class, I would have to implement the -methodSignatureForSelector: method, but in this case, I am not writing the proxy, just trying to use the proxy provided by the documented method. For what it's worth, I can see that the proxy is actually of type NSAutoContentAccessingProxy, so I would expect that that class would indeed have an implementation for -methodSignatureForSelector:.
Here is a small block of code using an NSPurgeableData instance instead of my custom class. This small block has exactly the same issue.
NSPurgeableData * data = [NSPurgeableData dataWithBytes:"123" length:3];
NSLog(#"data.length = %u", data.length);
id proxyData = [data autoContentAccessingProxy];
NSLog(#"proxyData.length = %u", [proxyData length]); // throws NSInvalidArgumentException!
[data endContentAccess];
[data release];
Do I have some misunderstanding of the -autoContentAccessingProxy method here, or is it just completely broken?

You can fix this bug by reimplementing what the NSAutoContentAccessingProxy class does but without the bugs. I have written such a class: XCDAutoContentAccessingProxy. The autoContentAccessingProxy method is replaced before your main function is called; this happens in the +load method. So all you have to do is compile the following code in your application and autoContentAccessingProxy will behave as expected.
Note that unlike my previous answer, you can actually use this solution in a shipping application.
#if !__has_feature(objc_arc)
#error This code must be compiled with Automatic Reference Counting (CLANG_ENABLE_OBJC_ARC / -fobjc-arc)
#endif
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#interface XCDAutoContentAccessingProxy : NSProxy
+ (XCDAutoContentAccessingProxy *) proxyWithTarget:(id)target;
#property (nonatomic, strong) id target;
#end
#implementation XCDAutoContentAccessingProxy
#synthesize target = _target;
static id autoContentAccessingProxy(id self, SEL _cmd)
{
return [XCDAutoContentAccessingProxy proxyWithTarget:self];
}
+ (void) load
{
method_setImplementation(class_getInstanceMethod([NSObject class], #selector(autoContentAccessingProxy)), (IMP)autoContentAccessingProxy);
}
+ (XCDAutoContentAccessingProxy *) proxyWithTarget:(id)target
{
if (![target conformsToProtocol:#protocol(NSDiscardableContent)])
return nil;
if (![target beginContentAccess])
return nil;
XCDAutoContentAccessingProxy *proxy = [self alloc];
proxy.target = target;
return proxy;
}
- (void) dealloc
{
[self.target endContentAccess];
}
- (void) finalize
{
[self.target endContentAccess];
[super finalize];
}
- (id) forwardingTargetForSelector:(SEL)selector
{
return self.target;
}
- (NSMethodSignature *) methodSignatureForSelector:(SEL)selector
{
return [self.target methodSignatureForSelector:selector];
}
- (void) forwardInvocation:(NSInvocation *)invocation
{
[invocation setTarget:self.target];
[invocation invoke];
}
#end
UPDATE This bug is fixed on OS X 10.8. According to OS X Mountain Lion Release Notes:
Prior to Mac OS 10.8, -[NSObject autoContentAccessingProxy] returned an object that did not properly implement message forwarding. This proxy now behaves correctly on Mac OS 10.8.
So you need to compile the above code only if you are targeting OS X 10.7 or earlier.

You are absolutely right, -autoContentAccessingProxy is totally broken. NSAutoContentAccessingProxy is a subclass of NSProxy and should therefore implement the methodSignatureForSelector: and forwardInvocation: methods or the forwardingTargetForSelector: method if running on iOS 4 or greater.
Here is a hardcore way of fixing the NSAutoContentAccessingProxy class by adding the methodSignatureForSelector: and forwardInvocation: methods at runtime. Just add the following to your project (do not compile this with ARC).
#import <mach-o/dyld.h>
#import <mach-o/nlist.h>
__attribute__((constructor)) void FixAutoContentAccessingProxy(void);
static id _target(id autoContentAccessingProxy);
static NSMethodSignature *NSAutoContentAccessingProxy_methodSignatureForSelector(id self, SEL _cmd, SEL selector);
static void NSAutoContentAccessingProxy_forwardInvocation(id self, SEL _cmd, NSInvocation *invocation);
__attribute__((constructor)) void FixAutoContentAccessingProxy(void)
{
Class NSAutoContentAccessingProxy = objc_lookUpClass("NSAutoContentAccessingProxy");
Method methodSignatureForSelector = class_getInstanceMethod([NSObject class], #selector(methodSignatureForSelector:));
Method forwardInvocation = class_getInstanceMethod([NSObject class], #selector(forwardInvocation:));
class_addMethod(NSAutoContentAccessingProxy, #selector(methodSignatureForSelector:), (IMP)NSAutoContentAccessingProxy_methodSignatureForSelector, method_getTypeEncoding(methodSignatureForSelector));
class_addMethod(NSAutoContentAccessingProxy, #selector(forwardInvocation:), (IMP)NSAutoContentAccessingProxy_forwardInvocation, method_getTypeEncoding(forwardInvocation));
}
static id _target(id autoContentAccessingProxy)
{
static uint32_t targetIvarOffset;
static dispatch_once_t once;
dispatch_once(&once, ^{
struct nlist symlist[] = {{"_OBJC_IVAR_$_NSAutoContentAccessingProxy._target", 0, 0, 0, 0}, NULL};
for(uint32_t i = 0; i < _dyld_image_count(); i++)
{
if (nlist(_dyld_get_image_name(i), symlist) == 0 && symlist[0].n_value != 0)
{
uint32_t *_OBJC_IVAR_NSAutoContentAccessingProxy_target = (uint32_t*)((uint32_t)_dyld_get_image_header(i) + symlist[0].n_value);
targetIvarOffset = *_OBJC_IVAR_NSAutoContentAccessingProxy_target;
break;
}
}
});
return *(id*)((uint32_t)autoContentAccessingProxy + targetIvarOffset);
}
static NSMethodSignature *NSAutoContentAccessingProxy_methodSignatureForSelector(id self, SEL _cmd, SEL selector)
{
return [_target(self) methodSignatureForSelector:selector];
}
static void NSAutoContentAccessingProxy_forwardInvocation(id self, SEL _cmd, NSInvocation *invocation)
{
[invocation setTarget:_target(self)];
[invocation invoke];
}
This workaround should be used only to demonstrate how NSAutoContentAccessingProxy is broken. Anyway this will only work on the simulator because the nlist call will fail on the device. You could actually make it work on the device by using APEFindSymbol from APELite-arm instead of nlist but I don't recommend it.
You should definitely file a bug report about it to Apple.

Related

In a macOS Objective-C application, I have subclassed NSMutableSet for enforcing an equality different from isEqual. Is my implementation fine?

In my macOS Objective-C application, I have created a subclass of NSMutableSet. What I want to achieve is a NSMutableSet that does not use isEqual: as the comparing strategy. Specifically, The set will contain objects of type NSRunningApplication, and I want the set to work based on the equality of the objects bundle identifiers. Following is my implementation:
Header file:
#import <Cocoa/Cocoa.h>
NS_ASSUME_NONNULL_BEGIN
#interface BundleIdentifierAwareMutableSet : NSMutableSet
#property (atomic, strong) NSMutableSet *backStorageMutableSet;
#property (atomic, strong) NSMutableArray *backStorageMutableArray;
#end
NS_ASSUME_NONNULL_END
Implementation file:
#import "BundleIdentifierAwareMutableSet.h"
#implementation BundleIdentifierAwareMutableSet
#synthesize backStorageMutableSet;
- (instancetype)init {
self = [super init];
if (self) {
self.backStorageMutableSet = [[NSMutableSet alloc] init];
self.backStorageMutableArray = [[NSMutableArray alloc] init];
}
return self;
}
- (NSUInteger)count {
return [self.backStorageMutableArray count];
}
- (NSRunningApplication *)member:(NSRunningApplication *)object {
__block NSRunningApplication *returnValue = nil;
[self.backStorageMutableArray enumerateObjectsUsingBlock:^(NSRunningApplication * _Nonnull app, NSUInteger __unused idx, BOOL * _Nonnull stop) {
if ([app.bundleIdentifier isEqualToString:[object bundleIdentifier]]) {
returnValue = app;
if (![app isEqual:object]) {
NSLog(#"An ordinary set would have not considered the two objects equal.");
}
*stop = YES;
}
}];
return returnValue;
}
- (NSEnumerator *)objectEnumerator {
self.backStorageMutableSet = [NSMutableSet setWithArray:self.backStorageMutableArray];
return [self.backStorageMutableSet objectEnumerator];
}
- (void)addObject:(NSRunningApplication *)object {
NSRunningApplication *app = [self member:object];
if (app == nil) {
[self.backStorageMutableArray addObject:object];
}
}
- (void)removeObject:(NSRunningApplication *)object {
NSArray *snapShot = [self.backStorageMutableArray copy];
[snapShot enumerateObjectsUsingBlock:^(NSRunningApplication * _Nonnull currentApp, NSUInteger __unused idx, BOOL * _Nonnull __unused stop) {
if ([[currentApp bundleIdentifier] isEqualToString:[object bundleIdentifier]]) {
[self.backStorageMutableArray removeObject:currentApp];
if (![currentApp isEqual:object]) {
NSLog(#"An ordinary set would have not considered the two objects equal.");
}
}
}];
}
This seems to work, and indeed, When applicable, Xcode logs that an ordinary NSMutableSet would have not considered two members equal. I would like to bring this implementation to the Production App, but I am afraid I have not considered something important, since this is the first time I subclass NSMutableSet. For example, I am worried about the following method:
- (NSEnumerator *)objectEnumerator {
self.backStorageMutableSet = [NSMutableSet setWithArray:self.backStorageMutableArray];
return [self.backStorageMutableSet objectEnumerator];
}
This is the only use I do of the backStorageMutableSet since the rest is backed to the array. Is this fine or can bring troubles ? Will other parts of the subclass bring problems ? Any help will be greatly appreciated. Thanks
Don't do this. Subclassing collections should be the last resort. It can have implications on performance, ... Try to use highest possible abstraction and go down if it doesn't work for you for some reason.
Wrapper object
Wrap the NSRunningApplication in another object and provide your own hash & isEqual: methods.
Application.h:
#interface Application: NSObject
#property (nonatomic, strong, readonly, nonnull) NSRunningApplication *application;
#end
Application.m:
#interface Application ()
#property (nonatomic, strong, nonnull) NSRunningApplication *application;
#end
#implementation Application
- (nonnull instancetype)initWithRunningApplication:(NSRunningApplication *_Nonnull)application {
if ((self = [super init]) == nil) {
// https://developer.apple.com/documentation/objectivec/nsobject/1418641-init?language=objc
//
// The init() method defined in the NSObject class does no initialization; it simply
// returns self. In terms of nullability, callers can assume that the NSObject
// implementation of init() does not return nil.
return nil;
}
self.application = application;
return self;
}
// https://developer.apple.com/documentation/objectivec/1418956-nsobject/1418795-isequal?language=objc
- (BOOL)isEqual:(id)object {
if (![object isKindOfClass:[Application class]]) {
return NO;
}
Application *app = (Application *)object;
return [self.application.bundleIdentifier isEqualToString:app.application.bundleIdentifier];
}
// https://developer.apple.com/documentation/objectivec/1418956-nsobject/1418859-hash?language=objc
- (NSUInteger)hash {
return self.application.bundleIdentifier.hash;
}
#end
Toll-free bridging & CFMutableSetRef
CFSet is bridged with the NSSet, CFMutableSet is bridged with the NSMutableSet, etc. It means that you can create a set via Core Foundation
API and then use it as NSSet for example. Core Foundation is a powerful
framework which exposes more stuff to you.
You can provide a custom set of callbacks for the CFSet.
/*!
#typedef CFSetCallBacks
Structure containing the callbacks of a CFSet.
#field version The version number of the structure type being passed
in as a parameter to the CFSet creation functions. This
structure is version 0.
#field retain The callback used to add a retain for the set on
values as they are put into the set. This callback returns
the value to store in the set, which is usually the value
parameter passed to this callback, but may be a different
value if a different value should be stored in the set.
The set's allocator is passed as the first argument.
#field release The callback used to remove a retain previously added
for the set from values as they are removed from the
set. The set's allocator is passed as the first
argument.
#field copyDescription The callback used to create a descriptive
string representation of each value in the set. This is
used by the CFCopyDescription() function.
#field equal The callback used to compare values in the set for
equality for some operations.
#field hash The callback used to compare values in the set for
uniqueness for some operations.
*/
typedef struct {
CFIndex version;
CFSetRetainCallBack retain;
CFSetReleaseCallBack release;
CFSetCopyDescriptionCallBack copyDescription;
CFSetEqualCallBack equal;
CFSetHashCallBack hash;
} CFSetCallBacks;
There're predefined sets of callbacks like:
/*!
#constant kCFTypeSetCallBacks
Predefined CFSetCallBacks structure containing a set of callbacks
appropriate for use when the values in a CFSet are all CFTypes.
*/
CF_EXPORT
const CFSetCallBacks kCFTypeSetCallBacks;
Which means that you're not forced to provide all of them, but you're free to modify just some of them. Let's prepare two callback functions:
// typedef CFHashCode (*CFSetHashCallBack)(const void *value);
CFHashCode runningApplicationBundleIdentifierHash(const void *value) {
NSRunningApplication *application = (__bridge NSRunningApplication *)value;
return [application.bundleIdentifier hash];
}
// typedef Boolean (*CFSetEqualCallBack)(const void *value1, const void *value2);
Boolean runningApplicationBundleIdentifierEqual(const void *value1, const void *value2) {
NSRunningApplication *application1 = (__bridge NSRunningApplication *)value1;
NSRunningApplication *application2 = (__bridge NSRunningApplication *)value2;
return [application1.bundleIdentifier isEqualToString:application2.bundleIdentifier];
}
You can use them in this way:
- (NSMutableSet<NSRunningApplication *> *_Nullable)bundleIdentifierAwareMutableSetWithCapacity:(NSUInteger)capacity {
// > Predefined CFSetCallBacks structure containing a set of callbacks
// > appropriate for use when the values in a CFSet are all CFTypes.
//
// Which means that you shouldn't bother about retain, release, ... callbacks,
// they're already set.
//
// CFSetCallbacks can be on stack, because this structure is copied in the
// CFSetCreateMutable function.
CFSetCallBacks callbacks = kCFTypeSetCallBacks;
// Overwrite just the hash & equal callbacks
callbacks.hash = runningApplicationBundleIdentifierHash;
callbacks.equal = runningApplicationBundleIdentifierEqual;
// Try to create a mutable set.
CFMutableSetRef set = CFSetCreateMutable(kCFAllocatorDefault, capacity, &callbacks);
if (set == NULL) {
// Failed, do some error handling or just return nil
return nil;
}
// Transfer the ownership to the Obj-C & ARC => no need to call CFRelease
return (__bridge_transfer NSMutableSet *)set;
}
&
NSMutableSet<NSRunningApplication *> *set = [self bundleIdentifierAwareMutableSetWithCapacity:50];
[set addObjectsFromArray:[[NSWorkspace sharedWorkspace] runningApplications]];
NSLog(#"%#", set);

How do you call a singleton class method from Objective C in a Swift class?

I have the following objective C code which I need to get into a Swift class :
In Logger.m -
+ (Logger *) log
{
static Logger *sharedLog = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedLogger = [[self alloc] init];
});
return sharedLogger;
}
- (void) printString:(NSString *) s
{
NSLog(s)
}
Which is in Logger.h as -
+ (Logger *) log;
- (void) printString:(NSString *) s;
Now, I have this code bridged in to Swift Project - LoggerUserApp where I'm trying to use the above print method from the singleton log shared class method.
I've tried -
Logger.log().printString("String") // Compiler Error. Use Object Construction Logger()
Logger().log().printString("String") // Compiler Error.
Logger().log.printString("String") // Compiler Error.
Logger.printString("String") // This does not call log
Can someone tell me what might be going wrong?
If the Swift compiler mistakenly identifies a method as a class factory method, you can use the NS_SWIFT_NAME macro, passing the Swift signature of the method to have it imported correctly. For example:
+ (id)recordWithQuality:(double)quality NS_SWIFT_NAME(record(quality:));
so,your method should be this:
+ (Logger *) log NS_SWIFT_NAME(log());
I can't reproduce your example completely, but at first sight, this should work:
Logger.log().printString("String")
Since your Obj-C singleton is a function returning the singleton (hence Logger.log() will return your singleton.
But since in your example code I see Logger and then PALog I can't say if this is all you need.
There are some other errors in your example: sharedLog and than sharedLogger.
Here is an example that should work:
Logger.h
#interface Logger : NSObject
+ (Logger *) log;
- (void) printString:(NSString *) s;
#end
Logger.m
#import "Logger.h"
#implementation Logger
+ (Logger *) log
{
static Logger *sharedLog = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedLog = [[self alloc] init];
});
return sharedLog;
}
- (void) printString:(NSString *) s
{
NSLog(#"%#", s);
}
#end
...-Bridging-Header.h
#import "Logger.h"
ViewController.swift
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Logger.log().printString("Hello")
}
}
In the Swift update, if the class method's name is similar to the class name, then it just takes that as a custom initializer. Now since there was no parameter in the method, it wouldn't come up as the default initializer would come up instead.
Hence, to solve this problem, I had to change the name of the class method log to something different like newLog... and it worked fine.
Logger.newLog().printString("")

Programmatically creating new instance method to track message sending

I want to design a class (TrackingClass) that would be in charge of tracking the calls to some methods of an other class (TrackedClass), i.e. of setting up the method swizzling from what I understood.
So let's say I load up an array with #selectors of the instance methods of TrackedClass i'm interested in.
Here is the pseudo-code I would like to run :
#implementation BCTrackedClass
-(void)doA
{
}
#end
and
#implementation BCTrackingClass
#import "BCTrackingClass.h"
#import "BCTrackedClass.h"
#include <objc/runtime.h>
#include <objc/objc-runtime.h>
#implementation BCTrackingClass
void myMethodIMP(id self, SEL _cmd);
void myMethodIMP(id self, SEL _cmd)
{
//NSLog(#"_cmd : %#",NSStringFromSelector(_cmd));
[BCTrackingClass logCallForMethod:NSStringFromSelector(_cmd)];
objc_msgSend(self,
NSSelectorFromString([NSString stringWithFormat:#"tracked%#",NSStringFromSelector(_cmd)]));
}
+(void)setUpTrackingForClass:(Class)aClass andMethodArray:(NSArray*)anArray //Array of selectorsStrings of methods to track
{
for (NSString* selectorString in anArray)
{
SEL selector = NSSelectorFromString(selectorString);
SEL trackedSelector = NSSelectorFromString([NSString stringWithFormat:#"tracked%#",selectorString]);
class_addMethod(aClass,
trackedSelector,
(IMP) myMethodIMP, "v#:");
//Swizzle the original method with the tracked one
Method original = class_getInstanceMethod(aClass,
selector);
Method swizzled = class_getInstanceMethod(aClass,
trackedSelector);
method_exchangeImplementations(original, swizzled);
}
}
+(void)logCallForMethod:(NSString*)aSelectorString
{
NSLog(#"%#",aSelectorString);
}
#end
Theoretically, I'm just missing the bit of code where I could effectively create this new instance method trackedSelector. Can I achieve that ?
Edit
I updated the code with some new piece of information, am I getting closer ?
Edit 2
I set up a Github repository with a Demo application if people want to dynamically try out their ideas.
Source : BCTrackingClass on Github
Edit 3
I finally come up with a working version of the code (cf Github repo, or just above). My next problem is : I want my class to be instance based (currently, all my methods are class methods), so that I can assign a property #property NSMutableDictionnary* to instances of the class for call logging.
I'm not sure how to achieve that. Any ides ?
Do you want to do it for all instances of all objects of that class?
for some selectors or all of them?
...
If what you want is to track specific instances, then the simplest route is to use isa swizzling, doing that, more or less (the code is absolutely untested)
#interface ClassTracker
+ (void)trackObject:(id)object;
#end
static const char key;
#implementation ClassTracker
+ (void)trackObject:(id)object
{
objc_setAssociatedObject(object, &key, [object class], OBJC_ASSOCIATION_ASSIGN);
object_setClass(object, [ClassTracker class]);
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
Class aClass = objc_getAssociatedObject(self, &key);
return [aClass instanceMethodSignatureForSelector:sel];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
Class aClass = objc_getAssociatedObject(self, &key);
// do your tracing here
object_setClass(self, aClass);
[invocation invoke];
object_setClass(self, [ClassTracker class]);
}
// dealloc is magical in the sense that you really want to undo your hooking
// and not resume it ever!
- (void)dealloc
{
Class aClass = objc_getAssociatedObject(self, &key);
object_setClass(self, aClass);
[self dealloc];
}
#end
If it's used for reverse engineering or debug purposes, that should (with minor adaptations) do the trick.
If you intend that to be fast, then you have to do instance method swizzling, knowing their type and so forth.
My "solution" has the drawback that it will only trace entering calls, IOW if a selector calls other ones, since the isa swizzling is paused to recurse the call, then you don't see the new ones until you restore the isa swizzling.
There may be a way to forward the invocation to the original class, without undoing isa swizzling, but I reckon I was too lazy to search for it.

Method Swizzling isEqualToString

I'm running into some odd behavior when trying to Method Swizzle isEqualToString: on the NSString class. Here is the code in question:
#import <Foundation/Foundation.h>
#import <objc/objc-runtime.h>
#interface NSString (SwizzleString)
- (BOOL) custom_isEqualToString:(NSString *)aString;
- (NSRange)custom_rangeOfString:(NSString *)aString;
#end
#implementation NSString (SwizzleString)
- (BOOL) custom_isEqualToString:(NSString *)aString;
{
NSLog(#"Inside custom_isEqualToString method definition");
return [self custom_isEqualToString:aString];
}
- (NSRange)custom_rangeOfString:(NSString *)aString;
{
NSLog(#"Inside custom_rangeOfString method definition");
return [self custom_rangeOfString:aString];
}
#end
int main(int argc, const char * argv[])
{
Method m1, m2;
m1 = class_getInstanceMethod([NSString class], #selector(isEqualToString:));
m2 = class_getInstanceMethod([NSString class], #selector(custom_isEqualToString:));
method_exchangeImplementations(m1, m2);
m1 = class_getInstanceMethod([NSString class], #selector(rangeOfString:));
m2 = class_getInstanceMethod([NSString class], #selector(custom_rangeOfString:));
method_exchangeImplementations(m1, m2);
NSString *foo = #"Foo";
// Does not log anything, is still using isEqualToString: implementation
[foo isEqualToString:#"Foo"];
// Also does not log anything, since it is using the method implementation from isEqualToString:
[foo custom_isEqualToString:#"Foo"];
// Does log something because rangeOfString now uses custom_rangeOfString IMP
[foo rangeOfString:#"Foo"];
// Does not log anything because it uses the method implementation from rangeOfString:
[foo custom_rangeOfString:#"Foo"];
}
isEqualToString: and rangeOfString: are both defined in a category on NSString called (NSStringExtensionMethods), so I included the rangeOfString swizzle to show that I'm swizzling methods correctly and specifically an NSString object successfully so I could eliminate questions about class cluster problems.
When I produce the assembly for the code above, instead of seeing normal objc_msgSend calls I instead see stuff like l_objc_msgSend_fixup_isEqualToString_. This led me to finding out more about the objective-c vtable, in which it seems isEqualToString: can be found:
static const char * const defaultVtable[] = {
"allocWithZone:",
"alloc",
"class",
"self",
"isKindOfClass:",
"respondsToSelector:",
"isFlipped",
"length",
"objectForKey:",
"count",
"objectAtIndex:",
"isEqualToString:",
"isEqual:",
"retain",
"release",
"autorelease",
};
I've been digging through the objective-c source and the internet all day on how it would be possible to somehow still be able to swizzle isEqualToString:.
As you're aware, this is a class cluster. You need the actual class, not the public class, so just ask the string object that you have:
NSString *foo = #"Foo";
m1 = class_getInstanceMethod([foo class], #selector(isEqualToString:));
m2 = class_getInstanceMethod([foo class], #selector(custom_isEqualToString:));
method_exchangeImplementations(m1, m2);
There's also the fragile alternative of using the class name itself:
NSClassFromString(#"__NSCFConstantString")
NSClassFromString(#"__NSCFString")

How to simplify callback logic with a Block?

Let's say I need to communicate with a class that provides a protocol and calls delegate methods when an operation is complete, as so:
#protocol SomeObjectDelegate
#required
- (void)stuffDone:(id)anObject;
- (void)stuffFailed;
#end
#interface SomeObject : NSObject
{
}
#end
Now, I've decided that while I could make another class implement the stuffDone: delegate method, I've decided that I'd rather encapsulate the process into a block which is written somewhere close to where SomeObject is instantiated, called, etc. How might I do this? Or in other words, if you look at this famous article on blocks (in the Replace Callbacks section); how might I write a method in SomeObject that accepts a completionHandler: of sorts?
It sounds like you wish to communicate with an existing class which is designed to take a delegate object. There are a number of approaches, including:
using a category to add block-based variants of the appropriate methods;
use a derived class to add the block-based variants; and
write a class which implements the protocol and calls your blocks.
Here is one way to do (3). First let's assume your SomeObject is:
#protocol SomeObjectDelegate
#required
- (void)stuffDone:(id)anObject;
- (void)stuffFailed;
#end
#interface SomeObject : NSObject
{
}
+ (void) testCallback:(id<SomeObjectDelegate>)delegate;
#end
#implementation SomeObject
+ (void) testCallback:(id<SomeObjectDelegate>)delegate
{
[delegate stuffDone:[NSNumber numberWithInt:42]];
[delegate stuffFailed];
}
#end
so we have some way to test - you will have a real SomeObject.
Now define a class which implements the protocol and calls your supplied blocks:
#import "SomeObject.h"
typedef void (^StuffDoneBlock)(id anObject);
typedef void (^StuffFailedBlock)();
#interface SomeObjectBlockDelegate : NSObject<SomeObjectDelegate>
{
StuffDoneBlock stuffDoneCallback;
StuffFailedBlock stuffFailedCallback;
}
- (id) initWithOnDone:(StuffDoneBlock)done andOnFail:(StuffFailedBlock)fail;
- (void)dealloc;
+ (SomeObjectBlockDelegate *) someObjectBlockDelegateWithOnDone:(StuffDoneBlock)done andOnFail:(StuffFailedBlock)fail;
// protocol
- (void)stuffDone:(id)anObject;
- (void)stuffFailed;
#end
This class saves the blocks you pass in and calls them in response to the protocol callbacks. The implementation is straightforward:
#implementation SomeObjectBlockDelegate
- (id) initWithOnDone:(StuffDoneBlock)done andOnFail:(StuffFailedBlock)fail
{
if (self = [super init])
{
// copy blocks onto heap
stuffDoneCallback = Block_copy(done);
stuffFailedCallback = Block_copy(fail);
}
return self;
}
- (void)dealloc
{
Block_release(stuffDoneCallback);
Block_release(stuffFailedCallback);
[super dealloc];
}
+ (SomeObjectBlockDelegate *) someObjectBlockDelegateWithOnDone:(StuffDoneBlock)done andOnFail:(StuffFailedBlock)fail
{
return (SomeObjectBlockDelegate *)[[[SomeObjectBlockDelegate alloc] initWithOnDone:done andOnFail:fail] autorelease];
}
// protocol
- (void)stuffDone:(id)anObject
{
stuffDoneCallback(anObject);
}
- (void)stuffFailed
{
stuffFailedCallback();
}
#end
The only thing you need to remember is to Block_copy() the blocks when initializing and to Block_release() them later - this is because blocks are stack allocated and your object may outlive its creating stack frame; Block_copy() creates a copy in the heap.
Now you can all a delegate-based method passing it blocks:
[SomeObject testCallback:[SomeObjectBlockDelegate
someObjectBlockDelegateWithOnDone:^(id anObject) { NSLog(#"Done: %#", anObject); }
andOnFail:^{ NSLog(#"Failed"); }
]
];
You can use this technique to wrap blocks for any protocol.
ARC Addendum
In response to the comment: to make this ARC compatible just remove the calls to Block_copy() leaving direct assignments:
stuffDoneCallback = done;
stuffFailedCallback = fail;
and remove the dealloc method. You can also change Blockcopy to copy, i.e. stuffDoneCallback = [done copy];, and this is what you might assume is needed from reading the ARC documentation. However it is not as the assignment is to a strong variable which causes ARC to retain the assigned value - and retaining a stack block copies it to the heap. Therefore the ARC code generated produces the same results with or without the copy.
You could do something like this:
typedef void (^AZCallback)(NSError *);
AZCallback callback = ^(NSError *error) {
if (error == nil) {
NSLog(#"succeeded!");
} else {
NSLog(#"failed: %#", error);
}
};
SomeObject *o = [[SomeObject alloc] init];
[o setCallback:callback]; // you *MUST* -copy the block
[o doStuff];
...etc;
Then inside SomeObject, you could do:
if ([self hadError]) {
callback([self error]);
} else {
callback(nil);
}
The below link explains how the call backs using delegates could be easily replaced with blocks.
The examples includes UITableview,UIAlertview and ModalViewController.
click me
Hope this helps.