Ajay Sharma
Ajay Sharma

Reputation: 4517

Diffence between two syntax for NSMutableDictionary

Can anybody please tell me the exact difference between these two syntax , I found it by co-incidence

Structre is like this :

-> NSMutableArray (CategoryAry)
    ...> NSMutableDictionary (multiple number of dictionaries)


 NSLog(@"%@",[(NSMutableDictionary *)[CategoryAry objectAtIndex:indexPath.row+1] valueForKey:@"status"]);
 NSLog(@"%@",(NSMutableDictionary *)[[CategoryAry objectAtIndex:indexPath.row+1] valueForKey:@"status"]);

Although both print the same results .

Upvotes: 0

Views: 104

Answers (1)

Oliver
Oliver

Reputation: 23510

[(NSMutableDictionary *)[CategoryAry objectAtIndex:indexPath.row+1] valueForKey:@"status"]);

the "objectAtIndex" is casted to an NSMutableDictionary*, then a value "status" is searched inside it.

(NSMutableDictionary *)[[CategoryAry objectAtIndex:indexPath.row+1] valueForKey:@"status"]);

The "status" object is casted to an NSMutableDictionary*

The first call is just the right syntax.

For the second one, why does it works ?
valueForKey method is called on an id, and as it seems to be a dictionary, it works and returns a comprehensive result. Then on that result, the "description" method is called (@"%@"), and as that method exists on any object, casting the result to a NSMutableDictionary does not bug. The method is called on the subclass returned, and that return is displayed into the NSLog.

I guess that for the second call, you may have a compiler-warning ?

Upvotes: 1

Related Questions