在'ViewController'类型的对象上找不到属性引用?

时间:2014-11-04 03:03:14

标签: ios objective-c firebase

我正在关注FB登录的firebase教程。

- (void)viewDidLoad {
   [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     // Align the button in the center horizontally


   Firebase *ref = [[Firebase alloc] initWithUrl:@"https://MYURL.com"];
    // Open a session showing the user the login UI
      [FBSession openActiveSessionWithReadPermissions:@[@"public_profile"] allowLoginUI:YES
      completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {

         if (error) {
            NSLog(@"Facebook login failed. Error: %@", error);
            } else if (state == FBSessionStateOpen) {
                  NSString *accessToken = session.accessTokenData.accessToken;
                  [self.ref authWithOAuthProvider:@"facebook" token:accessToken
                  withCompletionBlock:^(NSError *error, FAuthData *authData) {

                   if (error) {
                        NSLog(@"Login failed. %@", error);
                                                      } else {
                        NSLog(@"Logged in! %@", authData);
                                                      }
                              }];
                         }
                    }];

行中出现错误:

[self.ref authWithOAuthProvider:@"facebook" token:accessToken
              withCompletionBlock:^(NSError *error, FAuthData *authData) 

"找不到类型对象' ViewController'"

当我在部分的文件顶部声明它时。

@interface ViewController ()

@property (weak, nonatomic) Firebase* ref;

@end

错误消失,警告出现在此行代码

Firebase *ref = [[Firebase alloc] initWithUrl:@"https://sizzling-inferno-8395.firebaseio.com"];

它说"未使用的实体问题,未使用的变量' ref'"

为什么呢?如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

如果您首先声明变量" ref"在本地,因此它不属于" class"因此,自我将无法运作。

如果是两个你已声明变量" ref"在班级,因此它可以而且应该被称为" self.ref"。

您正在收到警告,因为您没有使用本地变量并使用班级成员" self.ref"。

答案 1 :(得分:1)

在您的代码中,您在本地创建了一个引用。而不是:

Firebase *ref = [[Firebase alloc] initWithUrl:@"https://MYURL.com"];

把:

self.ref = [[Firebase alloc] initWithUrl:@"https://MYURL.com"];

这将在视图控制器上设置它,而不是创建新的局部变量。如果您确实想要一个本地ref变量,可以在下一行使用:

Firebase *ref = self.ref;

另外,不确定您是否将Firebase属性声明为弱以避免保留周期,但您可能希望将其声明为强,以便ARC在ViewController仍在使用时不会随机决定回收它

@property (strong, nonatomic) Firebase* ref;