1

我是 iOS 初学者,在编码方面遇到了一些麻烦。通过发送 ASIHTTPRequest:

NSURL *url = [NSURL URLWithString:@"http://search.twitter.com/search.json?geocode=37.781157,-122.398720,25km"];

我收到这样的数据:

            {
        "created_at" = "Fri, 25 Jan 2013 17:48:29 +0000";
        "from_user" = "ZacHWallS_SBI";
        "from_user_id" = 252712138;
        "from_user_id_str" = 252712138;
        "from_user_name" = "ZACH WALLS";
        geo = "<null>";
        id = 294864332490162176;
        "id_str" = 294864332490162176;
        "in_reply_to_status_id" = 294861687004225537;
        "in_reply_to_status_id_str" = 294861687004225537;
        "iso_language_code" = en;
        location = "BAY AREA CALIFORNIA";
        metadata =             {
            "result_type" = recent;
        };
        "profile_image_url" = "http://a0.twimg.com/profile_images/3029389178/e3567459d147bc5d0a10f3c797b477aa_normal.png";
        "profile_image_url_https" = "https://si0.twimg.com/profile_images/3029389178/e3567459d147bc5d0a10f3c797b477aa_normal.png";
        source = "&lt;a href=&quot;http://twitter.com/#!/download/ipad&quot;&gt;Twitter for iPad&lt;/a&gt;";
        text = "@LilTioSBi yee ima slide through prolly";
        "to_user" = LilTioSBi;
        "to_user_id" = 35629694;
        "to_user_id_str" = 35629694;
        "to_user_name" = "Nevin Tio";
    },

接下来我这样做:

-(void) requestFinished: (ASIHTTPRequest *) request {
NSString *theJSON = [request responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSMutableArray *jsonDictionary = [parser objectWithString:theJSON];
//NSLog(@"\n\n%@", jsonDictionary);  
NSMutableArray *userName = [userinfo objectForKey:@"from_user"];
NSLog(@"\n\n\n%@",userName);

我需要捕获“from_user”。然后运行,编译器说: [__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x755bbc0... 如何正确执行?

4

4 回答 4

2

因此,我将首先澄清一些事情,即实际上是运行时给您“[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x755bbc0”错误。

让我们分解一下:

__NSArrayM听起来很可疑NSArrayNSArray从技术上讲,它被称为类集群,这意味着您NSArray在实践中并不总是能看到确切的内容,只是一些类似的东西)。

-objectForKey:是一种方法NSDictionary

因此,我们在一个我们认为是字典的数组上调用一个方法(调用一个方法 = 发送一条消息,或 ObjC 用语中的“选择器”)。

似乎您省略了一些代码,但如果您只是从以下内容传递值:

[jsonDictionary objectForKey:@"results"]

你最终会得到一系列推文。然后,您需要从该数组中获取一个项目以获取其from_user键。

NSArray *tweets = [jsonDictionary objectForKey:@"results"];
NSDictionary *tweet = [tweets objectAtIndex:0];
NSString *username = [tweet objectForKey:@"from_user"];
于 2013-01-25T18:34:30.080 回答
0

很可能,您有一系列推文。所以:

- (void) requestFinished: (ASIHTTPRequest *) request {
    NSString *theJSON = [request responseString];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSArray *parsedJson = [parser objectWithString:theJSON];

    NSMutableArray *allFromUsers = [NSMutableArray new];
    for (NSDictionary *tweet in parsedJson) {
        NSString *fromUser = [tweet objectForKey:@"from_user"];
        [allFromUsers addObject:fromUser];
    }
    NSLog(@"allFromUsers = %@", allFromUsers);

    // ... etc ...
}

parsedJson或者只是从数组中获取您想要的单个推文。

于 2013-01-25T18:31:33.697 回答
0

您需要使用:

NSString *userName = [jsonDictionary objectForKey:@"from_user"];
于 2013-01-25T18:25:51.253 回答
-1

它应该是:

NSMutableArray *userName = [NSMutableArray arrayWithObject:[jsonDictionary objectForKey:@"from_user"]];

或者也许你只是想把名字放在一个 NSString 中?

NSString *userName = [jsonDictionary objectForKey:@"from_user"];
于 2013-01-25T18:29:12.733 回答