【问题标题】:Property ref not found on object of type 'ViewController'?在“ViewController”类型的对象上找不到属性引用?
【发布时间】:2014-11-04 03:03:13
【问题描述】:

我正在关注 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'”

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

【问题讨论】:

    标签: ios objective-c firebase


    【解决方案1】:

    如果您首先在本地声明了变量“ref”,因此它不是“class”的一部分,因此 self 将不起作用。

    如果您在类级别声明了变量“ref”,那么它可以并且应该被称为“self.ref”。

    您收到警告,因为您没有使用局部变量并使用类成员“self.ref”。

    【讨论】:

    • 我也试过这样做:interface ViewController () property (weak, nonatomic) Firebase *ref = [[Firebase alloc] initWithUrl:@"https://.firebaseio .com"];结束,它仍然不起作用。如何在类级别声明并使其像这样工作: Firebase *ref = [[Firebase alloc] initWithUrl:@"https://.firebaseio.com"]; ?
    【解决方案2】:

    在您的代码中,您正在本地创建一个 ref。而不是这个:

    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;

    【讨论】:

      猜你喜欢
      • 2014-04-27
      • 2012-03-04
      • 1970-01-01
      • 2023-03-22
      • 2019-06-25
      • 2021-06-30
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多