Your code:
NSMutableArray *sampleArray = [[NSMutableArray alloc]init];
sampleArray = [[[NSUserDefaults standardUserDefaults]objectForKey:@"commentsArray"] copy];
NSString *commentCount = [NSString stringWithFormat:@"%@",[[sampleArray valueForKey:@"totalComments"]objectAtIndex:Row]];
int cCount = [commentCount intValue];
[[[sampleArray valueForKey:@"totalComments"] objectAtIndex:Row]setString:[NSString stringWithFormat:@"%d",cCount+1]];
1) You do
NSMutableArray *sampleArray = [[NSMutableArray alloc]init];
sampleArray = [[[NSUserDefaults standardUserDefaults]objectForKey:@"commentsArray"] copy];
The [[NSMutableArray alloc]init]
constructs an empty mutable array. The subsequent sampleArray = [[[NSUserDefaults ...
overwrites the pointer set in the prior line with a pointer to a different array. This is wasted motion (and creating an object is expensive). Thankfully for you there is ARC, or it would also represent a leaked object. It should be simply
NSArray *sampleArray = [[NSUserDefaults ...
(Note it is not an NSMutableArray -- see below.)
2) You do
sampleArray = [[[NSUserDefaults standardUserDefaults]objectForKey:@"commentsArray"] copy];
The copy
on the end unnecessarily takes the object returned by objectForKey
and copies it. Unless here were some possibility of unwanted concurrent modification of the original array (which is impossible in this case), this copy
operation is completely unnecessary and again an expensive operation.
3) You do
NSString *commentCount = [NSString stringWithFormat:@"%@",[[sampleArray valueForKey:@"totalComments"]objectAtIndex:Row]];
Doing [NSString stringWithFormat:@"%@",someValue]
simply (and expensively) creates a copy of someValue
. Again, this is totally unnecessary -- it only serves to make your statement longer and more confusing.
4) You do
[[[sampleArray valueForKey:@"totalComments"] objectAtIndex:Row]setString:[NSString stringWithFormat:@"%d",cCount+1]];
Let's take that (unnecessarily) long statement apart:
NSArray* temp1 = [sampleArray valueForKey:@"totalComments"];
This is using "key-value" coding and will examine every NSDictionary in the array and ask it for any "totalComments" element.
NSString* temp2 = [temp1 objectAtIndex:Row];
This takes the result from valueForKey
and examines only the Row
element. Though it's hard to know for sure without knowing how "commentsArray" is constructed, very likely it would have worked just as well (and more clearly) to access the Row
element of sampleArray
and then request the totalCommnts
value from the resulting NSDictionary. This would have been faster and clearer.
What you now have in temp2 is an NSString. You now effectively execute
[temp2 setString:[NSString stringWithFormat:@"%d",cCount+1]];
Of course, your problem is now that an NSString is immutable, so an exception occurs. If you want to change it you must have an NSMutableString. But you cannot simply "install" one of those in the temp1
array, since all values returned by NSUserDefaults are immutable, including sampleArray
, temp1
, and temp2
. (And, no, they cannot be made mutable by simply changing the declarations.)
So you're kind of stuck. You need to totally redesign this function.