将非属性列表项保存到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
最满意答案
如果你的
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 pointersconst 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); }
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 *”?(Why can't I convert 'char**' to a 'const char* const*' in C?)[2023-07-20]
几年前我也遇到过这个同样的问题,它让我无所作为。 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 ... -
为什么C ++允许我为const char *分配一个const char?(Why is C++ allowing me to assign a const char to a const char *?!)[2023-03-30]
如您所定义的那样, 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 to char问题(const char to char problem)[2022-03-04]
写下这样的作业: 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]; ... -
const char *返回类型(const char* Return Type)[2022-02-22]
第一个实现的问题是您返回一个指向临时内存的指针。 因为'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 ... -
const char *和free()(const char* and free())[2022-12-16]
如果它是一个常量,那就意味着它不应该被删除,因为它没有被分配:调用者很容易将一个基于栈的数组传递给你的方法,释放它将是一件坏事 。 最常见的约定是,无论代码分配一些数据应该释放数据。 因为它无法知道这是否是合法的事情,所以解决这个问题真的不是你的常规责任。 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 * ...