我正在使用桥接从 objective-c 调用一个 swift 函数。同时传递一个带有键值对的 NSMutableDictionary。一个值(op_status)虽然在调用方完好无损,但在收到时变为空值。附加代码:
NSMutableDictionary *offlineRequestData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
soId, @"soId",
AddNoteScreen, @"screen",
NotesOperation, @"operation",
jsonBlob, @"jsonBlob",
createdDate, @"created_datetime",
[NSNumber numberWithInt:OperationNotSynced], @"op_status",
@"", @"sync_datetime", nil];
//OperationNotSynced 是一个枚举。
--
//在 Swift 方面: //在使用字典的 Swift 函数中
newRecord.op_status = valuesDictionary["op_status"] as? NSNumber
op_status 在这里变为空。
另外,打印出 valuesDictionary["op_status"] 给我 optional(int value) 并展开打印值。但它不会转换为 NSNumber。
感谢任何帮助。
最佳答案
由于 op_status
是一个 Int?
,您可以有条件地将它转换为一个 Int
并创建一个 NSNumber
带有初始值设定项。
if let opStatusInt = valuesDictionary["op_status"] as? Int {
newRecord.op_status = NSNumber(value: opStatusInt)
}
关于objective-c - 从 Objective C 调用 Swift 函数时未转换字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39152497/