CoreData对象没有填充值

时间:2014-03-27 12:53:13

标签: ios objective-c core-data

我有这个代码,它读取我的coreData并将其调整为coredata对象数组。然后我将这些coredata对象中的一个放入其自己的var类型中,但并不是所有属性都填充开始。< / p>

这就是我的代码。

NSMutableArray *tempFinishing = [coreDataController readFinishing];
    for (int i = 0; i < [tempFinishing count]; i++) {
        currentProject = [[Project alloc] init];
        currentProject = [tempFinishing objectAtIndex:i];

        if ([currentProject.hasChange isEqualToString:@"T"]) {

当我检查hasChange时它会回来为零...但是这是一件奇怪的事情 如果我在控制台中这样做。

po currentProject.hasChange

返回nil

po currentProject.myID 

返回myID“1234”

然后

po currentProject.hasChange

返回“F”//这是不正确的,它应该设置为T并且在更改时已经看过调试....但是这只适用于我在终端中进行调试..

更新

现在收到这些警告

Incompatible pointer types sending 'NSString *' to parameter of type 'NSEntityDescription *'
Incompatible pointer types initializing 'Project *' with an expression of type 'NSManagedObject *'

使用此代码。

NSManagedObjectContext *context = [self managedObjectContext];
        Project *currentProj = [[NSManagedObject alloc] initWithEntity:@"Project" insertIntoManagedObjectContext:context];

更新2

这是我的coredata readFinishing方法。

- (NSMutableArray *)readFinishing {
    NSManagedObjectContext *context = [self managedObjectContext];
    if (context == nil) {
        NSLog(@"Nil");
    }
    else {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];
        NSError *error;
        NSMutableArray *projectDictionaryArray = [[NSMutableArray alloc] init];
        NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

        for (Project *projects in fetchedObjects) {
            [projectDictionaryArray addObject:projects];
        }
        return projectDictionaryArray;
    }
    return nil;
}

希望这有助于解决问题..我已经在这方面工作了几天...... :(大声笑)

更新3

我想我可能已经找到了问题....在核心数据对象类中我决定随机查看我发现了这个..

enter image description here

立即更新为动态和测试...

3 个答案:

答案 0 :(得分:3)

必须将CoreData对象分配给上下文,并且通常需要使用NSEntitiyDescription

创建
+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context

像:

Product *newProduct = [NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:context];

有关在代码中创建托管对象的更多信息,例如here

答案 1 :(得分:0)

有一些问题:

currentProject = [[Project alloc] init];
currentProject = [tempFinishing objectAtIndex:i];

1)您无法将项目创建为[[Project alloc] init];,但必须使用

Project *newProject = [NSEntityDescription insertNewObjectForEntityForName:@"Project"
inManagedObjectContext:context];

2)第二行currentProject = [tempFinishing objectAtIndex:i];在其前面的行处重写“created”对象。

3)所有代码(不是由xCode生成的)都必须在Project子类中。因为一旦你通过xCode重新生成Projects类,它将重写你所有的自定义代码。因此,请考虑使用mogenerator

答案 2 :(得分:0)

通过查看您的代码,我发现&#34; Project&#34;是核心数据的实体。 所以,在这段代码中,你不应该像你一样创建这样的实体;

  for (int i = 0; i < [tempFinishing count]; i++) {
       currentProject = [[Project alloc] init];
       currentProject = [tempFinishing objectAtIndex:i];

       if ([currentProject.hasChange isEqualToString:@"T"]) {
       }
}

//Instead you should use the code like this:-

  for (int i = 0; i < [tempFinishing count]; i++) {

       //currentProject = [[Project alloc] init]; //Delete this line of code...
         currentProject = [tempFinishing objectAtIndex:i]; 

//And, you should read an attribute/property value of an entity like this

        [currentProject valueForKey:@"hasChange"];
//So, 

        if ([[currentProject valueForKey:@"hasChange"] isEqualToString:@"T"]) {
        }
}