After Auto-ARC Conversion: Assigning retained obje

2019-03-09 17:14发布

问题:

I just converted an old project to ARC using Xcode's automatic refactoring.

@property (nonatomic, retain) NSMutableArray *cards;

was replaced by:

@property (nonatomic) NSMutableArray *cards;

This makes sense because what I've read is that "strong" is the default state. However, the following line is giving me the error in the title:

self.cards = [[NSMutableArray alloc] initWithCapacity:54];

The error is solved by adding strong back in where retain used to be:

@property (nonatomic, strong) NSMutableArray *cards;

However... if I need to go back and put strong in to every @property declaration that was retain... why did the ARC refactoring remove them all?

回答1:

I've run into the same warning and opened an Technical Support Incident. The engineer verified that the default was changed from "assign" to "strong" for reasons of consistency within ARC.

He said both the warning and the documentation are wrong and will be fixed. Until that is done, I would avoid the implicit default altogether!

Explicitly adding "strong" (as BJ Homer suggested) is a safe way to silence the warning and be compatible. But don't assume properties to be unretained by default. Always put "weak" or "assign" there, too.

Edit: The clang documentation now officially documents this change. The warning has been fixed.

Edit 2: Xcode 4.4 apparently includes the fix.



回答2:

It looks wrong that it converted nonatomic, retain to nonatomic. I've always seen it convert to nonatomic, strong. If you can produce a simple project that converts in the way you saw it then I suggest filing a radar with it.

I assume by the way that you're using the latest Xcode.