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".
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.
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.
Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays.
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 .
id anObject = [myArray objectAtIndex:42]; NSLog(@"%@", [anObject class]);
(To be totally correct, it should be:)
NSLog(@"%@", NSStringFromClass([anObject class]));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With