Getting user's complete information using TwitterKit

229 views Asked by At

I'm using this API to get a user's followers list, and returns the followers in JSON format as described in the documentation. Here is a snippet of a returned object:

{
  "users": [
    {
      "id": 2960784075,
      "id_str": "2960784075",
      "name": "Jesus Rafael Abreu M",
      "screen_name": "chuomaraver",
      "location": "",
      "profile_location": null,
      "url": null,
      "description": "",
      "protected": false,
      "followers_count": 1,
      "friends_count": 101,
      "listed_count": 0,
      "created_at": "Sun Jan 04 19:58:06 +0000 2015",
      .....
      .....
      "default_profile": true,
      "default_profile_image": false,
      "following": false,
      "follow_request_sent": false,
      "notifications": false,
      "muting": false
    },
    .....
    .....
],
  "next_cursor": 1489467234237774933,
  "next_cursor_str": "1489467234237774933",
  "previous_cursor": 0,
  "previous_cursor_str": "0"
}

As you notice, the user object has a lot of properties, and I don't want to parse them one by one or use a library to do that for me.

The TwitterKit has a class named TWTRUser, and here is it's documentation. To initialize an object of this class, you can just use a constructor that takes a JSON dictionary like this:

let follower = TWTRUser(jsonDictionary: jsonDictionary)

This way I can get the JSON Object the is returned to me parsed and initialize a TWTRUser object.

The problem is that TWTRUser doesn't have all the properties listed in the JSON returned, it only has these properties as listed in the documentation:

userID property
name property
screenName property
isVerified property
isProtected property
profileImageURL property
profileImageMiniURL property
profileImageLargeURL property
formattedScreenName property
profileURL property

I tried to use valueForKey method that takes a key and returns it value like this:

let createdAt = follower.value(forKey: "created_at")

I thought it would work, but it didn't. When I use it the application crashes and gives me the following message:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key contributors_enabled.'

What could I do to get all the user's properties using TWTRUser class?

2

There are 2 answers

0
Papershine On

value(forKey:) is a method inherited with NSObject and it is used for Key-Value Coding so it doesn't return results of the JSON. The TWTRUser class only defines 10 properties and that's all you can get with it. If you want to get other properties, you would have to parse the JSON yourself with the following line of code (using the standard library)

let user = try JSONSerialization.jsonObject(with: jsonDictionary) as? [String: Any]
0
EmilioPelaez On

First of all, like it's already been mentioned, don't use value(for key), if the object had that value it probably would expose it through a public property.

My suggestion would be to subclass TWTRUser, add the properties you want to your new class (you can call it something like TwitterUser) and override init(json), there you can see if the dictionary contains the values you want and add them to the object.

After that you'll be able to access those properties like you would with any other properties in the TWTRUser class.