Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取错误“;数据无法’;不要因为它不见了而被阅读;在使用alamofire和swift获取数据期间_Swift_Alamofire_Codable - Fatal编程技术网

获取错误“;数据无法’;不要因为它不见了而被阅读;在使用alamofire和swift获取数据期间

获取错误“;数据无法’;不要因为它不见了而被阅读;在使用alamofire和swift获取数据期间,swift,alamofire,codable,Swift,Alamofire,Codable,我使用Alamofire和codable从swift中的服务器获取数据。但我犯了一个错误 无法读取数据,因为缺少该数据 我的代码有什么错误吗?为什么我会犯这样的错误 我的代码: import Foundation // MARK: - Welcome struct UserDetails: Codable { let success: Int let message: String let result: UserResult enum CodingKeys: String, CodingK

我使用Alamofire和codable从swift中的服务器获取数据。但我犯了一个错误

无法读取数据,因为缺少该数据

我的代码有什么错误吗?为什么我会犯这样的错误

我的代码:

import Foundation

// MARK: - Welcome
struct UserDetails: Codable {
let success: Int
let message: String
let result: UserResult

enum CodingKeys: String, CodingKey {
    case success = "Success"
    case message = "Message"
    case result = "Result"
}
}

// MARK: - Result
struct UserResult: Codable {
let userID: Int
let firstName, location, state, about: String
let totalScore: Int
let sun, rising, moon: String
let ego, communication, birthStar, sexual: Int
let intellectual, temperament, emotional, healthGenetic: Int
let profilePic: String
let numberOfPhotos: Int
let imagelist: [Imagelist]
let verified: Int
let dateOfBirth, timeOfBirth: String
let age: Int

enum CodingKeys: String, CodingKey {
    case userID = "User_Id"
    case firstName = "First_Name"
    case location = "Location"
    case state = "State"
    case about = "About"
    case totalScore = "TotalScore"
    case sun
    case rising = "Rising"
    case moon = "Moon"
    case ego = "Ego"
    case communication = "Communication"
    case birthStar = "Birth_star"
    case sexual = "Sexual"
    case intellectual = "Intellectual"
    case temperament = "Temperament"
    case emotional = "Emotional"
    case healthGenetic = "Health_Genetic"
    case profilePic = "Profile_Pic"
    case numberOfPhotos = "NumberOfPhotos"
    case imagelist, verified
    case dateOfBirth = "DateOfBirth"
    case timeOfBirth = "TimeOfBirth"
    case age = "Age"
}
}

// MARK: - Imagelist
struct Imagelist: Codable {
let userID: Int
let imagePath: String

enum CodingKeys: String, CodingKey {
    case userID = "User_Id"
    case imagePath = "Image_path"
}
}


import Foundation
import Alamofire

class ApiRequest {

func getUserDetailData(_ userId: Int, _ accessToken: String, completion: @escaping UserProfileDetailsData) {

    guard let url = URL(string: "\(USER_PROFILE_URL)?UserID=\(userId)&RequestingUser_id=1")  else{return}
    var urlRequest = URLRequest(url: url)
    urlRequest.httpMethod = HTTPMethod.get.rawValue

    urlRequest.addValue(accessToken, forHTTPHeaderField: "Authorization")

    Alamofire.request(urlRequest).responseJSON { (response) in
        if let error = response.result.error {
            debugPrint(error.localizedDescription)
            completion(nil)
            return
        }

        guard let data = response.data else { return completion(nil)}
        let jsonDecoder = JSONDecoder()
        do {
            let person = try jsonDecoder.decode(UserDetails.self, from: data)

            completion(person)
        } catch {
            debugPrint(error.localizedDescription)
            completion(nil)
        }
    }

}
}
在我的常量类中,我有以下两个代码

let USER_PROFILE_URL = URL_BASE + "GetUserProfile/"

typealias UserProfileDetailsData = (UserDetails?) -> Void
我在视图控制器中调用api,如下所示:

 func getUserDetailsData(_ userId: Int, accessToken: String) {
    apiRequest.getUserDetailData(userId, accessToken) { (userDetails) in

        self.userDataResult = userDetails?.result


    }
}
我从服务器上获取数据,就像

{
"Success": 1,
"Message": "Record Get Successfully.",
"Result": {
    "User_Id": 230,
    "First_Name": "Fgfdg",
    "Location": "Fond du Lac County",
    "State": " Wisconsin",
    "About": "SDFDdsgsGgsddsSSGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG",
    "TotalScore": 27,
    "sun": "Scorpio",
    "Rising": "Scorpio",
    "Moon": "Libra",
    "Ego": 100,
    "Communication": 100,
    "Birth_star": 99,
    "Sexual": 50,
    "Intellectual": 100,
    "Temperament": 99,
    "Emotional": 0,
    "Health_Genetic": 100,
    "Profile_Pic": "Images/ProfilePic/yugva0y.jpg",
    "NumberOfPhotos": 8,
    "imagelist": [
        {
            "User_Id": 230,
            "Image_path": "Images/UploadImage/uUk8lUP.jpg"
        },
        {
            "User_Id": 230,
            "Image_path": "Images/UploadImage/Sf5jBIU.jpg"
        },
        {
            "User_Id": 230,
            "Image_path": "Images/UploadImage/uw8hdNt.jpg"
        },
        {
            "User_Id": 230,
            "Image_path": "Images/UploadImage/vRdKGmM.jpg"
        },
        {
            "User_Id": 230,
            "Image_path": "Images/UploadImage/vQchUIO.jpg"
        },
        {
            "User_Id": 230,
            "Image_path": "Images/UploadImage/HYHQHiL.jpg"
        },
        {
            "User_Id": 230,
            "Image_path": "Images/UploadImage/owM3Al9.jpg"
        },
        {
            "User_Id": 230,
            "Image_path": "Images/UploadImage/T4ufyc6.jpg"
        }
    ],
    "verified": 0,
    "DateOfBirth": "1978-11-28",
    "TimeOfBirth": "07:52:00.0000000",
    "Age": 41
}
}
我得到的错误是:

Swift.DecodingError.valueNotFound(Swift.String,Swift.DecodingError.Context(编码路径:[CodingKeys(stringValue:“Result”,intValue:nil),编码键(stringValue:“sun”,intValue:nil)],debugDescription:“预期的字符串值,但改为发现空值。”,UnderingError:nil))


对于初学者,请打印
error
,而不是
error.localisedDescription
,以获取更详细的错误消息,您是否对此进行了调试,或者是否知道错误发生在何处?您从何处获得该错误?我收到了错误:-Swift.DecodingError.valueNotFound(Swift.String,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:“结果”,intValue:nil),CodingKeys(stringValue:“sun”,intValue:nil)],debugDescription:“预期的字符串值,但找到的却是null。”,underlineError:nil))请在你的问题中添加这些信息,在评论中阅读是非常困难的。还要指出你在代码中的哪一行出现错误。Hi@koen。实际上有很多用户,对于一些用户,“sun”、“Rising”和“Moon”值的值为零。所以在将这些值设置为可选值(让sun、Rising、Moon:String?),如果问题已解决。感谢您的时间。对于初学者,请打印
错误
,而不是
错误。localisedDescription
以获取更详细的错误消息,您是否对此进行了调试或以其他方式知道错误发生在何处?您从何处获得该错误?我收到错误:-Swift.DecodingError.valueNotFound(Swift.String,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:“Result”,intValue:nil),CodingKeys(stringValue:“sun”,intValue:nil)],debugDescription:“预期的字符串值,但找到的却是null。”,underyingerror:nil))请在你的问题中添加这些信息,在评论中阅读是非常困难的。还要指出你在代码中的哪一行出现错误。Hi@koen。实际上有很多用户,对于一些用户,“sun”、“Rising”和“Moon”值的值为零。所以在将这些值设置为可选值(让sun、Rising、Moon:String?),如果问题解决了,谢谢您的时间。