Do you cast the returned object to your object type (MyObject
)?
You should do something like:
((MyObject*)[mutableArray objectAtIndex:0]).age = 20;
The reason you're not getting any errors when using [[mutableArray objectAtIndex:0] name]
syntax is that you're calling a method on the returned object (which is of type id
), and id
s tend to not choke in the compile-time if you call a (yet) non-existant method on them. At the run-time, [mutableArray objectAtIndex:0]
might resolve to type MyObject
an in that case, the message [obj name]
has a proper implementation (IMP
). If it doesn't resolve to MyObject
, your app will crash.
And note that the reason you're not even getting a compile-time warning is that Xcode knows that there is at least 1 class in your codebase that implements the method name
, and it trusts you with calling this method only on instances of that class. if you do something like ((MyObject*)[mutableArray objectAtIndex:0]).ageeeeee = 20;
, it'll give you a warning as there's a very good chance that it'll crash (no class in your app implements the method ageeeeee
statically).
The type id
does not have a property name
, and that's why you can't use dot syntax.
Actually, this incident shows perfectly why ObjC is called a dynamic language!