1

I want to provide methods used in several view controllers called in my delegate methods.

For example, I have some CloudKit functionality (I've added this to my own framework, but I don't think thats important), where I want to provide some crash logging. Previosuly I had a crashLog function in each of my view controllers, which worked fine, but I have a lot of duplicate code.

Therefore I'd like to produce a category with these methods instead.

However I'm having difficulty getting my delegate methods to see these category methods.

Here's my code..

UIViewController+CloudKitDelegates.h

@interface UIViewController (CloudKitDelegates) <iCloudDBDelegate>

@property (weak,nonatomic) id<iCloudDBDelegate>iCloudDBDelegate;

-(void)crashLog:(NSString*)message, ...;

@end

UIViewController+CloudKitDelegates.m

#import "UIViewController+CloudKitDelegates.h"

@implementation UIViewController (CloudKitDelegates)
@dynamic iCloudDBDelegate;

-(void)crashLog:(NSString*)message, ...
{
    va_list args;
    va_start(args, message);

    NSLog(@"%@", [[NSString alloc] initWithFormat:message arguments:args]);

    va_end(args);
}

@end

h file - my calling view controller (e.g. My View Controller)

#import "UIViewController+CloudKitDelegates.h"

m file - delegate method

-(NSString*)getDBPath
{
    [self.iCloudDBDelegate crashLog: @"testing"];

From this call I'm getting an error ...

'NSInvalidArgumentException', reason: '-[MyViewController crashLog:]: 
    unrecognized selector sent to instance

The error is showing that my calling view controller called MyViewController doesn't have the crashLog method, which I have in my category.

Any ideas where I'm going wrong ?

BillThomas
  • 55
  • 7
  • 1
    Is the category in a framework? That's the usual cause of this kind of problem. Alternately have you ensured that `UIViewController+CloudKitDelegates.m` is actually being compiled and included in the project (another common mistake). – Rob Napier Dec 07 '18 at 00:07
  • How is `iCloudDBDelegate` instantiated? Do `UIViewController` and `iCloudDBDelegate` both conform to `iCloudDBDelegate`? Does `Startup` conform to `iCloudDBDelegate`? – Willeke Dec 07 '18 at 00:14
  • Which object is the delegate, `iCloudDBDelegate` or the view controller? Which object should execute `crashLog:`? – Willeke Dec 07 '18 at 09:50
  • See @RobNapier's comment -- you said your code was in a framework, & he said putting that code in a framework often causes these problems. So consider just moving your category out of the framework and into your app for starters. – Caleb Dec 10 '18 at 12:28
  • Also: I see the word *delegate* being thrown around here rather a lot, but I don't see an actual delegate object. People misuse *delegate* all the time, and it just seems to be clouding the issue here. Is delegation an important aspect of your problem? – Caleb Dec 10 '18 at 12:30
  • Your example code from github doesn't crash. It prints "Before" and then "After." – Rob Napier Dec 10 '18 at 15:24

2 Answers2

1

The problem: identical method crashLog: in multiple classes, for example

@interface ViewController : UIViewController
@end

@implementation ViewController

- (void)someMethod {
    [self crashLog:@"error"];
}

-(void)crashLog:(NSString *)message {
    NSLog(@"%@", message);
}

@end

Solution A: move crashLog: to a common superclass (or a category on superclass UIViewController)

@interface CommonViewController : UIViewController

-(void)crashLog:(NSString *)message;

@end

@implementation CommonViewController

-(void)crashLog:(NSString *)message {
    NSLog(@"%@", message);
}

@end


@interface ViewController : CommonViewController
@end

@implementation ViewController

- (void)someMethod {
    [self crashLog:@"error"];
}

@end

Solution B: move crashLog: to a delegate and protocol

@protocol ICloudDBDelegate

-(void)crashLog:(NSString *)message;

@end


@interface DelegateClass : AnyClass <ICloudDBDelegate>
@end

@implementation DelegateClass

-(void)crashLog:(NSString *)message {
    NSLog(@"%@", message);
}

@end


@interface ViewController : UIViewController
@end

@implementation ViewController

@property (weak, nonatomic) id <ICloudDBDelegate> iCloudDBDelegate;

- (void)viewDidLoad
{
    [super viewDidLoad];
    AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    self.iCloudDBDelegate = appDel.iCloudDBDelegate;
}

- (void)someMethod {
    [self.iCloudDBDelegate crashLog:@"error"];
}

@end


@interface AppDelegate : UIResponder <UIApplicationDelegate, AppDelProtocolDelegate, iCloudDBDelegate>

@property (strong, nonatomic) id<iCloudDBDelegate>iCloudDBDelegate;

@end

@implementation AppDelegate

- (id<iCloudDBDelegate>)iCloudDBDelegate {
    if (!_iCloudDBDelegate) {
        _iCloudDBDelegate = [[DelegateClass alloc] init];
    }
    return _iCloudDBDelegate;
}

@end

Now we have new problem: property iCloudDBDelegate in multiple classes

Solution B + A: move crashLog to a delegate, move iCloudDBDelegate property to a superclass

@protocol ICloudDBDelegate

-(void)crashLog:(NSString *)message;

@end


@interface DelegateClass : AnyClass <ICloudDBDelegate>
@end

@implementation DelegateClass

-(void)crashLog:(NSString *)message {
    NSLog(@"%@", message);
}

@end


@interface CommonViewController : UIViewController

@property (weak, nonatomic) id <ICloudDBDelegate> iCloudDBDelegate;

@end

@implementation CommonViewController
@end


@interface ViewController : CommonViewController
@end

@implementation ViewController

- (void)someMethod {
    [self.iCloudDBDelegate crashLog:@"error"];
}

@end

Solution C: Another approach is a singleton object like NSUserDefaults.standardUserDefaults or NSFontManager.sharedFontManager: CloudDBManager.sharedCloudDBManager. No category or protocol required, just include CloudDBManager.h and use CloudDBManager.sharedCloudDBManager from everywhere.

@interface CloudDBManager : NSObject

@property(class, readonly, strong) CloudDBManager *sharedCloudDBManager;

-(void)crashLog:(NSString *)message;

@end

@implementation CloudDBManager

+ (CloudDBManager *)sharedCloudDBManager {
    static CloudDBManager *sharedInstance = nil;
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[CloudDBManager alloc] init];
        // Do any other initialisation stuff here
    });
    return sharedInstance;
}

-(void)crashLog:(NSString *)message {
    NSLog(@"%@", message);
}

@end


@interface ViewController : CommonViewController
@end

@implementation ViewController

- (void)someMethod {
    [CloudDBManager.sharedCloudDBManager crashLog:@"error"];
}

@end
Willeke
  • 14,578
  • 4
  • 19
  • 47
  • Is the delegate a singleton? – Willeke Dec 08 '18 at 22:17
  • Do all view controller share one delegate object or does each view controller have its own delegate object? – Willeke Dec 09 '18 at 05:47
  • In case you're still stuck, I added another solution which is less tangled and easier to implement. – Willeke Dec 13 '18 at 10:29
  • I don't know the structure of your app and framework. If your framework should call some method you provide in your app, a delegate would do. The TestDelegateProtocol project on Github doesn't log "testing" because `self.iCloudDBDelegate` is `nil`. You have to instantiate a delegate object and assign it to `iCloudDBDelegate`. – Willeke Dec 13 '18 at 12:08
  • Didn't you have a startup init method which sets the iCloudDBDelegate? – Willeke Dec 17 '18 at 09:29
  • How are the view controllers instantiated and which object is the owner of the delegate object? – Willeke Dec 22 '18 at 08:44
  • The `ApplicationCloudKit` object should be a [lazy property](https://stackoverflow.com/questions/11769562/lazy-instantiation-in-objective-c-iphone-development) of the app delegate. `ViewController` should use this app delegate property. – Willeke Dec 24 '18 at 08:24
  • This missing part to solution which solved this for me was adding a delegate to CommonViewController – BillThomas Dec 28 '18 at 23:09
1

(I've added this to my own framework, but I don't think thats important)

Yep, that's the typical problem. You've failed to include -ObjC in the link flags.

See Building Objective-C static libraries with categories. This applies to frameworks as well.

ObjC does not create linker symbols for methods. It can't, they're not resolved until runtime. So the category methods aren't seen by the linker as "missing" and it doesn't bother linking the relevant compile unit. This is an important optimization that keeps you from linking all of a massive C library just because you use one function in it, but Objective-C categories break some of the linker's assumptions. The compiler saw the definition (via the header), but the linker didn't care, so there's no error until runtime.

The -ObjC flag says "this C-looking compile unit is actually Objective-C; link all of it even if you don't think you need to."

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • The code you posted above and the code you're put on GitHub don't have any of the things you're describing. Neither has `Startup`. If `-[Startup crashLog:]` fails with "unrecognized selector sent to instance" then you're not loading the code you think you're loading, which almost certainly means you're not linking what you think you're linking, or `Startup` is not a `UIViewController`, or the code is running before categories are loaded (which is somewhat late in the launch). I suggest reducing this to an example that fails in the way you're describing. – Rob Napier Dec 10 '18 at 19:34