我正在使用this API获取用户的关注者列表,并按照文档中的说明以JSON格式返回关注者。这是返回对象的片段:
{
"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"
}
正如您所注意到的,用户对象有很多属性,我不想这样做 逐个解析它们或者使用库来为我做这件事。
TwitterKit有一个名为TWTRUser的类,这里是documentation。要初始化此类的对象,您可以使用一个采用如下JSON字典的构造函数:
let follower = TWTRUser(jsonDictionary: jsonDictionary)
这样我就可以获得解析后返回给我的JSON对象并初始化TWTRUser
对象。
问题是TWTRUser
没有返回JSON中列出的所有属性,它只有documentation中列出的这些属性:
userID属性
名称属性
screenName属性
isVerified 财产
isProtected属性
profileImageURL属性
profileImageMiniURL属性
profileImageLargeURL属性
formattedScreenName属性
profileURL属性
我尝试使用 valueForKey 方法,该方法接受一个键并返回如下值:
let createdAt = follower.value(forKey: "created_at")
我认为它会起作用,但事实并非如此。当我使用它时,应用程序崩溃并给我以下消息:
由于未捕获的异常'NSUnknownKeyException'而终止应用, 原因:'[valueForUndefinedKey:]:这个类 对于关键的contributors_enabled而言,它不符合关键值编码。'
如何使用TWTRUser
类来获取所有用户的属性?
答案 0 :(得分:2)
value(forKey:)
是一个继承 Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testService': Unsatisfied dependency expressed through field 'testDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.example.TestDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 25 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.example.TestDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 43 more
的方法,它用于Key-Value Coding,因此它不会返回JSON的结果。 NSObject
类只定义了10个属性,这就是你可以得到的所有属性。如果你想获得其他属性,你必须使用以下代码行自己解析JSON(使用标准库)
TWTRUser
答案 1 :(得分:1)
首先,就像已经提到的那样,不要使用value(for key)
,如果对象具有该值,它可能会通过公共财产公开它。
我的建议是将TWTRUser
子类化,将您想要的属性添加到新类(您可以将其称为TwitterUser
)并覆盖init(json)
,在那里您可以看到字典包含您想要的值并将它们添加到对象中。
之后,您将能够像TWTRUser
类中的任何其他属性一样访问这些属性。