在struct中使用Objective-C对象时修正了ARC的错误。

时间:2022-09-07 07:49:32

I am having an issue with my Xcode project.

我的Xcode项目有一个问题。

I have these lines:

我有这些线:

typedef struct
{
    NSString *escapeSequence;
    unichar uchar;
}

and I am getting this error:

我得到了这个错误:

ARC forbids Objective-C objects in structs or unions.

ARC禁止在结构或工会中使用Objective-C对象。

How can I fix it?

我怎样才能修好它?

I cannot seem to find how this violates ARC but I would love to learn.

我似乎不知道这是如何违反了弧线,但我很想学习。

4 个解决方案

#1


26  

Change it to:

把它改成:

typedef struct
{
    __unsafe_unretained NSString *escapeSequence;
    unichar uchar;
}MyStruct;

But, I recommend following Apple rules from this documentation.

但是,我建议遵循苹果的规则。

ARC Enforces New Rules

弧执行新规定

You cannot use object pointers in C structures.
Rather than using a struct, you can create an Objective-C class to manage the data instead.

不能在C结构中使用对象指针。您可以创建一个Objective-C类来管理数据,而不是使用struct。

#2


5  

The safest way is to use __unsafe_unretained or directly CFTypeRef and then use the __bridge, __bridge_retained and __bridge_transfer.

最安全的方法是使用__unsafe_un保留或直接CFTypeRef,然后使用__bridge、__bridge_保留和__bridge_transfer。

e.g

typedef struct Foo {
    CFTypeRef p;
} Foo;

int my_allocating_func(Foo *f)
{
    f->p = (__bridge_retained CFTypeRef)[[MyObjC alloc] init];
    ...
}

int my_destructor_func(Foo *f)
{
    MyObjC *o = (__bridge_transfer MyObjC *)f->p;
    ...
    o = nil; // Implicitly freed
    ...
}

#3


0  

I just integrated the same code into my project from the Google Toolbox for Mac

我只是将相同的代码从Mac的谷歌工具箱集成到我的项目中。

GTMNSString-HTML.m

GTMNSString-HTML.m

Their suggestion for ARC Compatibility of adding the -fno-objc-arc flag to each file worked for me.

他们建议将-fno-objc-arc标记添加到每个文件中,为我工作。

#4


0  

When we are defining C structure in Objective C with ARC enable, we get the error "ARC forbids Objective-C objects in struct". In that case, we need to use keyword __unsafe_unretained.

当我们在Objective C中定义C结构时,有了ARC,我们就得到了“ARC禁止在struct中的Objective-C对象”的错误。在这种情况下,我们需要使用关键字__unsafe_un。

Example

例子

struct Books{

    NSString *title;
    NSString *author;
    NSString *subject;
    int book_id;
};

Correct way to use in ARC enable projects:

正确使用弧线的方法:

struct Books{

    __unsafe_unretained NSString *title;
   __unsafe_unretained NSString *author;
   __unsafe_unretained NSString *subject;
   int book_id;
};

#1


26  

Change it to:

把它改成:

typedef struct
{
    __unsafe_unretained NSString *escapeSequence;
    unichar uchar;
}MyStruct;

But, I recommend following Apple rules from this documentation.

但是,我建议遵循苹果的规则。

ARC Enforces New Rules

弧执行新规定

You cannot use object pointers in C structures.
Rather than using a struct, you can create an Objective-C class to manage the data instead.

不能在C结构中使用对象指针。您可以创建一个Objective-C类来管理数据,而不是使用struct。

#2


5  

The safest way is to use __unsafe_unretained or directly CFTypeRef and then use the __bridge, __bridge_retained and __bridge_transfer.

最安全的方法是使用__unsafe_un保留或直接CFTypeRef,然后使用__bridge、__bridge_保留和__bridge_transfer。

e.g

typedef struct Foo {
    CFTypeRef p;
} Foo;

int my_allocating_func(Foo *f)
{
    f->p = (__bridge_retained CFTypeRef)[[MyObjC alloc] init];
    ...
}

int my_destructor_func(Foo *f)
{
    MyObjC *o = (__bridge_transfer MyObjC *)f->p;
    ...
    o = nil; // Implicitly freed
    ...
}

#3


0  

I just integrated the same code into my project from the Google Toolbox for Mac

我只是将相同的代码从Mac的谷歌工具箱集成到我的项目中。

GTMNSString-HTML.m

GTMNSString-HTML.m

Their suggestion for ARC Compatibility of adding the -fno-objc-arc flag to each file worked for me.

他们建议将-fno-objc-arc标记添加到每个文件中,为我工作。

#4


0  

When we are defining C structure in Objective C with ARC enable, we get the error "ARC forbids Objective-C objects in struct". In that case, we need to use keyword __unsafe_unretained.

当我们在Objective C中定义C结构时,有了ARC,我们就得到了“ARC禁止在struct中的Objective-C对象”的错误。在这种情况下,我们需要使用关键字__unsafe_un。

Example

例子

struct Books{

    NSString *title;
    NSString *author;
    NSString *subject;
    int book_id;
};

Correct way to use in ARC enable projects:

正确使用弧线的方法:

struct Books{

    __unsafe_unretained NSString *title;
   __unsafe_unretained NSString *author;
   __unsafe_unretained NSString *subject;
   int book_id;
};