无法获得用户的性别:在'NSDictionary <FBGraphUser>*'类型的对象上找不到属性'gender'

Mal*_*loc 0 ios facebook-ios-sdk fb-graph ios6

这是我的代码,用于获取代表用户Graph的整个字典:

 if (FBSession.activeSession.isOpen) {
        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection,
           NSDictionary<FBGraphUser> *user,
           NSError *error) {
             if (!error) {
                 NSLog(@"%@",user);//Displayed with the gender
                 NSLog(@"%@",user.name);//Name only displayed
                 NSLog(@"%@",user.gender);//Build error
             }
         }];
    }
Run Code Online (Sandbox Code Playgroud)

我想知道性别数据有什么问题,为什么它没有在字典中找到,实际上,我得到了一个构建错误:

property 'gender' not found on object of type 'NSDictionary<FBGraphUser> *'
Run Code Online (Sandbox Code Playgroud)

Gan*_*esh 9

您可以使用 [user objectForKey:@"gender"];


小智 7

Facebook没有发送性别来回应"requestForMe".为了获得性别发送请求,请执行以下操作:

NSDictionary* params = [NSDictionary dictionaryWithObject:@"id,gender,name, whatever you like" forKey:@"fields"];

[[FBRequest requestWithGraphPath:[NSString stringWithFormat:"%d",myUserId] parameters:params HTTPMethod:nil] startWithCompletionHandler:...
Run Code Online (Sandbox Code Playgroud)

我不确定,但我认为你也可以将图形路径设置为@"me"而不是用户的id.

  • Thanx对你的建议,我的也有效,如果我从字典中正常提取它,我终于可以阅读`gender`:`NSLog(@"%@",[user objectForKey:@"gender"]); (3认同)