php - 无法将类型 'NSTaggedPointerString' 的值转换为 'NSNumber' - 从 PHP 到 Swift

标签 php ios swift type-conversion

我正在尝试将响应从 PHP 转换为 swift Dictionary,但我不知道该怎么做。

  1. 想知道为什么我使用 [String: [String: Int]] 不起作用。
  2. 尝试过使用 NSDictionary,但认为这不是正确的方法,但似乎差不多了。

快速代码

Alamofire.request(requestString, method: .post, parameters: data, encoding:URLEncoding.default, headers: [:]).responseJSON { (response) in
    switch response.result {
        case .success(_):
            if let response = response.result.value as? [String: Any] {
                let updatedData = response["existData"] as! NSDictionary
                print(updatedData)
                let updatedData2 = response["existData"] as? [String: [String: Int]]
                print("updatedData2", updatedData2)
                var convertData = [String: [String: Int]]()
                for key in Array(updatedData.allKeys) {
                    var convertInsideData = [String: Int]()
                    if let array = updatedData[key] as? NSDictionary {
                        for (k, v) in array {
                            print(k, "-", v)
                            convertInsideData[k as! String] = v as! Int
                        }
                    }
                    convertData[key as! String] = convertInsideData
                }
       .....................

第一次打印,

{
    All =     {
        Maybelline = 2;
    };
}
updatedData2 Optional(["All": ["Maybelline": 2]])
Maybelline - 2

第二次打印,然后在 convertInsideData[k as! String] = v as!整数

{
    All =     {
        Maybelline = 2;
        Sephora = 2;
    };
}
updatedData2 nil
Sephora - 2
Maybelline - 2
Could not cast value of type 'NSTaggedPointerString' (0x10e374c88) to 'NSNumber' (0x10d0ad600).
2017-11-14 23:52:04.939332+0800 LeanCloudStarter[10014:272299] Could not cast value of type 'NSTaggedPointerString' (0x10e374c88) to 'NSNumber' (0x10d0ad600).

PHP 的响应应该是这样的

$existData = array("All"=>array("Maybelline"=>2, "Sephora"=>2));
echo json_encode("existData"=>$existData));

最佳答案

我建议你使用 optionals,那么你的 Int 值在你的 Json 中似乎是一个 String,试试:

for (k, v) in array {
    print(k, "-", v)
    if let stringV = v as? String {
        convertInsideData[k as! String] = Int(stringV)
    } else if let numberV = v as? NSNumber {
        convertInsideData[k as! String] = v.intValue
    }
}

关于php - 无法将类型 'NSTaggedPointerString' 的值转换为 'NSNumber' - 从 PHP 到 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47290025/

相关文章:

php - 将所有请求路由到子文件夹 htaccess

ios - 暂时禁用旋转(当动画运行时)

ios - 在静态表格 View 单元格中将文本设置为动态

javascript - iOS 7 键盘启动时未捕获表单提交按钮事件

ios - CLLocation 'coordinate' 不可用

php - 将关联数组转换为具有值的多维数组

不同用户的php登录系统

php - 使用 select 语句和 php 变量插入 MySQL

json - 为什么某些变量为零?

ios - 显示和隐藏 UIButton