Difference between “.text” and “setText:”? [duplicate]

我怕爱的太早我们不能终老 提交于 2019-12-10 10:17:41

问题


Possible Duplicate:
Is there a difference between setting a property with the dot or the bracket syntax?

If I define a label in the .h file and I want to change the text of it in the .m file, is there really a difference between using label.text = @"..." and [label setText:@"..."]? They appear to do exactly the same thing, and if that's the case, then why the different ways to do it?


回答1:


There is no difference in the inner workings. The first way requires the type of label to be such that the compiler could verify the presence of the property; the second way works even if the compiler does not know the exact type (i.e. when the type is an id).

The second syntax is the original one; the first syntax was added for convenience and readability. There are different schools of thought on the subject of using these syntaxes: some people will tell you not to use one syntax or the other, and provide very valid reasons for it. In the end, though, this is your choice: you should pick one syntax, and stay with. As long as you are consistent and your readers know of your preference, they should have no trouble reading and maintaining your code either way.



来源:https://stackoverflow.com/questions/11642735/difference-between-text-and-settext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!