开发者

Is @property and @synthesize in Objective-C the same as attr_accessible in the Ruby Programming Language?

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 开发者_StackOverflow中文版a little difference?


in basic terms YES

the @synthesize is the one saves you writing the methods

You can also use @dynamic and then implement them yourself.


You are almost right (tm). Probably only deviation is that declaring @property with readonly modifier would result in attr_reader in ruby. And while ruby has attr_writer there is no such thing as writeonly property in Objective-C.


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 :

@interface MaClasse : NSObject {
  int myVariable;
}

@property(nonatomic, assign) int myVariable;
@end

adding the @property is the same think as creating:

-(int)myVariable {
  return myVariable;
}

and

-(void)setMyVariable:(int)newValue {
  myVariable = newValue;
}

you add this methods by adding @synthetize myVariable in your .m file.

in ruby, you just have basicly to do this

class MyClass
  attr_accessor :my_variable 
end

attr_accessor :my_variable is equivalent to this:

def my_variable
  @my_variable
end

def my_variable=(my_variable)
  @my_variable = my_variable
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜