线程 1:在 JSONSerialization 中发出 SIGABRT 信号

Thread 1: signal SIGABRT in JSONSerialization

我正在创建 array 的切片并将其放入 JSONSerialization 并且编译器因 SIGABRT.

而失败
    let array = [1,2,3,4,5,6]
    let arraySlice = array[1...3]
    print(String(data: try! JSONSerialization.data(withJSONObject: arraySlice, options: .prettyPrinted), encoding: .utf8 )!)

Thread 1: signal SIGABRT

如何序列化数组切片?

JSONSerialization 不适用于 ArraySlice,您首先需要将其转换为 Array:

print(String(data: try! JSONSerialization.data(withJSONObject: Array(arraySlice), options: .prettyPrinted), encoding: .utf8 )!)