NSMutableArray在reloadData后变为nil [英] NSMutableArray becoming nil after reloadData

查看:204
本文介绍了NSMutableArray在reloadData后变为nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的iPhone应用程序中,我从Web服务获取一组数据,将其存储在可变数组中以便在表中显示,并且在没有网络时将其存储在核心数据中作为备份。

In an iPhone app I'm developing, I grab a set of data from a web service, store it in a mutable array for showing in a table, and also store it in core data as backup when there is no network.

我的数组在应用程序中初始化:didFinishLaunchingWithOptions:如下

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

然后在调用viewDidLoad之后填充数组,这意味着UITableView存在(通过检查调试器验证),并且在调用reloadData之前,我已经NSlog打印出内容

My array is initialized in application:didFinishLaunchingWithOptions: as follows
tableArray = [[NSMutableArray alloc] initWithCapacity:100];
the array is then filled after viewDidLoad gets called, meaning the UITableView exists (verified by checking debugger), and just before reloadData is called, I have NSLog print out the contents of the array, and sure enough everything is there.

我的问题是在 reloadData被调用后,tableArray变为null,如图所示NSLog in tableView:numberOfRowsInSection:。

My problem is that after reloadData is called, tableArray becomes null, as shown by an NSLog in tableView:numberOfRowsInSection:.

我完全失去了为什么会发生这种情况,虽然我只在Cocoa编程了几个月,

I'm at a complete loss as to why this is happening, though I've only been programming in Cocoa for a few months and could easily be missing something obvious.

@interface MyAppDelegate:NSObject {

...

NSMutableArray * tableArray;

}

@property(nonatomic,retain)NSMutableArray * tableArray;

...

@end

@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];
    ...
}

然后在masterViewController中调用一个名为runWithNetwork的方法viewDidLoad:

A method called runWithNetwork is then called in masterViewController's 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];
}

然后我转到我的表方法:

Then I go to my table methods:

- (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中描述,不会在其他任何地方重新创建。

The tableView is in the MasterViewController's xib, and is linked up correctly there. It is only ever described in MasterViewController and is set up as an IBOutlet. tableArray is only described in MyAppDelegate.h and is not re-created anywhere else.

推荐答案

正如我在评论中所说,很难弄清楚究竟是什么错了。但从我的经验帮助人们邮寄名单等,这样的错误的最常见的原因是有两个不同的对象,当你认为你有一个。例如,在一个nib中创建一个实例,另一个实例在其他地方的代码中手动创建。这可能值得检查。

As I said in my comment, it's hard to figure out what exactly is wrong. But from my experience helping people on mailing lists and the like, the most common cause of a bug like this is having two different objects when you think you have one. For example, one instance is created in a nib and another is manually created in code elsewhere. It might be worth checking.

这篇关于NSMutableArray在reloadData后变为nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆