【问题标题】:ios/objective-c/core-data: Call method in managedobjectcontext class syntaxios/objective-c/core-data: managedobjectcontext类语法中的调用方法
【发布时间】:2015-05-03 16:17:53
【问题描述】:

我正在尝试按照this 方法在核心数据中创建一个自动递增字段,以便与运行 SQL 的 Web 服务器同步。

但是,当我尝试调用此方法时,我得到“No known class method...”

这应该是微不足道的,因此非常令人沮丧。我有错字还是有人能指出我在这个简单的方法调用中做错了什么?谢谢。

Item.h

-(NSUInteger)autoId;

Item.m

-(NSUInteger) autoId {
    
    NSURL *url = [[self objectID] URIRepresentation];
    //get full url
    NSString *url_string = [url absoluteString];
    
    //split with a /
    NSArray *parts = [url_string componentsSeparatedByString:@"/"];
    
    NSString *last_segment=[parts objectAtIndex:[parts count]-1];
    
    //p#
    NSString *number_part = [last_segment stringByReplacingOccurrencesOfString:@"p" withString:@""];
    
    NSUInteger auto_id = [number_part integerValue];
    
    number_part =nil;
    last_segment=nil;
    parts=nil;
    url=nil;
    url_string=nil;
    
    
    return auto_id;
    
}

AddItem.m

#import "Items.h"


/within save method

  NSUInteger autoid = [Items autoId];//error here

错误是“选择器 autoId 没有已知的类方法”和(警告)“指向整数转换的指针不兼容,用 id 类型的表达式初始化 NSUInteger aka unsigned long”

感谢您的任何建议。

【问题讨论】:

  • 你应该向我们展示定义 Items 的类。
  • Items类的相关部分在上面已经给出。问题是我忘记实例化对象,如下所述。

标签: ios core-data methods objective-c++


【解决方案1】:

意识到我在调用它之前忘记实例化对象。既然是NSManagedObject,就应该实例化如下:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];

    Items *newItem = [[Items alloc]initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];

           NSUInteger autoid = [newItem autoId];

工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    相关资源
    最近更新 更多