English 中文(简体)
建立带有推特的NSDDlogy时, “ 未知选择器发送到实例” 。
原标题:"unrecognized selector sent to instance" when creating NSDictionary with tweets
  • 时间:2012-05-25 19:03:56
  •  标签:
  • ios
  • twitter

我在推特上搜寻此代码的推文:

- (void)fetchTweets
{

NSURL *url = [NSURL URLWithString:@"http://search.twitter.com/search.json"];
NSDictionary *params = [NSDictionary dictionaryWithObject:@"#winning" forKey:@"q"];

TWRequest *request = [[TWRequest alloc] initWithURL:url 
                                         parameters:params 
                                      requestMethod:TWRequestMethodGET];

[request performRequestWithHandler:
 ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {

     if (responseData) {
         NSError *jsonError;
         tweets = 
         [NSJSONSerialization JSONObjectWithData:responseData 
                                         options:kNilOptions
                                           error:&jsonError];


         if (tweets) {
            // NSLog(@"%@", tweets);
         } 

         else { 
             NSLog(@"%@", jsonError);
         }

     }

     [[self delegate] receivedTweets];

 }];

[self performSelector:@selector(fetchTweets) withObject:nil afterDelay:30];

Twitter变数是国安局的变数,

NSDictionary *tweet = [[TwitterHandler sharedInstance].tweets objectAtIndex:indexPath.row];

我当然会收到JSON的推文, 但当我试图把它们添加到字典上时, 最终可以把它们放进一张表格,

我不知道为什么我得到这个,如果有任何帮助,我将不胜感激。

最佳回答
[NSJSONSerialization JSONObjectWithData:responseData 
                                options:kNilOptions
                                  error:&jsonError];

这将返回推文的NSDaltical 对象。 从该 URL 的 Twitter 搜索结果可以通过查找密钥 结果 中的内容来过滤 。

亚卡:

NSDictionary *jsonTweet = [NSJSONSerialization JSONObjectWithData:responseData 
                                                          options:kNilOptions
                                                          error:&jsonError];

NSArray *tweet = [jsonTweet objectForKey:@"results"];

然后这些推文就会被重复, 因为他们是在一个NSANRRARY。

问题回答

在本守则中:

 if (responseData) {
         NSError *jsonError;
         tweets = 
         [NSJSONSerialization JSONObjectWithData:responseData 
                                         options:kNilOptions
                                           error:&jsonError];


         if (tweets) {
            // NSLog(@"%@", tweets);
         } 

         else { 
             NSLog(@"%@", jsonError);
         }

tweets 是否与您使用此代码获得的相同 :

[[TwitterHandler sharedInstance].tweets

如果是这样的话,在我看来,你把它分配到自动释放的物体上, 所以它最终会被释放, 并且你最终会用指示器指向一个被释放的物体。

最好保留该财产或在转让时使用保留财产。

类似

tweets = 
         [[NSJSONSerialization JSONObjectWithData:responseData 
                                         options:kNilOptions
                                           error:&jsonError] retain];

或者,假设 tweet 是保留书:

self.tweets = 
         [NSJSONSerialization JSONObjectWithData:responseData 
                                         options:kNilOptions
                                           error:&jsonError];

在不再需要时,不要忘记放出它(至少在班级 dealloc 中)

<强 > EDIT

Ok, 事情是这样的, 与记忆管理无关(错误的初步猜测! ) : 我试过你的 URL, 它返回 :

{"error":"You must enter a query."}

这是 JSON 字典。 然后JSON Parser 将它识别为字典, 并返回您的字典。 您可以将它存储在 NSARRRAY * 中, 但即时标的物体仍然是字典, 并且只对 NSD 词典方法做出响应。 正如您在打印时的评论所显示的, 它打印了字典 。

我曾经有过一次,这是一个程序 释放查看控制器, 检查你的查看控制器是否被释放。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签