objective c - class method where self if used within a block -
i've got class method uses dispatch_once create static object. inside dispatch_once block use [self class] , wondering if need use weak reference self avoid retain cycle?
+ (nsarray *)accountnames{ static nsarray *names = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ names = [[[self class] accounts] allkeys]; names = [names sortedarrayusingselector:@selector(caseinsensitivecompare:)]; }); return names; } if use weak reference self warning:
+ (nsarray *)accountnames{ static nsarray *names = nil; static dispatch_once_t predicate; __weak tuaccount *wself = self; dispatch_once(&predicate, ^{ names = [[[wself class] accounts] allkeys]; names = [names sortedarrayusingselector:@selector(caseinsensitivecompare:)]; }); return names; } incompatible pointer types initializing 'tuaccount *__weak' expression of type 'const class'
because warning don't think need use weak reference self in case wanted see guys thought.
there no reason worry retain cycle here, because it's meaningless retain or release class object -- retain , release have no effect.
your attempt @ making weak reference wrong, because taking class object self , trying cast instance of tuaccount. 2 different things.
also, can simplify:
names = [[[self class] accounts] allkeys]; since self class, [self class] == self, instead:
names = [[self accounts] allkeys];
Comments
Post a Comment