-1
@property (nonatomic, assign) Class aClass;

or

@property (nonatomic, retain) Class aClass;

Note: "Class" is that "Class" defined in objc.h

and does it need a release in the dealloc method?

Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
qiz
  • 869
  • 1
  • 9
  • 14
  • You can simply use ARC, you are actually encouraged to use ARC. and it should be nonatomic,strong. and as a good practice u should nil it in the viewdidunload – Pochi Aug 13 '12 at 08:12
  • @LuisOscar ARC is not better than MRC, it is practically the same. And `nil` something in `viewDidUnload` is recommended only if it's a IBOutlet which is obviously not the case here. – Sulthan Aug 13 '12 at 08:53
  • Thanks, And sorry about my poor English. I'm good at objc's memory manage. but my question's point is how to use **Class** as a property of a class. – qiz Aug 13 '12 at 09:00
  • @Sulthan according to apple Automatic Reference Counting (ARC) for Objective-C makes memory management the job of the compiler. By enabling ARC with the Apple LLVM compiler, you will never need to type retain or release again, dramatically simplifying the development process, while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps run as fast as ever, with predictable, smooth performance. – Pochi Aug 13 '12 at 09:02
  • @Sulthan tell me if THAT doesnt make it better, specially for someone new to ios development (which can be seen by the question). and about the second thing no you are wrong there aswell. IF its an IBOutlet it is often better to make it a weak reference, since the view already has a strong pointer to it. You nil things in viewdidunload for low memory conditions in where you should nil all things that can be easily reconstructed (usually things in the viewDidLoad). It is NOT a dealloc function per se. – Pochi Aug 13 '12 at 09:05
  • @LuisOscar There are small performance gains and small loses. The difference is not very big. You can still create memory leaks. You have to understand MRC perfectly even when using ARC. Even with MRC, if you have good guidelines, you are not creating memory leaks, you are just adding `autorelease` after every `alloc`. ARC is comfortable for beginner programmers because they can program without understanding. For experienced programmer it doesn't really make big difference. – Sulthan Aug 13 '12 at 09:06
  • @Sulthan i understand your point, its valid, but i stand firm on the idea that he should focus on ARC instead. – Pochi Aug 13 '12 at 09:10
  • @LuisOscar I know exactly how to use `viewDidUnload` (it will get deprecated in iOS 6 anyway). Note that your comment is only modifying the question into weak/strong from assign/retain. – Sulthan Aug 13 '12 at 09:10
  • @Sulthan which is why it is just a comment, im not posting it as an answer. and about the viewdidunload thing is just cuz it is not actually meant for what you said in your previous comment "viewDidUnload is recommended only if it's a IBOutlet". But im not here to argue anyway. cheers – Pochi Aug 13 '12 at 09:17

2 Answers2

1

Since it's a pointer to a class object, and class objects live for the lifetime of the app, memory management actions like retain and release have no effect on it. So it shouldn't matter whether you use assign or release. It would be simpler to use assign.

newacct
  • 119,665
  • 29
  • 163
  • 224
0

If you use retain, the compiler will generate an object for you and retain it in the class. For such cases you should release it in the dealloc. Do not forget to add the synthesize though, otherwise you won't get any getter and setters.

Read more here: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html
And here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW9

Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
  • 1
    Why would you want to retain a Class object? In most cases (when you don't create dynamic class pair during the runtime) I thought they stay around for the lifetime of the app to say the least. Is it wrong assumption? – Sash Zats Aug 13 '12 at 09:18
  • Because you want to have control and ownership over the object. If you put it to autorelease, it might be released whenever... when retaining the object you can release it when you are done with it, thus better memory management and less change of crashes do to e.g. EXC_BAD_ACCESS. Better explanation also here: http://stackoverflow.com/questions/1380338/objective-c-101-retain-vs-assign-nsstring – Paul Peelen Aug 13 '12 at 09:27
  • precisely my point: why would you want to retain something like UIKeyboardWillShowNotification constant that is definitely outlives your application life cycle, most of the classes fall into the same category from memory management point of view: you don't really need to retain those as they won't get released before your app would become terminated. Although in cases like CALayer apple advices to return Class object from your custom implementation – Sash Zats Aug 13 '12 at 09:32
  • I thik you might have miss-read my answer, I am not stating you should or should not retain. I am answering the users question on the difference. – Paul Peelen Aug 13 '12 at 09:56
  • My apologies, my point was that in the context of the question retaining classes is an invalid approach to begin with. Sorry for confusion:) – Sash Zats Aug 13 '12 at 10:51