Objective-C - Should we use strong, weak or assign

2019-03-01 12:06发布

问题:

I know that we should use strong/weak for Obj-C objects properties/ivars. But I found that I can use strong for Class-type properties/ivars.

@property (nonatomic, strong) Class testClass;

The code will not cause a build error. Why? And should we use strong or assign?

回答1:

According to The Secret Life of Classes

A class object is not an instance, but it is definitely a full-fledged object

You don’t have to do anything to create a class object. One class object for every class your program defines is created for you automatically as the program starts up.

Of course, you can use strong for an object.

When is a class object released? - Simple answer is when program is finished.

There is no reason to care about Retain Count of an object which will be never released while program is running. It means that doesn't matter if you use strong/weak/assign, this object is still not be destroyed until program is finished.

So you can use whatever you want, they will give same result.