开发者

How do I call NSManaged object's @dynamic method from from another object using a @selector and NSInvocation?

Cocoa newbie here. I am working on an iPhone UITableViewController-based widget that can be used to edit date and text properties in an object set during initiation. Currently, I am attempting to do this with a @selector and NSInvocation as below. Note: the "targetObject" is the object set when the controller is initialized.

- (IBAction)saveDate:(id)sender {
      //The selector below would normally be passed in when the controller is initialized
      [self setDoneSelector:@selector(setDate:)];
      NSMethodSignature * sig = nil;
      sig = [[targetObject class] instanceMethodSignatureForSelector:[self doneSelector]];
      NSInvocation * myInvocation = nil;
      myInvocation = [NSInvocation invocationWithMethodSignature:sig];
      [myInvocation setTarget:targetObject];
      [myInvocation setSelector:doneSelector];
      NSDate * myDate = [datePicker date];
      [myInvocation setArgument:&myDate atIndex:2];
      NSString * result = nil; 
      [myInvocation retainArguments]; 
      [myInvocation invoke];
}

This works fine on most objects, but I am running into trouble when passing in a Core Data (NSManagedObject) as 开发者_开发问答the targetObject. The object looks like this:

Transaction.h

#import <CoreData/CoreData.h>

@interface Transaction :  NSManagedObject  
{
}

@property (nonatomic, retain) NSString * message;
@property (nonatomic, retain) NSDate * date;

@end

Transaction.m

#import "Transaction.h"

@implementation Transaction 

@dynamic message;
@dynamic date;

@end

If I set this object in my controller as the targetObject, I can call the "setDate:" method directly without issue.

[targetObject setDate:[datePicker date]];

But when I try to invoke it with the @selector, I get 'Program received signal: "EXC_BAD_ACCESS”.'

I imagine this has something to do with the @dynamic methods used in the NSManagedObject and when they are created, but I don't know enough about that process to know how to or if I can workaround this to get it working. I have tried explicitly creating the "setDate:(NSDate *)aDate" method in the Transaction object, and that works, but I am wondering if I should do that and how it might the NSManagedObject.

Can I access these setter methods with a @selector without explicitly defining them?


Agreed w/ NSD here. You should start by simplifying this code to the much simpler -performSelector:withObject: version:

- (IBAction)saveDate:(id)sender {
      [self.targetObject performSelector:self.doneSelector withObject:[self.datePicker date]];
}

If that still has trouble, we can start debugging where the real problem is. NSInvocation is a very fancy object for solving this kind of simple problem.

If you still get the crash, then you'll want to look at the actual stacktrace to see what variable is not being correctly initialized or being over-released.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜