Ich habe versucht, etwas wie https://github.com/ranmyfriend/FirebasePhoneAuth auf meinem Projekt zu implementieren. Ich habe die JSONReader.swift in meinem Projekt:JSON-Datei gibt keine
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 Datei:
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
}
}
und JSON-Datei cCodes.json in meinem Projekt genannt:
[
{
"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"
}
]
Aber aus unbekannten Gründen die App stürzt beim Start mit Fehler: Thema 1: EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, Subcode = 0x0)
und hier ist die Fehlerkonsole:
So wie ich diese URL verstanden Variable gibt nil zurück, aber wie kann es nil zurückgeben, wenn die json-Datei da ist und nicht leer ist?
Fehle ich etwas?
können Sie die Deklaration des Landes –
@VivekMolkar einschließen Ich habe den Code jetzt –
gepostet die Antwort plz überprüfen und lassen Sie mich wissen, ob es funktioniert! –