Fix ARC error when using Objective-C object in structure

I am having a problem with my Xcode project.

I have the following lines:

typedef struct
{
    NSString *escapeSequence;
    unichar uchar;
}

and I get this error:

ARC prohibits Objective-C objects in structures or unions.

How can i fix this?

I cannot find how this violates the ARC, but I would love to know.

+5
source share
4 answers

Change it to:

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

But I recommend following Apple's rules from this documentation .

ARC provides new rules

You cannot use object pointers in C structures.

Instead of using a structure, you can create an Objective-C class to manipulate data instead.

+26
source

- __unsafe_unretained CFTypeRef, __bridge, __bridge_retained __bridge_transfer.

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
    ...
}
+5

Google Toolbox Mac

GTMNSString-HTML.m

ARC Compatibility -fno-objc-arc , .

0

When we define a C structure in Objective-C with ARC support, we get the error "ARC forbids Objective-C objects in the structure." In this case, we need to use the keyword __unsafe_unretained.

Example

struct Books{

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

The correct way to use ARC enable in projects is:

struct Books{

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

All Articles