Swift - 创建参数对象

时间:2016-06-12 16:48:26

标签: json swift

我在为alamofire创建参数对象时遇到问题。 一些String键没有“”和一些字符串值。

你们能告诉我我做错了吗?

provider_user_id provider_uid

装置 - > DPI 名称 OS

maker-> 名称

有人可以向我解释原因:

func callFineBackend(provider_uid:String!,provider_user_id:String!) -> String{
    print("get token and post")

    let screen = UIScreen.mainScreen().bounds
    let res_x = Int(screen.width)
    let res_y = Int(screen.height)
    let devicename = String(UIDevice.currentDevice().name)
    let os = String(UIDevice.currentDevice().systemVersion)
    let mem_mb = Double(NSProcessInfo.processInfo().physicalMemory)
    let provider_name = "facebook"
    let dpi = 126


    let xlatGPS = 34.951231
    let xlonGPS = 43.714323
    let stringLat = String(xlatGPS)
    let stringLon = String(xlonGPS)

    let headers: [String: String] = [
        "Accept": "application/json",
        "Content-Type":"application/json",
        "X-Latitude": stringLat,
        "X-Longitude": stringLon
    ]

    let parameters: [String: AnyObject] = [

        "maker": [
            "name": "Apple"
        ],
        "device": [
            "name": devicename,
            "mem_mb": mem_mb,
            "dpi": dpi,
            "res_x": res_x,
            "rex_y": res_y,
            "os": os
        ],
        "provider_name": provider_name,
        "provider_user_id": provider_user_id,
        "provider_uid": provider_uid,
    ]

    print(parameters)

变成:

    [
  "provider_user_id": 12315123512, "device": {
    dpi = 126;
    "mem_mb" = 4294967296;
    name = "iPhone Simulator";
    os = "9.3";
    "res_x" = 320;
    "rex_y" = 480;
}, "provider_uid": EAAX, 
"maker": {
    name = Apple;
}, "provider_name": facebook
]

1 个答案:

答案 0 :(得分:0)

打印的内容没有错。

打印int main() { class Vector2 { public: Vector2(float x, float y) { this->x = x; this->y = y; } float x = 0.f; float y = 0.f; }; struct Test { int x; Vector2 v; }; Test tst = {0,Vector2(4,5)}; return 0; } 时,只有当字符串包含非字母或数字的字符时,字符串才会被引号括起来。

在这种情况下,NSDictionary的类型为parameters, 但Dictionaryparameters["maker"]等属于parameters["device"]类型。