Iphone 存储typedef枚举值的FMDB

Iphone 存储typedef枚举值的FMDB,iphone,objective-c,ios,sqlite,enums,Iphone,Objective C,Ios,Sqlite,Enums,我使用FMDB将对象序列化到数据库中,我有一个不可为null的int列,我想在其中存储一个枚举值 Person.h @interface TreasureChest : NSObject { ... ComponentSorting componentSorting; } @property (nonatomic, readwrite) ComponentSorting componentSorting; @end typedef enum ComponentSorti

我使用FMDB将对象序列化到数据库中,我有一个不可为null的int列,我想在其中存储一个枚举值

Person.h

@interface TreasureChest : NSObject {
        ...
    ComponentSorting componentSorting;
}
@property (nonatomic, readwrite) ComponentSorting componentSorting;
@end
typedef enum ComponentSorting {
    kSortOrderName = 0,
    kSortOrderSortOrder = 1,
} ComponentSorting;
我在一个单独的类中声明
ComponentSorting

常数.h

@interface TreasureChest : NSObject {
        ...
    ComponentSorting componentSorting;
}
@property (nonatomic, readwrite) ComponentSorting componentSorting;
@end
typedef enum ComponentSorting {
    kSortOrderName = 0,
    kSortOrderSortOrder = 1,
} ComponentSorting;
当我尝试在下面的语句中插入值时,我得到一个“column ComponentSorting cannot be 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的背景,枚举几乎可以转换为整数,我不明白这为什么会导致问题。

我认为你把GDB的一般聪明与枚举的实际值混淆了。是的,它是一个整数(或多或少),您是否尝试过使用数据库浏览器检查数据库和插入的值?如果没有,也可以通过命令行尝试使用“sqlite3”。

强制转换无效:
(int)comp.componentSorting
,但转换为NSNumber则无效:
[NSNumber numberwhithint:(int)comp.componentSorting]
。但奇怪的是,我听说FMDB可能会对int和nsnumber很挑剔。

问题在于插入失败是由于空值。如果我将
替换为
0
,它会成功,但这不是一个解决方案。谢谢,部分原因在于此(请参阅我的答案)。