Im a C# developer whom is new to Objective C and this migration has not been easy due to the difference between the two languages.
What im try do is to create a class that has properties similar to as you do in C#.
If for ex have a C# class which looks like:
public class X
{
private int _d, _y;
public int D { get {return _d; } set{ _d= value;}}
public int Y { get{ return _y;} set {_y = value;}}
}
Who does one write this in objective C?
I have tried but been unable to do this.
My objective C class for the moment looks like this:
@interface X : NSObject
{
@private
int _d ,_y;
}
@property (nonatomic, readwrite, retain) int d;
@property (nonatomic, readwrite, retain) int y;
@end
@implementation X
@synthesize d = _d;
@synthesize y = _y;
-(void)dealloc
{
[d release];
[y release];
[super dealloc];
}
@end
Thank you in advance.
The following code is like your class in C#
Some notes
When you use primitive type like int, your cannot retain it. You can only use assign policy (the default one). Retain policy is used only with objects. Also readwrite policy is set by default. If you want to have only a getter for that variable use readonly (instead of readwrite).
For further information see Property Declaration Attributes section in properties reference.
Edit
For
NSStringyou can do the follow:Since
NSStringis an object (note the *) you have to release that object in dealloc method.You should use copy policy for
NSString. This is explained in the following stackoverflow topic.