2

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?

Eimantas
  • 48,927
  • 17
  • 132
  • 168
sjm
  • 301
  • 2
  • 3
  • 10
  • Interesting read on the subject: [Objective C Primer: Part 3 – @property and @synthesize](http://www.optictheory.com/iphone-dev/2010/02/objective-c-primer-part-3-property-and-synthesize/) – dee-see Aug 25 '11 at 13:08
  • In Ruby, it is not `attr_accessible`, it is `attr_accessor`. `attr_aceessible` is used in Rails framework in a different context. `attr_accessor` defines getter and setter methods for u. – rubyprince Aug 25 '11 at 13:12
  • Oh, my fault good to know. I started with Rails programming and learned Ruby kind of like "top down". But where is the difference between attr_accessor and attr_accessor ? – sjm Aug 25 '11 at 21:44
  • see [this SO question](http://stackoverflow.com/questions/3136420/difference-between-attr-accessor-and-attr-accessible) and [this ruby forum question](http://www.ruby-forum.com/topic/95220) – rubyprince Aug 26 '11 at 06:37

3 Answers3

2

in basic terms YES

the @synthesize is the one saves you writing the methods

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

bigkm
  • 2,218
  • 1
  • 16
  • 12
2

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.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
1

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
rubyprince
  • 17,559
  • 11
  • 64
  • 104
Victor Carmouze
  • 947
  • 6
  • 10
  • I doubt ruby code is correct since `attr_accessor` is declared outside of class's scope. – Eimantas Aug 25 '11 at 13:15
  • Also `myVariable = 0` line is incorrect since `attr_accessor` is defined for instance variables and former variable is declared in class context. – Eimantas Aug 25 '11 at 13:20
  • Simple `@myVariable = 0` would suffice. No need for block .) `def initialize; @myVariable = 0; end;` – Eimantas Aug 25 '11 at 13:25
  • is there a need to initialize the `@myVariable` unless we want a default value...the equivalent ruby code will be one with no `initialize` function..isnt it? – rubyprince Aug 25 '11 at 14:10
  • Yeah, I add the initialize fonction compared to objective c, code, I will delete it to have basicly the same thing in objective c and ruby. – Victor Carmouze Aug 25 '11 at 14:30
  • @victor..u dont need `@myVariable` at all in the code(u dont have to declare variables in Ruby). I edited your answer with this and some more details in the ruby section(and also edited the variable name to follow lower snake case which is the convention)..I hope you dont mind :) – rubyprince Aug 26 '11 at 06:56