开发者

Is it possible to create a @property for an anonymous struct in Objective-C?

I have an anonymous struct in my code that I'd like to access via an assign @property (no pointer). However, since this is an anonymous structure. Here's the cocoa code I created (even if it's cocoa code, it's relevant to objective-c in general.)

@interface ProfileViewController : UIViewController {

    struct {
       BOOL isDeviceOwner:1;

    } _statusFlags;
}

Now I'd like to create a property for _statusFlags:

@property (nonatomic, assign开发者_运维技巧)


Yes, you just define it inline where you would define the type.

@property (nonatomic, assign) struct { ... } statusFlags;

Then when you synthesize it you can do @synthesize statusFlags = _statusFlags if you really like the underscored ivars, but this will generate the ivar for you. You do not need to define it explicitly.


You can also do it by making the property or method take a pointer to a struck, you then only have to let the compiler know that the struct exists but not what is in the struct i.e. the size, for example

struct myPrivateStruct;
...
@property(assign,nonatomic)  struct myPrivateStruct * myStructProperty;

the struct myPrivateStruct then has to be then defined in your implementation file and property implement the property manually for example

struct myPrivateStruct { int a, b; float c; };

- (void)setMyStructProperty:(struct myPrivateStruct *)aValue
{
    memcpy(&myIVar,aValue,sizeof(struct myPrivateStruct));
}

this is vary similar to us @class in interface files, of Objective-C class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜