当我们保存在NSUserDefaults中时,从核心数据和应用程序中获取“<null>”数据会崩溃吗?

时间:2017-11-14 05:37:25

标签: ios objective-c core-data nsuserdefaults

在应用版本1.0中,我的核心数据有5个属性,如attribute1,attribute2,attribute3,attribute4,attribute5(所有属性类型都是可转换的)。它目前在App Store中可用。在应用程序版本2.0中,我更新了我的核心数据DB。我添加了一个属性,如attribute6。我将核心数据版本从核心数据更改为核心数据2,我在persistentStoreCoordinator中更改了这样的内容。

NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];

它工作正常,但在应用程序从版本1.0更新到2.0(首次安装新版本)时,我从attribute6获取“NULL”数据(当我从数据库获取数据时)我正在尝试在NSUserDefaults中保存attribute6数据,但应用程序崩溃并低于错误。

[User Defaults] Attempt to set a non-property-list object <null> as an NSUserDefaults/CFPreferences value for key MyKeyName
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object null for key MyKeyName'

1 个答案:

答案 0 :(得分:1)

如果您希望它不为空,请确保在读取之前在DB中写入了attribute6的数据。如果您只想在从DB读取后直接保存值,并且允许attribute6为空,那么在尝试将[NSNull null] NSUserDefaults NSUserDefaults保存为[NSNull null]之前,您必须检查[NSNull null]的值。 }不接受//assuming the object is named attribute if (attribute != [NSNull null]) { [[NSUserDefaults standardUserDefaults] setObject:attribute forKey:MyKeyName]; } 个对象。

编辑: 从您的评论来看,该值最初为import pandas as pd dict ={"country":["Japan","Japan","Japan"], "date":["t1","t2","t3"], "value":[123,456,789]} df = pd.DataFrame(dict) print(df) 。所以你可以这样处理它:

inviteFriend = async () => {
    // only canonicalIdentifier is required
    let branchUniversalObject = await Branch.createBranchUniversalObject('ShareFromApp', {
        automaticallyListOnSpotlight: true,
        title: 'What do I put here...?',
        contentDescription: 'This isn\'t content, I just want to share the app'
    });

    let shareOptions = { messageHeader: 'Check out Foobar', messageBody: 'Check out Foobar so we can connect!' }
    let linkProperties = { feature: 'share', channel: 'App' }
    let controlParams = {}
    let {channel, completed, error} = await branchUniversalObject.showShareSheet(shareOptions, linkProperties, controlParams);
}