Alamofire在Int上转换bool

时间:2019-01-28 14:15:29

标签: swift alamofire

我对请求有func,对参数有字典。字典包含[String,Any]。而且我需要在参数中发送Bool,但是alamofire在Int上转换我的Bool,服务器无法获取它。我如何发送布尔?

我的功能:

Child

我尝试发送:

class Parent {
  constructor() {
    this._x = 255;
  }
  set x(v) {
    console.log("Calling Parent setter");
    this._x = v;
  }
  get x() {
    console.log("Calling Parent getter");
    return this._x;
  }
}

class Child extends Parent {
  constructor() {
    super();
    this.prop = new Prop(this);
  }

  set x(v) {
    console.log("AVOID! Calling Child setter");
    super.x = v;
    // Shennanigans I don't want to run
  }

  get x() {
    console.log("Calling Child getter");
    return super.x;
  }
}

class Prop {
  constructor(child) {
    this.child = child;
  }
  setX() {
    const parent = this.child; // Not sure what to do here.
    const old = parent.x;
    parent.x = 0;
    console.log(`parent.x changed from ${old} to ${parent.x}`);
  }
}

const child = new Child();
child.prop.setX();

但这会发送alamofire:

func postDataFor(methodPath: String, params:[String:Any], completion: @escaping (Any)->()) {
    guard let url = URL(string:baseURL+methodPath) else {print("fail url"); return}

        let token_type = KeychainWrapper.standard.string(forKey: "token_type")
        let access_token = KeychainWrapper.standard.string(forKey: "access_token")

        let headers:HTTPHeaders? = ["Authorization": "\(token_type ?? "") \(access_token ?? "")"]


        Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.queryString, headers: headers).response { response in

            if let data = response.data {
                completion(data)
            } else {
           print("error")
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我猜所有这些值都存储在params中。如果是这样,请将isFavorite更改为字符串:

let params = [
    "promoId": 6,
    "isFavorite": String(isFavorite)
]