objective c - ARC behavior on assigned local variable to an instance variable -
question 1
supposed have code:
myclass * __strong foo = [myclass new]; myclass * __strong bar = foo; // foo = nil; // arc?
in reference this answer, arc
automatically nil out foo
on line 3 since bar
acquired reference?
question 2
//ecgservice.m @property (strong) muttablearray *rridata; (muttablearray *)getrridata { return _rridata; } //algorithmtest.m // according apple docs, local variable marked __strong default muttablearray *rridata = [self.ecgservice getrridata]; for(nsnumber *rri in rridata) { // use rri! } // rridata = nil;
should nil out local variable rridata
after using it?
and, __strong
attribute must applied 1 instance of object?
answer 1.
in principle, arc doesn't “nil out” foo
until goes out of scope. when goes out of scope, arc releases reference. doesn't have set foo
nil, effect if arc set nil.
in practice, arc allowed release reference held foo
after last use of foo
variable, may long before goes out of scope. in example, if place use foo
in function in assignment bar
, arc allowed release foo
's reference after assignment. note there's no way know release because don't use foo
again! note bar
still reference object unless bar
not used later in function.
there special attributes can prevent arc performing release, called objc_precise_lifetime
, objc_returns_inner_pointer
applied through macros ns_valid_until_end_of_scope
, ns_returns_inner_pointer
. these advanced features won't have worry time soon, if want see examples of ns_returns_inner_pointer
, take @ nsstring.h
.
answer 2.
you don't need “nil out” rridata
. arc release reference when goes out of scope (or sooner—see answer 1).
you don't need __strong
usually, because it's default, , it's want usually. normal have multiple strong references object. use __weak
explicitly when need prevent retain cycle. there many explanations of retain cycles on web , on stack overflow, if need learn them, please visit favorite search engine.
Comments
Post a Comment