我一直试图在我的项目中实施https://github.com/ranmyfriend/FirebasePhoneAuth之类的东西。 我的项目中有 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文件:
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
}
}
和我项目中名为 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"
}
]
但由于未知原因,应用程序在启动时因错误而崩溃:线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)
因为我知道url变量返回nil,但是当json文件存在且不为空时它如何返回nil??
我错过了什么吗?
答案 0 :(得分:0)
您的代码很好并为我生成结果。
据我所知,您错过了将public function Adminlogin()
{
if(Auth::attempt(['email' => request('email'), 'password' => request('password')]))
{
$user = Auth::user();
if($user->hasRole('admin'));
{
$success['token'] = $user->createToken('MyApp')->accessToken;
return response()->json(['success' => $success], $this->successStatus);
}
return response()->json(['error'=>'Abort'], 403);
}
else
{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
文件添加到目标会员资格中。
添加目标会员资格将解决您的问题。 见截图:
希望它有所帮助!!