0

Is it possible to directly access an object variable with an array of objects like so..

NSArray *myObjectsInArray = [NSArray arrayWithObjects:(id) myClass,myOtherClass, nil];

NSLog(@"%d",[myObjectsInArray[0] returnSize]);   //works perfect

NSLog(@"%d",[myObjectsInArray[0].size]);  // error

I'm not sure if it's a syntax problem or if it cannot be accessed without a method. thanks for any help.

0

1 Answer 1

1

Accessing an array like so myArray[0] is equivelant to calling [myArray objectAtIndex:0]. The return value of objectAtIndex: is id. Any method call may be sent to an id reference. However, the compiler will not allow the use of the dot syntax shortcut to call methods on it.

If you really want to use the dot syntax, you need to cast it.

NSLog(@"%d",[((MyClass*)myObjectsInArray[0]).size]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.