2009-09-25 57 views
0

我有一個NSArray的字符串,我想添加一定數量的行到大綱視圖取決於有多少個字符串在數組中,每個字符串的標題加入。添加到NSArray的核心數據模型

我認爲這將涉及循環這樣的數組。

for(NSString *title in array) { 
    JGManagedObject *theParent = 
     [NSEntityDescription insertNewObjectForEntityForName:@"projects" 
             inManagedObjectContext:managedObjectContext]; 
    [theParent setValue:nil forKey:@"parent"]; 
    [theParent setValue:@"Project" forKey:@"name"]; 
    [theParent setValue:[NSNumber numberWithInt:0] forKey:@"position"]; 

} 
+0

你確定你的意思是設置一個字符串說「零」作爲父親的父親的價值,而不是'nil'本身? – 2009-09-26 15:44:52

+0

哦。感謝您的注意。我的意思是「無」。 – Joshua 2009-09-26 16:11:12

+0

correct @「nil to nil – Abizern 2009-09-26 16:30:09

回答

1

不要打擾。樹控制器位於模型(Spark正在使用的Core數據存儲)和視圖(源視圖)之間。您應該從數組添加到數據存儲,而不是從數組添加到樹控制器。

樹控制器將選取模型中的更改並顯示視圖中的更改。

編輯:

(請記住這是很難從遠處調試)

垃圾收集,如果你不堅持自己的對象,他們容易成爲從你下面清理。

試試這個,看看會發生什麼:

for(NSString *title in array) { 
    NSManagedObjectContext *moc = [self managedObjectContext]; 
    JGManagedObject *theParent = 
     [NSEntityDescription insertNewObjectForEntityForName:@"projects" 
             inManagedObjectContext:moc]; 
    [theParent setValue:nil forKey:@"parent"]; 
    // This is where you add the title from the string array 
    [theParent setValue:title forKey:@"name"]; 
    [theParent setValue:[NSNumber numberWithInt:0] forKey:@"position"]; 

} 
+0

嗯,好吧,我該怎麼做?我習慣於添加到樹控制器中。 – Joshua 2009-09-25 17:25:07

+0

從數組中的每個字符串創建模型對象並將其添加到託管對象上下文中。 – Abizern 2009-09-25 21:18:43

+0

喜歡... http://snapplr.com/xc5y – Joshua 2009-09-26 07:24:42