我正在使用 FMDB 将对象序列化到数据库中,我有一个不可为 null 的 int 列,我想在其中存储一个枚举值。
Person.h
@interface TreasureChest : NSObject {
...
ComponentSorting componentSorting;
}
@property (nonatomic, readwrite) ComponentSorting componentSorting;
@end
我在一个单独的类中声明 ComponentSorting
:
常量.h
typedef enum ComponentSorting {
kSortOrderName = 0,
kSortOrderSortOrder = 1,
} ComponentSorting;
当我尝试在以下语句中插入值时,出现“列 ComponentSorting 不能为 NULL”错误,就好像 FMDB 或 SQLite 的 iOS 实现忽略了枚举。
[db executeUpdate:@"INSERT INTO Components (ComponentID, Name, Description, ComponentSorting) VALUES (?,?,?,?)",
comp.componentId,
comp.name,
comp.description,
comp.componentSorting
]
调试显示comp.componentSorting
确实是kSortOrderName
(在对象的init
中设置)。来自 .NET 背景,枚举几乎可以转换为整数,我不明白为什么这会导致问题。
最佳答案
转换无效:(int)comp.componentSorting
但转换为 NSNumber 有效:[NSNumber numberWithInt:(int)comp.componentSorting]
。奇怪的是,我听说 FMDB 对 int 和 NSNumbers 很挑剔。
关于iphone - FMDB 存储 typedef 枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8636389/