iosswiftxcodesnapchat

Swift Snapchat fetchSnapUserInfo() 'UserEntity' value not a resolved identity


I am working on integrating Snapchat login into my iOS app, in Xcode. This is my current code, implementing an SCSDKLoginButton in my view controller: (The error is highlighted with a comment)

import SCSDKLoginKit

var scLoginButton: SCSDKLoginButton!

let scLoginButton = SCSDKLoginButton()
scLoginButton.center = CGPoint(x: 200, y: 200)
view.addSubview(scLoginButton)



    @IBAction func loginButtonTapped(_ sender: Any) {
    SCSDKLoginClient.login(from: self, completion: { success, error in

        if let error = error {
            print(error.localizedDescription)
            return
        }

        if success {
            self.fetchSnapUserInfo()
        }
    })
}


private func fetchSnapUserInfo(){
    let graphQLQuery = "{me{displayName, bitmoji{avatar}}}"

    SCSDKLoginClient
        .fetchUserData(
            withQuery: graphQLQuery,
            variables: nil,
            success: { userInfo in

                if let userInfo = userInfo,
                    let data = try? JSONSerialization.data(withJSONObject: userInfo, options: .prettyPrinted),
                    let userEntity = try? JSONDecoder().decode(UserEntity.self, from: data) { // ERROR HERE

                    DispatchQueue.main.async {
                        self.goToLoginConfirm(userEntity)
                    }
                }
        }) { (error, isUserLoggedOut) in
            print(error?.localizedDescription ?? "")
    }
}

On the line where "// ERROR HERE" is added, I keep getting the error "Use of unresolved identifier 'UserEntity'". I directly copied and pasted that method from the Snapchat LoginKit tutorials, and I do not know how to fix it. Any help is very much appreciated :))

(btw my AppDelegate and info.plist files already have the required code implemented)


Solution

  • UserEntity is a model you should create it in your project, i think u forget to follow this step: UserEntity.swift:

    struct UserEntity { let displayName: String? let avatar: String?

    private enum CodingKeys: String, CodingKey {
        case data
    }
    
    private enum DataKeys: String, CodingKey {
        case me
    }
    
    private enum MeKeys: String, CodingKey {
        case displayName
        case bitmoji
    }
    
    private enum BitmojiKeys: String, CodingKey {
        case avatar
    }
    

    }

    extension UserEntity: Decodable {

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        let data = try values.nestedContainer(keyedBy: DataKeys.self, forKey: .data)
        let me = try data.nestedContainer(keyedBy: MeKeys.self, forKey: .me)
    
        displayName = try? me.decode(String.self, forKey: .displayName)
    
        let bitmoji = try me.nestedContainer(keyedBy: BitmojiKeys.self, forKey: .bitmoji)
        avatar = try? bitmoji.decode(String.self, forKey: .avatar)
    }
    

    }