cocoa touch - iPhone - block enumeration -
i have array several objects , want add 90.0 each value. i want using enumerate. create this:
[myarray enumerateobjectswithoptions:nsenumerationconcurrent usingblock:^(id obj, nsuinteger idx, bool *stop) { double avalue = [obj doublevalue]; avalue += 90.0; obj = [nsnumber numberwithdouble:avalue]; }];
my question regarding last line. object value, add 90 , have store back. little bit weird, because working on object level, not sure if third line has correct syntax store updated value on object. xcode complains "obj" never used, because thinks assigning number obj intention use it, storing value back.
is correct?
a better way store amended values new array:
nsmutablearray *newarray = [[nsmutablearray alloc] initwithcapacity:[myarray count]]; __block newarray; [myarray enumerateobjectswithoptions:nsenumerationconcurrent usingblock:^(id obj, nsuinteger idx, bool *stop) { double avalue = [obj doublevalue]; avalue += 90.0; [newarray insertobject:[nsnumber numberwithdouble:avalue] atindex:idx]; }];
note i've typed straight in, please excuse syntax errors.
Comments
Post a Comment