Reputation: 12184
I have an NSMutableArray
which returns me some object.
The object which I added had properties name,age.
now when I use these properties on the Object returned (obj.name
or obj.age
),
Compiler says, no such member, use (->
) instead of (.
)
I understand that NSObject wont have these members and hence it wont understand the property.
But If i use setters, and getters as method ([obj name]
or [obj age]
) syntax instead of this properties, I dont get any errors.
But using property means calling a setter or getter only ?
ad Objective C is suppose to be dynamic language, right ?
Upvotes: 0
Views: 178
Reputation: 8633
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!
Upvotes: 3
Reputation: 1301
That's right - dot syntax is not supported in such case.
You need to cast a pointer to the actual class:
((MyObject*)[array objectAtIndex: 0]).name = @"Bill";
Upvotes: 1