是一个返回__weak var的好方法吗?

时间:2015-06-30 09:34:16

标签: ios objective-c

我有以下代码:

@interface SomeClass {
...
__weak SomeType* ivar;
}
- (SomeType*) getVar;

@implementation SomeClass
...
// set ivar some value
- (void) someFunc {
...
ivar = someVar;
}

- (SomeType*) getVar{
 return ivar;
}
@end

我想要返回ivar,如果someVar有时为零,则ival为nil,这就是使用weak的原因。这个代码是对的吗? 或者以下代码是对的吗?

 @interface SomeClass
 @property(weak,readonly) SomeType* ivar;
 @end   

 @implementation SomeClass
    ...
    // set ivar some value
    - (void) someFunc {
    ...
    _ivar = someVar;
    }

 @end

1 个答案:

答案 0 :(得分:1)

好吧,如果您的"users" : { "simplelogin:24" : { "age" : "24", "email" : "a@a.com", "gender" : "male", "location" : "USA", "name" : "IY" }, "simplelogin:25" : { "age" : "21", "email" : "girl@girls.com", "gender" : "female", "location" : "USA", "name" : "iris" } } 在方法完成后没有其他人对var app = angular.module('myApp.home', ['firebase.auth', 'firebase', 'firebase.utils', 'ngRoute']); app.controller('HomeCtrl', ['usersList','$scope', 'fbutil', 'user', '$firebaseObject', 'FBURL', function (usersList, $scope, fbutil, user, $firebaseObject, FBURL) { $scope.syncedValue = $firebaseObject(fbutil.ref('syncedValue')); $scope.user = user; $scope.FBURL = FBURL; $scope.users = usersList; }]); app.factory('usersList', ['fbutil', '$firebaseArray', function(fbutil, $firebaseArray) { var ref = fbutil.ref('users').limitToLast(50); return $firebaseArray(ref); }]); 有强烈的引用,那么您的<ul><li ng-repeat='user in users'>{{user.name}}</li></ul> 将不再拥有您已分配给它的值。你会失去它。

更好地使用财产。但它应该是someFunc,至少在您的实现中,以便您以某种方式保留分配给someVar的值