2014-11-04 46 views
1

我正在關注FB登錄的firebase教程。在'ViewController'類型的對象上找不到屬性ref?

- (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) 

當我聲明它的文件的頂部「屬性REF不類型‘的ViewController’的對象中找到」部分。

@interface ViewController() 

@property (weak, nonatomic) Firebase* ref; 

@end 

錯誤消失,出現在這行代碼

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

它說:「未使用的實體問題,未使用的變量‘裁判’」

爲什麼報警?如何解決這個問題?

回答

1

如果您首先在本地聲明瞭變量「ref」,並且因此它不屬於「class」,那麼自我將無法工作。

如果您在申請級別中聲明瞭變量「ref」,因此它可以並且應該被稱爲「self.ref」。

由於您未使用本地變量並使用類成員「self.ref」,所以您將收到警告。

+0

我試圖這樣做太: 接口的ViewController() 屬性(弱,非原子)火力地堡* REF = [[火力地堡的alloc] initWithUrl:@「的https:// .firebaseio.com「]; 結束 它仍然不起作用。如何在類級別聲明並使其工作:Firebase * ref = [[Firebase alloc] initWithUrl:@「https:// .firebaseio.com」]; ? – user3270418 2014-11-04 09:05:58

1

在你的代碼中,你正在本地創建一個ref。取而代之的是:

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

地說:

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

這將設置它的視圖控制器上,而不是創建一個新的局部變量。如果你真的想有一個本地ref變量,你可以做到這一點與下一行:

Firebase *ref = self.ref;

另外,如果不知道你宣佈你的火力地堡性質爲弱,以避免保留週期,但你可能希望將其聲明爲強大,以便在ViewController仍在使用時ARC不會隨機決定收回它。

@property (strong, nonatomic) Firebase* ref;

相關問題