简体   繁体   English

JSON文件返回nil

[英]JSON file returns nil

I've been trying to implement something like https://github.com/ranmyfriend/FirebasePhoneAuth on my project. 我一直在尝试在我的项目中实现类似https://github.com/ranmyfriend/FirebasePhoneAuth的功能。 I have the JSONReader.swift in my project: 我的项目中有JSONReader.swift

    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 file: 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
  }

}

and JSON file called cCodes.json in my project: 和我的项目中名为cCodes.json的 JSON文件:

    [
  {
    "e164_cc": "93",
    "iso2_cc": "AF",
    "e164_sc": 0,
    "geographic": true,
    "level": 1,
    "name": "Afghanistan",
    "example": "701234567",
    "display_name": "Afghanistan (AF) [+93]",
    "full_example_with_plus_sign": "+93701234567",
    "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"
  }
]

But for unknown reason the app crashes at startup with error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) 但是由于未知原因,应用程序在启动时因错误而崩溃:线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

and here is the error console: 这是错误控制台: 在此处输入图片说明

So as I understood that url variable returns nil, but how it can returns nil when the json file is there and not empty !? 因此,据我了解,url变量返回nil,但是当json文件存在且不为空时,如何返回nil呢?

Am I missing something ? 我想念什么吗?

Your code is fine and generates the result for me. 您的代码很好,可以为我生成结果。

As far as I can understand, you missed adding .json file to Target Membership. 据我了解,您没有将.json文件添加到Target Membership中。

Adding target membership will solve your problem. 添加目标成员资格将解决您的问题。 See screenshot: 看截图:

在此处输入图片说明

Hope it helps!! 希望能帮助到你!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM