【问题标题】:NSArray coming back as null when called from NSNotificationCenter从 NSNotificationCenter 调用时,NSArray 返回为 null
【发布时间】:2012-07-13 02:32:27
【问题描述】:

我有一个使用 NSUserDefaults 保存的 NSMutableArray,当调用 appLaunched 时,该数组返回 null,但当被任何其他函数调用时,它返回数组中的内容。有人知道发生了什么吗?

- (id)init
 {
[super init];
theArray = [[NSMutableArray alloc] init];
theArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"TheArray"];
if(!theArray) {
    theArray = [[NSMutableArray alloc] init];
} else {
    theArray = [[NSMutableArray alloc] initWithArray:theArray];
};

// NSLog(@"%@", theArray);
[[NSUserDefaults standardUserDefaults] setObject:theArray forKey:@"TheArray"];
// NSLog(@"Is saving");

return self;
}


 -(IBAction)addNewProcess:(id)sender
 {
     NSString *newProcess = [theField stringValue];
     [theArray addObject:newProcess];
     [theField setStringValue:@""];
     NSLog(@"%@", theArray);
     [theTable reloadData];

 }

 -(IBAction)removeProcess:(id)sender
 {
     int listRow = [theTable selectedRow];
     NSLog(@"%d", listRow);
     if (listRow < 0) return;
     [theArray removeObjectAtIndex:listRow];
     [theTable reloadData];
 }

 -(IBAction)save:(id)sender
 {
     NSLog(@"%@", theArray);
     int count = [theArray count];
     NSLog(@"%i", count);
     [[NSUserDefaults standardUserDefaults] setObject:theArray forKey:@"TheArray"];
     NSLog(@"Is saving");
  }

 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
 {
     return [theArray count];
 }

 - (id)tableView:(NSTableView *)tableView 
 objectValueForTableColumn:(NSTableColumn *)tableColumn 
             row:(NSInteger)row
 {
     return [theArray objectAtIndex:row];
 }

 - (void)tableView:(NSTableView *)tableView 
    setObjectValue:(id)object 
    forTableColumn:(NSTableColumn *)tableColumn 
               row:(NSInteger)row
 {
     [theArray replaceObjectAtIndex:row
                               withObject:object];
 }

 -(void)appLaunched:(NSNotification *)note
 {
     NSLog(@"%@", theArray);

 }


 @end

【问题讨论】:

    标签: cocoa nsmutablearray nstableview nsnotificationcenter


    【解决方案1】:

    您没有同步您的 NSUserDefaults。 每次使用[[NSUserDefaults standardUserDefaults] setObject:someObject forKey:@"someKey"]; 都必须使用[[NSUserDefaults standardUserDefaults] synchronize]; 后记。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多