NSMutableArray没有添加我的NSString

时间:2013-05-08 07:07:18

标签: objective-c memory-management nsstring nsmutablearray nsuserdefaults

我正在尝试向NSString添加NSMutableArray,然后将数组转换为NSData并将其保存为NSUserDefaults。但阵列总是零。

这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    library = [[ALAssetsLibrary alloc] init];
    groups = [[NSMutableArray alloc] init];
    userDefaults = [[NSUserDefaults alloc] init];

    NSData *data = [userDefaults objectForKey:@"GroupArray"];

    groups = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    NSLog(@"%i", [groups count]);
}

-(IBAction)newFolder {
    if (textField.text != nil) {
        NSString *string = textField.text.capitalizedString;
        [library addAssetsGroupAlbumWithName:string
                    resultBlock:^(ALAssetsGroup *group) {
                        NSLog(@"Created a folder named: %@", group);
                    }
                    failureBlock:^(NSError *error) {
                        NSLog(@"An error occured! - %@", error);
                    }
         ];
        [groups addObject:string];
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:groups];
        [userDefaults setObject:data forKey:@"GroupArray"];

        NSLog(@"%i", [groups count]);
    }
    [self.view sendSubviewToBack:subView];
}

当应用程序启动时,我在控制台中收到一条消息,表明该数组为NULL。当我尝试添加字符串时,NSLog(@"%i", [groups count]);总是返回0。

为什么会这样?

2 个答案:

答案 0 :(得分:1)

userDefaults = [[NSUserDefaults alloc] init];
NSData *data = [userDefaults objectForKey:@"GroupArray"];

在这种情况下,第一次执行代码时data将是nil,因为还没有“GroupArray”属性存在。

groups = [NSKeyedUnarchiver unarchiveObjectWithData:data];

这会导致groups成为nil,因为以unarchiveObjectWithData:作为参数调用nil也会返回nil

由于这一切,在-newFolder中,[groups addObject:string]变为[nil addObject:string] 在Objective-C中允许在nil上调用方法,因此您不会在此处获得异常。 nil上调用的任何方法的返回值都是nil0

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:groups];

这会导致datanil,因为群组为nil

所以你总是在打电话:

[userDefaults setObject:data forKey:@"GroupArray"];

data = nil

快速修复: 将if (groups == nil) groups = [NSMutableArray array];添加到-newFolder

的开头

答案 1 :(得分:0)

每次初始化 NSUserDefaults 的新实例并调用

时,查看didLoad方法
  

NSData * data = [userDefaults objectForKey:@" GroupArray"];在新创建的对象上

将一直返回nil,因为在新实例中不存在键@" GroupArray"的任何对象。而是将 userDefaults 替换为单例对象 [NSUserDefaults standardUserDefaults]

修改您的代码,如下所示

- (void)viewDidLoad 
{
     [super viewDidLoad];
     library = [[ALAssetsLibrary alloc] init];
     NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"GroupArray"];

     groups = [NSKeyedUnarchiver unarchiveObjectWithData:data];
     if (!groups)
     {
         groups = [[NSMutableArray alloc] init];    
     }
     NSLog(@"group %@", groups);
     NSLog(@"%i", [groups count]);
}     

您的newFolder方法

  -(IBAction)newFolder 
  {
     if (textField.text != nil) 
     {
          NSString *string = textField.text.capitalizedString;
         [library addAssetsGroupAlbumWithName:string
                     resultBlock:^(ALAssetsGroup *group) {
                         NSLog(@"Created a folder named: %@", group);
                     }
                     failureBlock:^(NSError *error) {
                         NSLog(@"An error occured! - %@", error);
                     }
          ];
        [groups addObject:string];
         NSData *data = [NSKeyedArchiver archivedDataWithRootObject:groups];

        [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"GroupArray"];
        [[NSUserDefaults standardUserDefaults] synchronize];

         NSLog(@"%i", [groups count]);
     }
     [self.view sendSubviewToBack:subView];
 }

这应该有效。因为它对我有用。