开发者

Objective-C: message syntax vs. dot syntax; what's the difference?

开发者 https://www.devze.com 2023-01-26 19:43 出处:网络
If I\'m using @synthesize foo;, what\'s the difference between the following: // message syntax [myObj setFoo:5];

If I'm using @synthesize foo;, what's the difference between the following:

// message syntax
[myObj setFoo:5];
[myObj foo];

and

// dot syntax
myObj.foo = 5;
myObj.foo;

I like the consistency of the dot syntax but I don't know if it's doing something I should be 开发者_运维问答I concerned about.

Any additional information would be a great help.


There is no functional difference between using dot syntax and using message syntax.

I find that using message syntax is more consistent with the language as a whole and that dot syntax was just implemented as a convenience to programmers who were coming over from languages that used it (Java comes to mind).

All I ask is: Whichever one you choose, be consistent with it. Do not mix and match single property setters! (Multiple-argument setters are obviously exempt).


They are the same.

The dot syntax is used to signify the access of @property's which you can also acces via:

[myObject setValue: [NSNumber numberWithIntValue:5] forKey:@"foo"];
[myObject valueForKey:@"foo"];

But in essence the calls are the same.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号