Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether an object is an NSArray or NSDictionary

Tags:

objective-c

As per subject, how can I check whether an object is an NSArray or NSDictionary?

like image 293
Charles Yeung Avatar asked Oct 25 '11 07:10

Charles Yeung


People also ask

What is the difference between NSDictionary and NSMutableDictionary?

The key difference: NSMutableDictionary can be modified in place, NSDictionary cannot. This is true for all the other NSMutable* classes in Cocoa. NSMutableDictionary is a subclass of NSDictionary, so everything you can do with NSDictionary you can do with both.

What is NSDictionary?

An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.

Is NSArray ordered?

The workhorse of collection types is the array. In Objective-C, arrays take the form of the NSArray class. An NSArray represents an ordered collection of objects. This distinction of being an ordered collection is what makes NSArray the go-to class that it is.

How do you add value in NSMutableDictionary?

[myDictionary setObject:nextValue forKey:myWord]; You can simply say: myDictionary[myWord] = nextValue; Similarly, to get a value, you can use myDictionary[key] to get the value (or nil).


2 Answers

if([obj isKindOfClass:[NSArray class]]){     //Is array }else if([obj isKindOfClass:[NSDictionary class]]){     //is dictionary }else{     //is something else } 
like image 118
zakishaheen Avatar answered Sep 22 '22 16:09

zakishaheen


Try

[myObject isKindOfClass:[NSArray class]] 

and

[myObject isKindOfClass:[NSDictionary class]] 

Both of these should return BOOL values. This is basic use of the NSObject method:

-(BOOL)isKindOfClass:(Class)aClass 

For a bit more information, see this answer here: In Objective-C, how do I test the object type?

like image 36
Chris Grant Avatar answered Sep 21 '22 16:09

Chris Grant