我试图遵循这方法在核心数据中创建一个自动递增字段,以便与运行SQL的web服务器进行同步。
然而,当我试图调用这个方法时,我得到了“没有已知的类方法.”
这应该是微不足道的,所以它是非常令人沮丧的。我是否有错误,或者有人能指出我在这个简单的方法调用中做错了什么?谢谢。
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类方法”和(警告)“不兼容的整数转换指针,初始化NSUInteger (即用id类型的表达式进行无符号长)”。
谢谢你的建议。
发布于 2015-05-03 17:49:03
意识到我忘记在调用对象之前实例化它。因为它是一个NSManagedObject,所以它应该实例化如下:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];
Items *newItem = [[Items alloc]initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
NSUInteger autoid = [newItem autoId];很管用。
https://stackoverflow.com/questions/30016200
复制相似问题