iphone - FMDB 存储 typedef 枚举值

标签 iphone objective-c ios sqlite enums

我正在使用 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/

相关文章:

ios - UITableViewCell 子类或原型(prototype)单元格

ios - UITableView 在 ios 中使用自定义表格单元格滚动不流畅

iphone - 使用 SVN 时如何解决 xcode 4 中的冲突?

ios - 将 IBOutlet 添加到自定义类

iphone - NSUserDefaults 重置

iphone - 实时图像处理 (OCR)

ios - WKWebView 的缓存

objective-c - 当父级折叠时,NSOutlineView ParentForItem 返回 nil

ios - iOS 的事件处理 - 如何 hitTest :withEvent: and pointInside:withEvent: are related?

objective-c - Objective C 中的 Scanf 导致 Xcode 和程序崩溃