objective-c - 类别实例方法不调用

标签 objective-c ios xcode methods categories

我有一个类(class)(生成核心数据):

@interface Place : NSManagedObject

 @property (nonatomic, retain) NSString * title;
 @property (nonatomic, retain) NSString * subtitle;
 @property (nonatomic, retain) NSNumber * latitude;
 @property (nonatomic, retain) NSNumber * longitude;

@end
@implementation Place

@dynamic title;
@dynamic subtitle;
@dynamic latitude;
@dynamic longitude;

@end

为其添加一个类别:

@interface Place (Create)
+ (Place *)placeWithTitle:(NSString *)title
        inManagedObjectContext:(NSManagedObjectContext *)context;

+(Place *) placeWithTitle:(NSString *)title andSubtitle:(NSString *)subtitle inManagedObjectContext:(NSManagedObjectContext *)context;
+(Place *) placeWithCoordinate:(CLLocationCoordinate2D) coordinate inManagedObjectContext:(NSManagedObjectContext *)context;
- (void) setLattitudeAndLongitudeByCoordinate:(CLLocationCoordinate2D) coordinate;


- (CLLocationCoordinate2D) coordinate;
@end

在此类别中必须实例方法:

- (void) setLattitudeAndLongitudeByCoordinate:(CLLocationCoordinate2D) coordinate
{
    self.latitude=[NSNumber numberWithDouble:coordinate.latitude];//break point HERE
    self.longitude=[NSNumber numberWithDouble:coordinate.longitude];
}

-(CLLocationCoordinate2D) coordinate
{
    CLLocationCoordinate2D coord;//break point HERE
    coord.latitude=[self.latitude doubleValue];
    coord.longitude=[self.longitude doubleValue];
    return coord;
}

正如你所见,我必须在那里断点。现在,我调用这些方法

#import "Place+Create.h"

-(void)someMethod
{
    [self.place setLattitudeAndLongitudeByCoordinate:coordiante];
}

它似乎永远不会到达 setLattitudeAndLogitudeByCooperative 方法内的那些断点, 调试显示它甚至没有调用它们!为什么?

最佳答案

someMethod 是否已达到?检查是否在其头部使用断点或额外的 NSLog

如果不是,那么您的问题出在其他地方,因为您的程序流程与您预期的不同。

如果达到了,那么self.place显然是nil。这意味着您从未初始化过它,并且您的程序流程与您想象的不同。

关于objective-c - 类别实例方法不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10213786/

相关文章:

ios - MMDrawerViewController 设置

objective-c - 即使使用默认 568h@2x 图像,iOS 应用程序也不会使用 4 英寸尺寸

xcode - 更改默认 SKScene

iphone - iPhone SDK 4.0 中的滑动手势?

iphone - 安装 Facebook SDK - iPhone

ios - Objective C 冒充与多态性/方法覆盖

ios - 发送用户名和密码后收到服务器响应,但我无法验证登录页面中的响应?

iOS Swift 用户默认观察者错误消息已收到但未处理

ios - 我如何组织这些数据?

objective-c - 弹出窗口和桌面 View 透明