Iphone NSManagedObject不兼容指针类型警告

Iphone NSManagedObject不兼容指针类型警告,iphone,objective-c,core-data,ios5,nsmanagedobjectcontext,Iphone,Objective C,Core Data,Ios5,Nsmanagedobjectcontext,让我先确定我有X代码4.3.2版本和iOS 5.1 SDK。我在我的项目中使用了以下核心数据操作方法。两种方法都给出相同的警告。i、 e“不兼容的指针类型从结果类型为“NSManagedObject”的函数返回“NSManagedObject*” 方法A: - (NSManagedObject<Protocol> *)newMOforNilMOC { return [[NSManagedObject alloc] initWithEntity:[self entityDes

让我先确定我有X代码4.3.2版本和iOS 5.1 SDK。我在我的项目中使用了以下核心数据操作方法。两种方法都给出相同的警告。i、 e“不兼容的指针类型从结果类型为“NSManagedObject”的函数返回“NSManagedObject*”

方法A:

- (NSManagedObject<Protocol> *)newMOforNilMOC 
{
    return [[NSManagedObject alloc] initWithEntity:[self entityDescription] insertIntoManagedObjectContext:nil];
}
+ (NSManagedObject<Protocol> *) newInContext:(NSManagedObjectContext *)context 
    {   
        return [[NSEntityDescription insertNewObjectForEntityForName:[[(NSManagedObject<Protocol> *)self class] entityName]                                     inManagedObjectContext:context]retain]; 
    }
-(NSManagedObject*)新MOFORNILMOC
{
返回[[NSManagedObject alloc]initWithEntity:[self entityDescription]InsertintoManagedObject上下文:nil];
}
对于方法方法A我只需进行类型转换并添加(NSManagedObject*),然后删除警告,如下所述

- (NSManagedObject<Protocol> *)newMOforNilMOC 
{
    return  (NSManagedObject<Protocol> *) [[NSManagedObject alloc] initWithEntity:[self entityDescription] insertIntoManagedObjectContext:nil];
}
-(NSManagedObject*)新MOFORNILMOC
{
return(NSManagedObject*)[[NSManagedObject alloc]initWithEntity:[self entityDescription]insertIntoManagedObjectContext:nil];
}
方法B:

- (NSManagedObject<Protocol> *)newMOforNilMOC 
{
    return [[NSManagedObject alloc] initWithEntity:[self entityDescription] insertIntoManagedObjectContext:nil];
}
+ (NSManagedObject<Protocol> *) newInContext:(NSManagedObjectContext *)context 
    {   
        return [[NSEntityDescription insertNewObjectForEntityForName:[[(NSManagedObject<Protocol> *)self class] entityName]                                     inManagedObjectContext:context]retain]; 
    }
+(NSManagedObject*)newInContext:(NSManagedObjectContext*)上下文
{   
在ManagedObjectContext:context中返回[[NSEntityDescription insertNewObjectForEntityForName:[[(NSManagedObject*)自类]entityName]inManagedObjectContext:context]retain];
}
对于方法B当我进行打字时,它将不起作用,因此我只需将方法的名称从newInContext更改为AddnewInContext(在谷歌搜索时找到),然后删除警告

我有以下问题:

  • 如果第一种方法需要类型转换,那么为什么第二种方法不能使用该解决方案呢
  • 更改方法名称的确切含义是什么。这是消除上述警告的正确方法吗?为什么打字不工作 方法B

  • 这可能很复杂,但如果您有任何疑问,请随时发表评论。因为我想知道两者之间的区别,至少我学到了一些关于核心数据的新东西。

    我认为您在类方法中使用了“self”。您应该使用类本身。让我用代码显示

    + (NSManagedObject<Protocol> *) newInContext:(NSManagedObjectContext *)context 
    {   
            //Usage of [self class] is not correct, as self points already to a class.
            NSEntityDescription* desc = [NSEntityDescription entityForName:@"myObjectName" inManagedObjectContext:context];
            return [[NSEntityDescription insertNewObjectForEntityForName:desc inManagedObjectContext:context]retain]; 
    }
    
    +(NSManagedObject*)newInContext:(NSManagedObjectContext*)上下文
    {   
    //[self class]的用法不正确,因为self已指向某个类。
    NSEntityDescription*desc=[NSEntityDescription entityForName:@“myObjectName”在托管对象上下文:上下文中];
    返回[[NSEntityDescription insertNewObjectForEntityForName:desc inManagedObjectContext:context]retain];
    }
    

    不能将类类型强制转换为“id”类型,如果您在对象方法而不是类方法中,self会指向该类型。我不认为任何重命名方法都能解决任何警告。

    AddnewInContext在做什么?它只是新更改的名称,仅此而已,也可能是其他名称。正如我前面提到的,我只是更改了方法的名称。它仍然给出了相同的警告。正如我从警告中看到的,“不兼容的指针类型从结果类型为“NSManagedObject”的函数返回“NSManagedObject”,强制转换是从NSManagedObject到NSManagedObject,没有*。这可能是打字错误吗?我在你发布的方法中看不到这一点。