我想归档一个使用简单枚举作为密钥的字典:
public enum DaysOfWeek: Int, Codable {
case Sunday = 1
case Monday
case Tuesday
case Wednesday
case Thursday
case Friday
case Saturday
}
字典是:
var myDict: Dictionary<DaysOfWeek, [SomeDataType]>
我尝试过编码:
aCoder.encode(myDict, forKey: "MyKey")
但是这会导致匿名的NSException错误:
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)bt
* thread#1,queue ='com.apple.main-thread',stop reason = signal SIGABRT
帧#0:0x0000000183b75348 libsystem_kernel.dylib __pthread_kill + 8
frame #1: 0x0000000183c89344 libsystem_pthread.dylib
pthread_kill $ VARIANT $ mp + 396
第2帧:0x0000000183ae4fb8 libsystem_c.dylib abort + 140
frame #3: 0x0000000183284068 libc++abi.dylib
abort_message + 132
第4帧:0x0000000183284210 libc ++ abi.dylib default_terminate_handler() + 304
frame #5: 0x00000001832ac810 libobjc.A.dylib
_ objc_terminate()+ 124
帧#6:0x000000018329c54c libc ++ abi.dylib std::__terminate(void (*)()) + 16
frame #7: 0x000000018329c158 libc++abi.dylib
__ cxa_rethrow + 144
帧#8:0x00000001832ac6e8 libobjc.A.dylib objc_exception_rethrow + 44
frame #9: 0x0000000183f24024 CoreFoundation
CFRunLoopRunSpecific + 544
帧#10:0x0000000185dbbf84 GraphicsServices GSEventRunModal + 100
frame #11: 0x000000018d4f82f4 UIKit
UIApplicationMain + 208
*帧#12:0x00000001026fbe98 CycleTrainer main at AppDelegate.swift:18
frame #13: 0x0000000183a4656c libdyld.dylib
开始+ 4
我缺少什么?
答案 0 :(得分:0)
为什么不使用枚举的rawValue
代替?因此,您将能够对字典进行编码。
var myDict: [Int: SomeDataType] = [:]
myDict[DaysOfWeek.Tuesday.rawValue] = SomeDataType() // Assign some encodable data type.