用Freddy(json)编码2D数组

时间:2017-05-06 21:44:26

标签: json swift multidimensional-array

尝试编码变量,包括使用JSON框架Freddy的Int的2D数组。我收到的错误很明显,"无法转换类型' [[Int]]'预期的参数类型' [JSON]'",但我不确定通过符合Freddy.JSONEncodeable来完成myArray编码的正确方法。在两个维度中,myArray的大小可能并不总是相同。

import UIKit
import Freddy


class MyClass: NSObject, JSONEncodable {

var myArray: [[Int]] = []
var myType:String = ""
var myName:String = ""


public override init() {
    super.init()

    myName = "Freddy"
    myType = "plaid"

    myArray = Array(repeating: Array(repeating: 0, count: 7), count: 7)
    myArray[0] = [0,0,1,1,1,0,0]
    myArray[1] = [0,1,1,1,1,1,0]
    myArray[2] = [1,1,1,1,0,0,0]
    myArray[3] = [1,1,0,0,0,0,0]
    myArray[4] = [1,1,1,1,0,0,0]
    myArray[5] = [0,1,1,1,1,1,0]
    myArray[6] = [0,0,1,1,1,0,0]  
 }

//MARK: JSONEncodable 

public func toJSON() -> JSON {   
        let returnDictionary = JSON.dictionary(["type": .string(self.myType),
                                                "name": .string(self.myName),
                                                "myarray": .array(self.myArray)])
        return returnDictionary
}

}

0 个答案:

没有答案