2

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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Alyek
  • 135
  • 1
  • 11
  • Shouldnt NSObjective be NSObject? You dont need [d release] or [y release] because you have not allocated an object pointer. Nor will you need readwrite or retain. I too am experienced in C# and have very little knowledge of Objective C – craig1231 Jan 18 '12 at 10:46
  • Yes it should be NSObject not NSObjective. – Alyek Jan 18 '12 at 11:05

3 Answers3

1

You did that almost correctly. The only issue is that your properties are plain integers so you cannot retain/release them. Corrected version will be (just remove all retain/release mentions as they are applicable to objective-c objects):

@interface X :NSObject
{
@private
   int _d ,_y;
}

@property (nonatomic, readwrite) int d;
@property (nonatomic, readwrite) int y;

@end

@implementaion X 
@synthesize d = _d;
@synthesize y = _y;

- (void) dealloc
{    
   [super dealloc];
}
@end

P.S. Correct name for base class should be NSObject

Vladimir
  • 170,431
  • 36
  • 387
  • 313
1

Your code is almost correct, with one big caveat: int and other primitive C-types should not be released. Only objects needs to be released, any object type declared with the @interface declaration that is.

You can get away with even less code if you target iOS 5 and later and using the Apple LLVM compiler and ARC.

@interface X : NSObject
@property(nonatomic, assign) int d;
@property(nonatomic, assign) int y;
@end

@implementation X
@synthesize d = d_, y = y_;
@end

You do not need to:

  • Declare ivars for properties in their owen block.
  • Implement a dealloc method, unless using advances resources such as streams or observers.
  • No need to release even objects.
PeyloW
  • 36,742
  • 12
  • 80
  • 99
0

The following code is like your class in C#

@interface X : NSObject
{
   @private
   int _d, _y;
}

@property (nonatomic, assign) int d;
@property (nonatomic, assign) int y;

@end

@implementation X

@synthesize d = _d;
@synthesize y = _y;

- (void) dealloc
{
   [super dealloc];
}

@end

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 NSString you can do the follow:

 //.h
@interface X : NSObject    
{
    @private
    NSString* _myString;
}

@property (nonatomic, copy) NSString* myString;

@end

//.m
@implementation X

@synthesize myString = _myString;

- (void) dealloc
{
   [_myString release];
   _myString = nil;

   [super dealloc];
}        

@end

Since NSString is 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.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • Ok, and what is seen as a primitive type in objective C? – Alyek Jan 18 '12 at 11:02
  • A primitive type could be an int, float, struct.. In general, primitive types are allocated on the stack (but also on the heap). Objects can only be allocated on the heap. – Lorenzo B Jan 18 '12 at 11:24
  • In the dealloc function wouldn't it be enough to only do [mystring release]?? – Alyek Jan 18 '12 at 12:49
  • you could do this `[_myString release]` and not `[myString release]`. There is no variable called `myString`. If you do `self.myString = nil` you access the setter for `_myString` variable passing it the `nil` value. Internally the setter release the current value for `_myString`. For further info read **Declared Properties** section in [ocProperties documentation](http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1) – Lorenzo B Jan 18 '12 at 13:18
  • For further info about properties I suggest you to read about [assign-retain-copy-pitfalls-in-obj-c](http://cocoawithlove.com/2010/06/assign-retain-copy-pitfalls-in-obj-c.html). – Lorenzo B Jan 18 '12 at 13:20
  • Ok thanks. One last question, how does one add reference to other proejcts code as done in Visual studio (like adding dll reference to solution)? – Alyek Jan 18 '12 at 13:46
  • do you have to add a specific framework or libraries? if yes, click in your project name (e.g. "simple project"). Now you are able to see PROJECT and TARGETS items. Click TARGETS. Select "Build Phases". In "Link Binaries with Libraries" you can add external framework (e.g. "CoreData.framework"). – Lorenzo B Jan 18 '12 at 13:54
  • see the edit. I've modified as the comment above. For effective memory management see [mmPractical](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html) – Lorenzo B Jan 18 '12 at 13:55