NSUserdefaults可以保存两个相同名称的数组吗 [英] Can NSUserdefaults save two arrays of same name

查看:46
本文介绍了NSUserdefaults可以保存两个相同名称的数组吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSUserdefault可以保存两个相同名称的数组还是可以替换先前的数组?

Can NSUserdefault save two arrays of same name or it replaces previous array?

推荐答案

是的,它替换为相同的变量,并且xcode不允许复制同一类中任何对象的相同名称,因此,在不删除任何一个数组之前,您的应用程序无法编译班上的名字...

yes it replace with same variable and xcode not allowed to duplicate the same name of any object in same class so your application not compile until you not remove any one array name from class...

更新:-

AppDelegate.h 文件中,只需声明变量...

In AppDelegate.h file just declare variable...

NSUserDefaults  *userDefaults;
NSMutableArray *yourArray;

之后...

AppDelegate.m 中填写 applicationDidFinishLonching:方法

userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingtblArrayForSearch = [userDefaults objectForKey:@"yourArray"];
    if (dataRepresentingtblArrayForSearch != nil) {
        NSArray *oldSavedArray = [NSKeyedUnarchiverunarchiveObjectWithData:dataRepresentingtblArrayForSearch];
        if (oldSavedArray != nil)
            yourArray = [[NSMutableArray alloc]initWithArray:oldSavedArray];
        else
            yourArray = [[NSMutableArray alloc] init];
    } else {
        yourArray = [[NSMutableArray alloc] init];
    }
    [yourArray retain];

之后,当您要从该用户插入,更新或删除数据时,默认使用波纹管代码..."

after that when you want to insert,update or delete Data from this userDefaults Use Bellow Code...

要删除记录,请在下面

[appDelegate.yourArray removeObjectAtIndex:IndexValue];// define integer value here on IndexValue

或者对于插入记录,使用波纹管..

         or     for insert record use bellow..

[appDelegate.yourArray addObject:AddValueHere];

或将整个数组替换为新数组,然后编写

or for replace whole array with new array then write this

appDelegate.yourArray = YourNewArray; 

[appDelegate.yourArray retain];

之后,用于保存整个数据同步 NSUserDefaults ,如下所示...

after that for save whole your data synchronize NSUserDefaults like bellow...         

NSData *data=[NSKeyedArchiverarchivedDataWithRootObject:appDelegate.yourArray];
    [appDelegate.userDefaults setObject:data forKey:@"yourArray"];
    [appDelegate.userDefaults synchronize];

这篇关于NSUserdefaults可以保存两个相同名称的数组吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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