jdog
jdog

Reputation: 10769

Casting the return of objectAtIndex on NSArray

Trying to cast the return of objectAtIndex.

(MyClass *)[myArray objectAtIndex:1].name;

Can you cast inline like this in Objective-C?

Upvotes: 4

Views: 2990

Answers (2)

graver
graver

Reputation: 15213

Yes you can:

((MyClass *)[myArray objectAtIndex:1]).name

Upvotes: 11

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

You can avoid casting altogether by replacing the dot-syntax of accessing properties with the regular method invocation syntax:

[[myArray objectAtIndex:1] name]

Upvotes: 2

Related Questions