自定义类型数组Alamofire参数:' JSON写入中的类型无效

时间:2016-02-18 18:28:10

标签: swift swift2 alamofire

我正在尝试将一组自定义类型内联到dir中 - 作为Alamofire的参数。 Alamofire在'Invalid type in JSON write

ParameterEncoding.swift: Line142失败
case .JSON:
    do {
        let options = NSJSONWritingOptions()
        let data = try NSJSONSerialization.dataWithJSONObject(parameters, options: options) //  'Invalid type in JSON write

MyClass的

class MyClass{
    var name: String = ""
    var address: String = ""
}

这给出了错误:

class MyParameters{

    var MyClasses: [MyClass] = []

    var dictionary: [String: AnyObject]{
        get {
            return [
                "myclasses": self.MyClasses,
                ]
        }
    }
}

尝试了这个from

class MyClass{
    var name: String = ""
    var address: String = ""
}


class MyParameters{

    var MyClasses: [MyClass] = []

    var dictionary: [String: AnyObject]{
        get {
            return [
                "myclasses": self.MyClasses.map({$0.name}),
                ]
        }
    }
}

0 个答案:

没有答案