2017-10-31 6 views
0

내 프로젝트에 https://github.com/ranmyfriend/FirebasePhoneAuth과 같은 것을 구현하려고했습니다. 내 프로젝트에 JSONReader.swift 있습니다JSON 파일에서 nil을 반환합니다.

import Foundation 

public struct JSONReader{ 
    static func countries()->[Country] { 
     let url = Bundle.main.url(forResource: "cCodes", withExtension: "json") 
     let data = try! Data.init(contentsOf: url!) 
     do { 
      let wrapped = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [[String:Any]] 
      return wrapped.map({ Country.init(object: $0) }) 
     } catch { 
      // Handle Error 
      debugPrint(error) 
      return [] 
     } 
    } 
} 

Country.swift 파일 :

import Foundation 
import SwiftyJSON 

fileprivate let baseScalar: UInt32 = 127397 

public struct Country { 

    // MARK: Declaration for string constants to be used to decode and also serialize. 
    private let kCountryNameKey: String = "name" 
    private let kCountryE164CcKey: String = "e164_cc" 
    private let kCountryGeographicKey: String = "geographic" 
    private let kCountryDisplayNameKey: String = "display_name" 
    private let kCountryE164ScKey: String = "e164_sc" 
    private let kCountryIso2CcKey: String = "iso2_cc" 
    private let kCountryE164KeyKey: String = "e164_key" 
    private let kCountryLevelKey: String = "level" 
    private let kCountryExampleKey: String = "example" 
    private let kCountryDisplayNameNoE164CcKey: String = "display_name_no_e164_cc" 
    private let kCountryFullExampleWithPlusSignKey: String = "full_example_with_plus_sign" 

    // MARK: Properties 
    public var name: String? 
    public var e164Cc: String? 
    public var geographic: Bool = false 
    public var displayName: String? 
    public var e164Sc: Int? 
    public var iso2Cc: String? 
    public var e164Key: String? 
    public var level: Int? 
    public var example: String? 
    public var displayNameNoE164Cc: String? 
    public var fullExampleWithPlusSign: String? 
    public var flag: String? 

    // MARK: SwiftyJSON Initalizers 
    /** 
    Initates the instance based on the object 
    - parameter object: The object of either Dictionary or Array kind that was passed. 
    - returns: An initalized instance of the class. 
    */ 
    public init(object: Any) { 
    self.init(json: JSON(object)) 
    } 

    /** 
    Initates the instance based on the JSON that was passed. 
    - parameter json: JSON object from SwiftyJSON. 
    - returns: An initalized instance of the class. 
    */ 
    public init(json: JSON) { 
    name = json[kCountryNameKey].string 
    e164Cc = json[kCountryE164CcKey].string 
    geographic = json[kCountryGeographicKey].boolValue 
    displayName = json[kCountryDisplayNameKey].string 
    e164Sc = json[kCountryE164ScKey].int 
    iso2Cc = json[kCountryIso2CcKey].string 
    e164Key = json[kCountryE164KeyKey].string 
    level = json[kCountryLevelKey].int 
    example = json[kCountryExampleKey].string 
    displayNameNoE164Cc = json[kCountryDisplayNameNoE164CcKey].string 
    fullExampleWithPlusSign = json[kCountryFullExampleWithPlusSignKey].string 
    flag = iso2Cc?.unicodeScalars.flatMap { String.init(UnicodeScalar(baseScalar + $0.value)!) }.joined() 
    } 

    /** 
    Generates description of the object in the form of a NSDictionary. 
    - returns: A Key value pair containing all valid values in the object. 
    */ 
    public func dictionaryRepresentation() -> [String: Any] { 
    var dictionary: [String: Any] = [:] 
    if let value = name { dictionary[kCountryNameKey] = value } 
    if let value = e164Cc { dictionary[kCountryE164CcKey] = value } 
    dictionary[kCountryGeographicKey] = geographic 
    if let value = displayName { dictionary[kCountryDisplayNameKey] = value } 
    if let value = e164Sc { dictionary[kCountryE164ScKey] = value } 
    if let value = iso2Cc { dictionary[kCountryIso2CcKey] = value } 
    if let value = e164Key { dictionary[kCountryE164KeyKey] = value } 
    if let value = level { dictionary[kCountryLevelKey] = value } 
    if let value = example { dictionary[kCountryExampleKey] = value } 
    if let value = displayNameNoE164Cc { dictionary[kCountryDisplayNameNoE164CcKey] = value } 
    if let value = fullExampleWithPlusSign { dictionary[kCountryFullExampleWithPlusSignKey] = value } 
    return dictionary 
    } 

} 

및 JSON 파일 내 프로젝트에 cCodes.json라고 :

[ 
    { 
    "e164_cc": "93", 
    "iso2_cc": "AF", 
    "e164_sc": 0, 
    "geographic": true, 
    "level": 1, 
    "name": "Afghanistan", 
    "example": "7", 
    "display_name": "Afghanistan (AF) [+93]", 
    "full_example_with_plus_sign": "+937", 
    "display_name_no_e164_cc": "Afghanistan (AF)", 
    "e164_key": "93-AF-0" 
    }, 
    { 
    "e164_cc": "263", 
    "iso2_cc": "ZW", 
    "e164_sc": 0, 
    "geographic": true, 
    "level": 1, 
    "name": "Zimbabwe", 
    "example": "711234567", 
    "display_name": "Zimbabwe (ZW) [+263]", 
    "full_example_with_plus_sign": "+263711234567", 
    "display_name_no_e164_cc": "Zimbabwe (ZW)", 
    "e164_key": "263-ZW-0" 
    } 
] 

그러나 알 수없는 이유에 대한 오류와 시작시 응용 프로그램 충돌 : 스레드 1 : EXC_BAD_INSTRUCTION (코드 = EXC_I386_INVOP, 서브 코드 = 0x0으로)

여기에 오류 콘솔입니다 : 그래서 enter image description here

내가 그 URL을 이해로 변수는 nil을 반환하지만 json 파일이있을 때 nil을 반환 할 수있는 방법은 비어 있지 않습니다.

내가 누락 된 항목이 있습니까?

+0

당신은 국가의 선언을 포함 할 수 있습니까 –

+0

@VivekMolkar 지금 코드를 추가했습니다 –

+0

대답 plz 수표를 게시하고 그것이 작동하는 경우 알려주십시오! –

답변

0

귀하의 코드는 괜찮습니까? 그리고 나를 위해 결과를 생성합니다.

내가 이해할 수있는 한, .json 파일을 대상 회원에 추가하지 못했습니다.

대상 회원 정보를 추가하면 문제가 해결됩니다. 참조 스크린 샷 :

enter image description here

는 희망이 도움이!

+0

감사합니다. –

관련 문제