I\'m new to Objective C
I tried using a simple struct
and got
arc forbids objective-c objects in struct
Looking up ARC, it
You can use struct
in Objective-C++ with ARC in any case.
#import
@interface City : NSObject
struct Data {
NSString *name;
};
@property struct Data data;
@end
@implementation City
@end
int main()
{
City *city = [[City alloc] init];
city.data = (struct Data){@"San Francisco"};
NSLog(@"%@", city.data.name);
return 0;
}
If you compile it as Objective-C, you failed as you said.
$ clang -x objective-c -fobjc-arc a.m -framework Foundation
a.m:5:15: error: ARC forbids Objective-C objects in struct
NSString *name;
^
1 error generated.
Because C struct doesn't have the capability of management for variable life span.
But in C++, struct does have destructor function. So C++ struct is compatible with ARC.
$ clang++ -x objective-c++ -fobjc-arc a.m -framework Foundation
$ ./a.out
San Francisco