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

时间:2022-11-15 22:33:30

I am trying to follow this method to create an auto-incrementing field in core data for purposes of syncing with a web server running SQL.

我试图遵循此方法在核心数据中创建自动递增字段,以便与运行SQL的Web服务器同步。

When I try to call this method, however, I get "No known class method..."

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

This should be trivial so it is very frustrating. Do I have a typo or can anyone point out what I am doing wrong with this simple method call? Thank you.

这应该是微不足道的,所以非常令人沮丧。我是否有拼写错误或任何人都可以通过这个简单的方法调用指出我做错了什么?谢谢。

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

The errors are "No known class method for selector autoId" and (warning) "Incompatible pointer to integer conversion initializing NSUInteger aka unsigned long with an expression of type id"

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

Thanks for any suggestions.

谢谢你的任何建议。

1 个解决方案

#1


Realized I forgot to instantiate the object before calling it. Since it is an NSManagedObject, it should be instantiated as follows:

意识到我忘了在调用之前实例化对象。由于它是NSManagedObject,因此应将其实例化如下:

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

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

           NSUInteger autoid = [newItem autoId];

Works.

#1


Realized I forgot to instantiate the object before calling it. Since it is an NSManagedObject, it should be instantiated as follows:

意识到我忘了在调用之前实例化对象。由于它是NSManagedObject,因此应将其实例化如下:

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

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

           NSUInteger autoid = [newItem autoId];

Works.