Adding Member Variables in Objective-C

I have to do first

@property (retain) aMember;

Then in the implementation file I had to

@synthesize aMember;

Then in dealloc I need to do

self.aMember= nil; (or [aMember release])

It writes 3 times what is essentially the same

Is there any way to speed this up?

I mean, I can drag the control out of IB and xcode, automatically generating these codes, why can't I do this for more normal codes?

+3
source share
4 answers

As someone from C # and managed languages ​​for my day job, I totally agree with you on this three-step process. Actually its almost insanely easy to create properties in C # in MS Visual Studio, but I'm distracted.

, , , .

  • @property objective-c (, , , ..) , , . , ( - ); , , , , . @property , (). , , , .

  • @synthesize , , . . - , objective-c, . @synthesize , get set.

  • dealloc - , . , , , .

, 3 , , , .

+3

, ( ). Apple , Clang , :

@interface Blah : NSObject
@property (retain) Blorg *blorg;
@end

@implementation Blah

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

@end

, , @dynamic blorg . - , .

, , , Apple . .

:

  • , -dealloc: , -finalize (, ).

  • -dealloc ++, : @property prop_wrapper<Blorg> blorg; . , , ~prop_wrapper(). , , , KVO KVC.

  • , copy retain. -dealloc - [self releaseProperties]. , , , , .

+2

- objective-c, .

:

  • -. ( , , "" , -)

  • getters .

  • , .

-, , , :

@interface SomeClassObject : NSObject {
   int someMemberVariable;
}
@end
+1

3 ,

, .

@property (retain) aMember;

, , -aMember -setAMember: . ( ), (.. , , ), / .

@synthesize aMember;

, setter getter . , getter.

[aMember release]; // in dealloc

Whether runtime should be indicated that when an object is freed, it no longer needs to refer to this instance variable. This is necessary because when you use reference counting rather than garbage collection, the runtime does not automatically clean up unwanted objects.

Each of these lines does a different thing. So you do not do the same thing three times.

+1
source

All Articles