在'ViewController'类型的对象上找不到属性ref?
问题描述:
我正在关注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"];
它说:“未使用的实体问题,未使用的变量‘裁判’”
为什么报警?如何解决这个问题?
答
如果您首先在本地声明了变量“ref
”,并且因此它不属于“class
”,那么自我将无法工作。
如果您在申请级别中声明了变量“ref
”,因此它可以并且应该被称为“self.ref
”。
由于您未使用本地变量并使用类成员“self.ref
”,所以您将收到警告。
答
在你的代码中,你正在本地创建一个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;
我试图这样做太: 接口的ViewController() 属性(弱,非原子)火力地堡* REF = [[火力地堡的alloc] initWithUrl:@“的https:// .firebaseio.com“]; 结束 它仍然不起作用。如何在类级别声明并使其工作:Firebase * ref = [[Firebase alloc] initWithUrl:@“https:// .firebaseio.com”]; ? –
user3270418
2014-11-04 09:05:58