2010-07-12 70 views
2

在我正在開發的iPhone應用程序中,我從Web服務中獲取一組數據,將其存儲在可變數組中以便在表中顯示,並將其作爲備份存儲在覈心數據中沒有網絡。NSMutableArray在reloadData後變爲零

我的數組初始化中的application:didFinishLaunchingWithOptions:如下
tableArray = [[NSMutableArray alloc] initWithCapacity:100];
然後陣列viewDidLoad中後填寫被調用,這意味着UITableView的存在(通過檢查調試驗證),並reloadData調用前,我有NSLog打印出陣列的內容,並確保一切都在那裏。

我的問題是, reloadData被調用,tableArray變爲null,如NSView在tableView:numberOfRowsInSection:所示。

我完全喪失了爲什麼會發生這種情況,儘管我只在Cocoa進行了幾個月的編程,可能很容易丟失一些明顯的東西。

編輯:用基本代碼更新。

@interface MyAppDelegate : NSObject {
...
NSMutableArray *tableArray;
}
@property (nonatomic, retain) NSMutableArray *tableArray;
...
@end

@implementation MyAppDelegate
@synthesize tableArray

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    tableArray = [[NSMutableArray alloc] initWithCapacity:100]; 

    masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:[NSBundle mainBundle]]; 
    [masterViewController.tableView setDelegate:self]; 
    [masterViewController.tableView setDataSource:self]; 
    ... 
} 

稱爲runWithNetwork的方法,然後調用在masterViewController的viewDidLoad:

- (void)runWithNetwork { 
    ... 
    NSArray *backpackArray = [[mainDict objectForKey:@"items"] objectForKey:@"item"]; 
    NSLog(@"%i", [backpackArray count]); 
    for (int a=0; a<[backpackArray count]; a++) { 
    NSDictionary *itemDict = [backpackArray objectAtIndex:a]; 
     NSString *ID = [[itemDict valueForKey:@"defindex"] stringValue]; 
     NSString *level = [[itemDict valueForKey:@"level"] stringValue]; 
     NSString *quality = [[itemDict valueForKey:@"quality"] stringValue]; 
     NSString *inventory = [[itemDict valueForKey:@"inventory"] stringValue]; 
     NSNumber *uid = [NSNumber numberWithInt:a]; 

     //Insert new 
     myItem = [NSEntityDescription insertNewObjectForEntityForName:@"Backpack" inManagedObjectContext:managedObjectContext_]; 
     [myItem setValue:level forKey:@"level"]; 
     [myItem setValue:inventory forKey:@"inventory"]; 
     [myItem setValue:quality forKey:@"quality"]; 
     [myItem setValue:uid forKey:@"uid"]; 

     //Link to item db 
     NSFetchRequest *fetch = [[NSFetchRequest alloc] init]; 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id == %@", ID]; 
     [fetch setEntity:[NSEntityDescription entityForName:@"GlobalItems" inManagedObjectContext:managedObjectContext_]]; 
     [fetch setPredicate:predicate]; 

     item = [[managedObjectContext_ executeFetchRequest:fetch error:nil] objectAtIndex:0]; 
     [myItem setValue:item forKey:@"item"]; 

     [tableArray addObject:[item valueForKey:@"name"]]; //a series of nsstrings 

    } 
    NSLog(@"%@, %i", tableArray, [tableArray retainCount]); // all strings are printed correctly here, retaincount is 1 

    [masterViewController.tableView reloadData]; 
} 

然後我去我的表的方法:

- (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"%@, %i", tableArray, [tableArray retainCount]); //(null) printed, 0 for retaincount 
    return [tableArray count]; //returns 0 
} 

tableView在MasterViewController的xib中,並在那裏正確連接。它僅在MasterViewController中進行了描述,並被設置爲IBOutlet。 tableArray僅在MyAppDelegate.h中描述,不會在其他地方重新創建。

+1

這是非常奇怪的是,您的變量自發變成'nil'。你有沒有嘗試用Guard Malloc(運行菜單 - >啓用Guard Malloc)運行你的程序,看看你是否有緩衝區voerflow? – zneak 2010-07-12 23:45:21

+0

剛剛嘗試過,我假設GuardMalloc發現的任何東西都會打印在控制檯中?在這種情況下,一切似乎都很好,我收到了GM的「歡迎」消息,然後像以前一樣,所有內容都由NSLog打印出來。 – MattMcN 2010-07-13 00:03:36

+1

有幾種可能性浮現在腦海中,但很難用模糊的描述來說明代碼的工作方式。看到代碼(或者仍然存在問題的簡化版本)會使其更易於推斷。 – Chuck 2010-07-13 00:11:14

回答

1

正如我在評論中所說的,很難弄清楚究竟是什麼錯誤。但從我幫助郵件列表等人的經驗來看,像這樣的錯誤最常見的原因是當你認爲有兩個不同的對象時。例如,一個實例在一個筆尖中創建,另一個實例在別處的代碼中手動創建。這可能值得檢查。

+0

用代碼更新。Re:重複錯誤,tableView只在一個文件中定義,並且在IB中正確鏈接,tableArray只在一個文件中定義,並且不會在任何文件中被意外重新創建。但是,感謝輸入,我還沒有檢查多個對象。 – MattMcN 2010-07-13 00:40:39

+0

@MattMcN:它的方法返回不一致的結果,通常具有獨立性。在這種情況下,將在該類的一個實例上調用'runWithNetwork',並且該表將發送其數據源方法到另一個實例。 (我沒有試圖堅持這是錯的 - 只是你不小心忽略了最有可能的嫌疑人。) – Chuck 2010-07-13 00:58:09

+0

啊,我現在明白你的意思。我調用runWithNetwork的方式是'[[[[UIApplication sharedApplication] delegate] runWithNetwork]'這似乎已經返回了我的委託的一個新實例,其中一個空的數組。 我現在已經重新構建了應用程序,從委託到viewController回委託給委託給viewController,並且一切都工作盛大。 – MattMcN 2010-07-13 10:19:00

1

您的tableArray沒有被保留,因爲您沒有使用您的存取器來設置它。更改:

tableArray = [[NSMutableArray alloc] initWithCapacity:100]; 

到:

[self setTableArray:[[NSMutableArray alloc] initWithCapacity:100]];