2017-11-14 1 views
1

PHP에서 응답을 신속 사전으로 변환하려고하지만이를 수행하는 방법을 찾지 못했습니다.'NSTaggedPointerString'유형의 값을 'NSNumber'로 변환 할 수 없습니다 - PHP에서 Swift로

  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 

두 번째 인쇄, 다음이

같아야 PHP에서 convertInsideData[k as! String] = v as! Int

{ 
    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). 

응답에 충돌 난 당신이 선택적 항목을 사용하는 것이 좋습니다

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

답변

2

는 다음 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 
    } 
} 
+0

비슷한 오류가 발생합니다. ''__NSCFNumber '(0x10f77d008) 형식의 값을'NSString '으로 변환 할 수 없습니다 –

+0

025 인 경우 일부 요소가 누락되는 경우 사용할 수 없습니다 –

+0

가끔 내 V 값이 String 인 것으로 보입니다 답변을 편집했습니다. 때로는 NSNumber. v 값이 nil인지 확인하기 위해 else를 추가 할 수 있습니다. –

관련 문제