Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show what type of object is getting returned from an array or method?

Tags:

I have an array that can store different types of objects. When I retrieve that object I'd like to print that object type to the log. I can't seem to find a method that does this. I don't want to print the contents of the object.

I'd like the log to say something like "NSString" or "NSDictionary".

like image 780
Andrew Avatar asked Jan 20 '10 03:01

Andrew


People also ask

How do you find the type of an object in an array?

To get the object type from an array of objects first, we can use the typeof type operator followed by the array variable name or array literal followed by writing a square brackets symbol ( [] ) and inside the square brackets, we can write the type number to get the type of array's elements.

Why is my array typeof an object?

Apparently there seems to be something wrong because the array is recognized as an object and seems to be no real difference between object and array. This because in javascript all derived data type is always a type object. Included functions and array.

What does typeof array return?

Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays.

How do you check if an object is an array or not?

isArray() method is used to check if an object is an array. The Array. isArray() method returns true if an object is an array, otherwise returns false .


1 Answers

id anObject = [myArray objectAtIndex:42]; NSLog(@"%@", [anObject class]); 

(To be totally correct, it should be:)

NSLog(@"%@", NSStringFromClass([anObject class])); 
like image 103
Dave DeLong Avatar answered Nov 07 '22 05:11

Dave DeLong