ios/objective-c/core-data: managedobjectcontext class 语法中的调用方法

ios/objective-c/core-data: Call method in managedobjectcontext class syntax

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

然而,当我尝试调用此方法时,我得到“没有已知的 class 方法...”

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

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

错误是“没有已知的 class 选择器 autoId 方法”和(警告)“不兼容的指向整数转换的指针初始化 NSUInteger aka unsigned long with an expression of type id”

感谢您的任何建议。

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

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

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

           NSUInteger autoid = [newItem autoId];

有效。