我正在尝试将数组转换为json但无法使其工作。它说数组是一个无效的类型转换为json,但它只是一个int数组。
//These are for me to able to use time
let date = NSDate()
let calendar = NSCalendar.current
//This is the structure that my array will be filled with
struct Day
{
var week: Int?
var date: Int?
var month: Int?
var year: Int?
var weekday: Int?
}
//This is my array
var theArrayContainingAllTheUserData = [Day]()
var numberofloops = 0
//This is a loop that fills my array with data
while theArrayContainingAllTheUserData.count < 20
{
var theincreasingnumberofdays: NSDate {
return NSCalendar.current.date(byAdding: .day, value: numberofloops, to: NSDate() as Date)! as NSDate
}
let myCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
numberofloops=numberofloops+1
var ScoopDay = Day()
ScoopDay.week=Int(myCalendar.component(.weekOfYear, from: theincreasingnumberofdays as Date!))
ScoopDay.date=Int(myCalendar.component(.day, from: theincreasingnumberofdays as Date!))
ScoopDay.month=Int(myCalendar.component(.month, from: theincreasingnumberofdays as Date!))
ScoopDay.year=Int(myCalendar.component(.year, from: theincreasingnumberofdays as Date!))
ScoopDay.weekday=Int(myCalendar.component(.weekday, from: theincreasingnumberofdays as Date!))
theArrayContainingAllTheUserData.append(ScoopDay)
}
//And here is my failed attempt at converting it to json
do {
let jsonData = try JSONSerialization.data(withJSONObject: theArrayContainingAllTheUserData, options: JSONSerialization.WritingOptions.prettyPrinted)
} catch {
print(error.localizedDescription)
}
获取错误:
2016-10-28 13:20:09.669 jsona arrayen [3284:332873] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'JSON写入的无效类型(_SwiftValue)'
First throw call stack:
(
0 CoreFoundation 0x000000010840834b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000107e6921e objc_exception_throw + 48
2 CoreFoundation 0x0000000108471265 +[NSException raise:format:] + 197
3 Foundation 0x000000010795d284 _writeJSONValue + 668
4 Foundation 0x000000010795d6d9 ___writeJSONArray_block_invoke + 132
5 CoreFoundation 0x00000001083ae36f __NSArrayEnumerate + 607
6 Foundation 0x000000010795d3df _writeJSONArray + 330
7 Foundation 0x000000010795d204 _writeJSONValue + 540
8 Foundation 0x000000010795cf94 -[_NSJSONWriter dataWithRootObject:options:error:] + 124
9 Foundation 0x000000010795ce74 +[NSJSONSerialization dataWithJSONObject:options:error:] + 333
10 ??? 0x0000000116ddd3cb 0x0 + 4678603723
11 jsona arrayen 0x0000000107892620 main + 0
12 CoreFoundation 0x00000001083ad25c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
13 CoreFoundation 0x0000000108392304 __CFRunLoopDoBlocks + 356
14 CoreFoundation 0x0000000108391a75 __CFRunLoopRun + 901
15 CoreFoundation 0x0000000108391494 CFRunLoopRunSpecific + 420
16 GraphicsServices 0x000000010d796a6f GSEventRunModal + 161
17 UIKit 0x0000000108f3bf34 UIApplicationMain + 159
18 jsona arrayen 0x00000001078926e9 main + 201
19 libdyld.dylib 0x000000010b8e968d start + 1
20 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException