12

I am having an issue with my Xcode project.

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.

How can I fix it?

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Seth bollenbecker
  • 121
  • 1
  • 1
  • 3
  • possible duplicate of [Alternative to Objective-C objects in structs (ARC)](http://stackoverflow.com/questions/10055404/alternative-to-objective-c-objects-in-structs-arc) – Kurt Revis Feb 09 '13 at 04:37
  • Also: [ARC forbids Objective-C objects in structs or unions despite marking the file -fno-objc-arc](http://stackoverflow.com/questions/8093099/arc-forbids-objective-c-objects-in-structs-or-unions-despite-marking-the-file-f), [Objective-C classes in structs with ARC](http://stackoverflow.com/questions/10851870/objective-c-classes-in-structs-with-arc), [the ARC documentation](http://clang.llvm.org/docs/AutomaticReferenceCounting.html#ownership-qualified-fields-of-structs-and-unions). – Kurt Revis Feb 09 '13 at 04:39
  • brother, did u use the above code for epub reader. If so pls guide me to convert it to arc. It would be extremely helpful for me. – Arunavh Krishnan May 20 '14 at 16:09

4 Answers4

34

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
bitmapdata.com
  • 9,572
  • 5
  • 35
  • 43
8

The safest way is to use __unsafe_unretained or directly CFTypeRef and then use the __bridge, __bridge_retained and __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
    ...
}
lu_zero
  • 978
  • 10
  • 14
1

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.

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;
};
manman
  • 4,743
  • 3
  • 30
  • 42
Anil Gupta
  • 1,155
  • 1
  • 19
  • 26
0

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

GTMNSString-HTML.m

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

foldinglettuce
  • 522
  • 10
  • 28
  • brother, did u use the above code for epub reader. If so pls guide me to convert it to arc. It would be extremely helpful for me.... – Arunavh Krishnan May 20 '14 at 16:10