ARC 禁止在结构或联合中使用 Objective-C 对象,尽管它标记了 file-fno-objecc-ARC

ARC 禁止在结构或联合中使用 Objective-C 对象,尽管标记了 file-fno-objecc-ARC? 为什么会这样?

我假设如果将其标记为-fno-objecc-Arc,则不存在这个限制。

53200 次浏览

That is because arc can't track objects in structs or unions (since they are at that point plain C pointers).

Even though you marked the file/class in question with -fno-objc-arc you might still pass an object controlled by arc to it as parameter, which would most likely result in a memory leak.

If you got this message try __unsafe_unretained. It is only safe, if the objects in the struct are unretained. Example: If you use OpenFeint with ARC the Class OFBragDelegateStrings says this error in a struct.

typedef struct OFBragDelegateStrings
{
NSString* prepopulatedText;
NSString* originalMessage;
} OFBragDelegateStrings;

to

typedef struct OFBragDelegateStrings
{
__unsafe_unretained NSString* prepopulatedText;
__unsafe_unretained NSString* originalMessage;
} OFBragDelegateStrings;

Rather than using a struct, you can create an Objective-C class to manage the data instead.

Looks like this now works without errors, probably after this change.

i.e., You can put normal (strong) pointers to Objective-C objects in a C struct. It is managed by ARC e.g., it is unretained when the struct is destructed. Verified with:

$ clang --version Apple LLVM version 10.0.0 (clang-1000.11.45.2)