首页 \ 问答 \ 将非属性列表项保存到NSUserDefaults(Saving non-property list items to NSUserDefaults)

将非属性列表项保存到NSUserDefaults(Saving non-property list items to NSUserDefaults)

我有一个自定义对象数组,我想存储在更新UITableView的数组中。 该阵列非常小〜最多10个对象。 但是,我发现我无法保存自定义对象数组,因为:

 Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')
2015-01-04 17:56:33.414 fanSyncDemo[13985:1264828] Attempt to set a non-property-list object (

看起来我将不得不使用NSKeyArchiver将对象转换为NSData对象。 但是,我无法理解它的外观。 有人可以帮我举个例子吗? 我保存的代码如下所示:

- (IBAction)savePressed:(id)sender {

    if (_NotificationsSegmentedControl.selectedSegmentIndex == 0){
        _notificationBool = @"Yes";

    }
    else{
        _notificationBool = @"No";
    }

    MyFavorite *favorite = [[MyFavorite alloc] initWithName:_nameFavoriteTextField.text withLocation:@"FANLOCATION" withIsOnNotificationsScreen:_notificationBool];



    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *favoritesList = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:@"favoritesList"]];
    [favoritesList addObject:favorite];
    [defaults setObject:favoritesList forKey:@"favoritesList"];
    [defaults synchronize];
}

这不是这个问题的重复因为这个例子没有解释如何检索数据,我也无法弄清楚如何将它应用到我的例子中,这通常我没有问题,但我一直在努力这个。


I have an array of custom objects which I want to store in array that updates the UITableView. The array is quite small ~10 objects at most. However, I discovered that I am unable to save an array of custom objects because:

 Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')
2015-01-04 17:56:33.414 fanSyncDemo[13985:1264828] Attempt to set a non-property-list object (

It looks like I am going to have to turn the objects into NSData objects using NSKeyArchiver. However, I am having trouble understanding how this will look. Could someone help me with an example? My code for the saving looks like this:

- (IBAction)savePressed:(id)sender {

    if (_NotificationsSegmentedControl.selectedSegmentIndex == 0){
        _notificationBool = @"Yes";

    }
    else{
        _notificationBool = @"No";
    }

    MyFavorite *favorite = [[MyFavorite alloc] initWithName:_nameFavoriteTextField.text withLocation:@"FANLOCATION" withIsOnNotificationsScreen:_notificationBool];



    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *favoritesList = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:@"favoritesList"]];
    [favoritesList addObject:favorite];
    [defaults setObject:favoritesList forKey:@"favoritesList"];
    [defaults synchronize];
}

This is not a duplicate of this question because that example doesn't explain how to retrieve the data, nor can I figure out how to apply it to my example which normally I don't have a problem with but I have just been struggling with this.


原文:https://stackoverflow.com/questions/27771360
更新时间:2023-07-03 20:07

最满意答案

如果你的names是一个指针数组const char* names[]并按照你的方式初始化它们,那么你可以执行以下操作:

#include <stdio.h>

int main()
{
   const char* names[] = {"apples", "oranges", "grapes"};

   const char* first = names[0];
   const char* second = names[1];
   const char* third = names[2];

   const char* foo = &(*names[0]);

   printf("%s", foo);
   printf("%s", second);
   printf("%s", third);

}

实例

如果你想要地址,你可以这样做:

 const char* addr = &(*names[0]); //print addr gets "apples"
 const char** add = &names[0]; //print add gets 0x7fff14531990

If you make your names an array of pointers const char* names[] and initialize them as you did, then you can do the following:

#include <stdio.h>

int main()
{
   const char* names[] = {"apples", "oranges", "grapes"};

   const char* first = names[0];
   const char* second = names[1];
   const char* third = names[2];

   const char* foo = &(*names[0]);

   printf("%s", foo);
   printf("%s", second);
   printf("%s", third);

}

Live Example

If you want the address you can do this:

 const char* addr = &(*names[0]); //print addr gets "apples"
 const char** add = &names[0]; //print add gets 0x7fff14531990

相关问答

更多
  • 它应该是 bufferType vec(buffer, buffer + size); 不 bufferType vec(buffer, size); It should be bufferType vec(buffer, buffer + size); not bufferType vec(buffer, size);
  • 几年前我也遇到过这个同样的问题,它让我无所作为。 C中的规则更简单地表示(即它们不列出例外,将char**转换为const char*const* )。 相反,这是不允许的。 使用C ++标准,它们包含更多的规则来允许这样的情况。 最后,这只是C标准的一个问题。 我希望下一个标准(或技术报告)能够解决这个问题。 I had this same problem a few years ago and it irked me to no end. The rules in C are more simply s ...
  • 如您所定义的那样, nullchar是一个值为0的整数常量表达式。 C ++ 03标准定义了一个空指针常量:“空指针常量是一个整数类型的积分常数表达式(5.19)rvalue,计算结果为零。 为了使一个漫长的故事简短,您的nullchar是一个空指针常量,这意味着它可以隐式转换并分配给基本上任何指针。 请注意,所有这些元素是隐式转换工作所必需的。 例如,如果您使用'\1'而不是'\0' ,或者您没有为nullchar指定const限定符, nullchar不会得到隐式转换 - 您的作业将失败。 包含这种转换 ...
  • 问题是你无法将const变量传递给期望非const参数的函数。 其他字, const char *是const char *的子集。 删除const /*const*/ char* cmd_left[v.size()+1]; 在这里添加const_cast cmd_left[i] = const_cast( v.at(i).c_str() ); 你的代码的其他部分看起来很可疑,但这会让它编译 The problem is you cannot pass const variable to ...
  • 写下这样的作业: const char *SendBuf = "Hello"; 您无需对函数调用执行任何操作。 ( const char*参数将char*变量作为输入。) Write the assignment like this: const char *SendBuf = "Hello"; You don't have to do anything about the function call. (A const char* parameter will take a char* variabl ...
  • 如果你的names是一个指针数组const char* names[]并按照你的方式初始化它们,那么你可以执行以下操作: #include int main() { const char* names[] = {"apples", "oranges", "grapes"}; const char* first = names[0]; const char* second = names[1]; const char* third = names[2]; ...
  • 第一个实现的问题是您返回一个指向临时内存的指针。 因为'string data'变量仅在该函数的范围内定义,所以当函数消失时,您刚刚返回的指针现在指向free'd内存。 在您的情况下,它必须被全部零覆盖。 这就是你看到NULL的原因。 The problem with your first implementation is that you return a pointer to temporary memory. Because the 'string data' variable is only de ...
  • 如果它是一个常量,那就意味着它不应该被删除,因为它没有被分配:调用者很容易将一个基于栈的数组传递给你的方法,释放它将是一件坏事 。 最常见的约定是,无论代码分配一些数据应该释放数据。 因为它无法知道这是否是合法的事情,所以解决这个问题真的不是你的常规责任。 If it's a constant, that implies that it shouldn't be deleted, because it wasn't allocated: the caller could easily have passed ...
  • 您所观察到的是未定义的行为。 对toLatin1()的调用会创建一个临时的QByteArray ,它会在该行之后立即销毁,因为您不存储它。 data()获得的指针悬空,可能会或可能不会打印有用的东西。 正确版本: const QByteArray& latinName = name.toLatin1(); const char* strName = latinName.data(); What you are observing is undefined behavior. The call to toL ...
  • 不,指向const char *指针与其他指针的工作方式不同。 你的代码有什么不同之处在于C ++输出流(特别是)如何处理const char *与它们如何处理其他指针。 它通过提供operator<<()不同重载来实现。 一个重载将const char *作为参数,它是ASSUMED以指向nul终止的char数组(这是字符串文字如"Hello World"在内存中的表示)并输出每个char直到达到nul。 另一个重载接受void * ,并简单地输出值(即地址)。 这依赖于将指针类型隐式转换为void * ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)