我正在尝试将数组添加到 iPhone 上的 SQLite 数据库中。问题是,我不知道用户想要使用多少个数组对象。可能是 1,也可能是 10。如果我在 SQLite 表中分配了 10 个字段,那么如果用户使用一个字段,那将是 9 种浪费,并且它会将上限设置为 10 个条目。
因此我想到使用单个 VARCHAR 类型字段,并在数组的每个对象之间添加特殊字符。这对性能有何影响?还有更好的想法吗?
最佳答案
我的猜测是使用 NSCoder 框架。由于 NSArray 符合 NSCoding 协议(protocol),因此序列化已经完成:
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:myArray forKey:myArchiveKey];
[archiver finishEncoding];
[archiver release];
// write here data object to a BLOB field of your SQLite db
解码将非常相似
关于iphone - 在 SQLite 中保存数组的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9534867/