1

我正在使用 Facebook api 来获取用户的朋友姓名、UID 和生日。我遇到的问题是,一旦我得到结果,我如何获得这些值。意思是,我想获取每个返回值的用户名、生日?

这段代码在这里一直为我崩溃

//set query
NSString *query =  @"SELECT uid, name, birthday_date FROM user WHERE uid IN " @"(SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 25)";

// Set up the query parameter
NSDictionary *queryParam = [NSDictionary dictionaryWithObjectsAndKeys:query, @"q", nil];

[FBRequestConnection startWithGraphPath:@"/fql"
                             parameters:queryParam
                             HTTPMethod:@"GET"
                      completionHandler:^(FBRequestConnection *connection,
                                          id result,
                                          NSError *error) {
    if (error) {
            NSLog(@"Error: %@", [error localizedDescription]);
        } 
    else {
        NSLog(@"Result: %@", result);
        NSArray *friendInfo = (NSArray *) [result objectForKey:@"data"];
        NSArray *arryData3 = [[NSArray alloc] initWithArray:friendInfo];
        NSLog(@"[friendInfo count]: %d ... [arryData3 count]: %d ...",[friendInfo count],[arryData3 count]);
        for (int i=0; i<[arryData3 count]; i++) {
            NSArray *bdayArray = [[NSArray alloc] initWithArray:[arryData3 objectAtIndex:i]];
            //CODE CRASH HERE!!!!
            NSString *bdayStr = [bdayArray objectAtIndex:0];
            NSString *nameStr = [bdayArray objectAtIndex:1];
        }
    }
}];

这就是我的崩溃日志的样子

data(
    {
        "birthday_date" = "01/06";
        name = "John Doe";
        uid = 555555;
    },
    {
        "birthday_date" = "09/01/1984";
        name = "Mrs. Smith";
        uid = 4444444;
    },
)
[friendInfo count]: 2 ... [arryData3 count]: 2 ...
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSArray initWithArray:range:copyItems:]: array argument is not an NSArray'
*** First throw call stack:
(0x168f052 0x1cabd0a 0x167fe0b 0x15e8170 0x16ca2 0x27c03 0x26c7e 0x2444e 0x2927e 0x296e7 0xfc1a59 0xfbfe94 0xfc0eb7 0xfbfe4f 0xfbffd5 0xf04f6a 0x3964bbd 0x3a315ea 0x395b298 0x3a3116b 0x395b137 0x166397f 0x15c6b73 0x15c6454 0x15c5db4 0x15c5ccb 0x3bd1879 0x3bd193e 0x5cca9b 0x2038 0x1f95)
4

2 回答 2

1

对于任何可能对未来感兴趣的人。这就是您在 Facebook 的结果中获得个人价值观的方式。

NSLog(@"Result: %@", result);
NSArray *friendInfo = (NSArray *) [result objectForKey:@"data"];
NSLog(@"[friendInfo count]: %d ....",[friendInfo count]);
for (int i=0; i<[friendInfo count]; i++)
{
    NSMutableString *userNameStr = [NSMutableString stringWithFormat:@"%@", [[friendInfo objectAtIndex:i]  objectForKey:@"name"]];
    NSMutableString *userDateStr = [NSMutableString stringWithFormat:@"%@", [[friendInfo objectAtIndex:i]  objectForKey:@"birthday_date"]];
    NSLog(@"userNameStr: %@ ... userDateStr: %@ ...", userNameStr,userDateStr);
   //From this point onwards you can put these individual strings into an array 
   //and if you had defined the array globally you can get to these results from 
   //any function in your code
}
于 2012-12-29T20:25:10.830 回答
0

返回的是字典,而不是数组。请阅读 NSDictionary 并像使用结果一样使用它 - objectForKey 函数。在这种情况下,键是生日日期,然后是 for 函数中字符串的名称。

利维乌

于 2012-12-29T20:07:18.703 回答