Objective-C 中的 @property 和 @synthesize 与 Ruby 编程语言中的 attr_accessible 相同吗?
我可以在 Objective-C 中使用 @property 和 @synthesize ,这样我就不必编写 getter 和 setter 方法。在我看来,Ruby 中有 attr_accessible
做着同样的事情。我是对的还是有一点区别?
I can use @property
and @synthesize
in Objective-C so i do not have to write getter and setter methods. In Ruby there is the attr_accessible
doing the same in my opinion. Am i right or is there a little difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从基本术语来说,是的,
@synthesize 可以节省您编写方法的时间。
您也可以使用@dynamic,然后自己实现它们。
in basic terms YES
the @synthesize is the one saves you writing the methods
You can also use @dynamic and then implement them yourself.
你几乎是对的(tm)。可能唯一的偏差是使用
readonly
修饰符声明@property
会导致 ruby 中的attr_reader
。虽然 ruby 有attr_writer
,但 Objective-C 中没有writeonly
属性。You are almost right (tm). Probably only deviation is that declaring
@property
withreadonly
modifier would result inattr_reader
in ruby. And while ruby hasattr_writer
there is no such thing aswriteonly
property in Objective-C.基本上是的,这是同一件事:
在 ruby 中,您有 arr_accessible 方法,它为您创建 getter 和 setter。
在 Objective-C 中,@property 在 .m 文件中直接创建 getter 和 setter。
示例:
添加 @property 与创建相同:
并且
可以通过在 .m 文件中添加 @synthetize myVariable 来添加此方法。
在 ruby 中,您只需执行以下操作
attr_accessor :my_variable
相当于:Basicly yeah, it the same thing :
In ruby you've got the arr_accessible method, who create for you getter and setters.
in objective-c, @property creates directly getter and setter in your .m file.
example :
adding the @property is the same think as creating:
and
you add this methods by adding @synthetize myVariable in your .m file.
in ruby, you just have basicly to do this
attr_accessor :my_variable
is equivalent to this: