尝试将FBSDKGraphRequest变成自定义对象

时间:2018-07-10 18:56:16

标签: json swift fbsdk custom-object fbsdkloginkit

我无法将FBSDKGraphRequest变成自定义模型对象。当然使用MVC。请帮帮我! :)

这里是我的用户控制器

UserController类{

// READ

static func fetchUser() {
    print("In fetchUser() function in UserController")

    let parameters = ["fields": "id, ig_id, name, username, profile_picture_url, media_count, followers_count, follows_count, biography, website"]
    FBSDKGraphRequest(graphPath: "its a secret ;)", parameters: parameters).start { (connection, result, error) -> Void in

        print("In FBSDKGraphRequest for User")

        if error != nil {
            print(error as Any)
            return
        }

        if let resultDictionary = result as? [String: Any] {
            if let id = resultDictionary["id"] as? String {
                print(id)
            }
            if let username = resultDictionary["username"] as? String {
                print(username)
            }
            if let biography = resultDictionary["biography"] as? String {
                print(biography)
            }
            if let website = resultDictionary["website"] as? String {
                print(website)
            }
        }
    }
}

}

这里是我的用户对象模型

导入基金会

struct用户:可编码{

let userID: String
let userIGID: String
let userUsername: String
let userName: String
let userProfilePictureURL: String
let userMediaCount: Int
let userFollowersCount: Int
let userFollowingCount: Int
let userBio: String?
let userWebsite: String?

}

扩展用户{

init?(dictionary: [String: Any]) {
    guard let userID = dictionary["id"] as? String,
        let userIGID = dictionary["ig_id"] as? String,
        let userUsername = dictionary["username"] as? String,
        let userName = dictionary["name"] as? String,
        let userProfilePictureURL = dictionary["profile_picture_url"] as? String,
        let userMediaCount = dictionary["media_count"] as? Int,
        let userFollowersCount = dictionary["followers_count"] as? Int,
        let userFollowingCount = dictionary["follows_count"] as? Int,
        let userBio = dictionary["biography"] as? String,
        let userWebsite = dictionary["website"] as? String else { return nil }
    self.init(userID: userID, userIGID: userIGID, userUsername: userUsername, userName: userName, userProfilePictureURL: userProfilePictureURL, userMediaCount: userMediaCount, userFollowersCount: userFollowersCount, userFollowingCount: userFollowingCount, userBio: userBio, userWebsite: userWebsite)
}

}

0 个答案:

没有答案