Cocoa Objective-c Property C structure assign fails

泄露秘密 提交于 2019-11-28 07:01:46

To make a change to your 'member' instance variable, you need to set it in its entirety. You should do something like:

SomeType mem = t.member;
mem.a = 10;
t.member = mem;

The problem is that t.member is being used as a "getter" (since it's not immediately followed by an '='), so t.member.a = 10; is the same as [t member].a = 10;

That won't accomplish anything, because [t member] returns a struct, which is an "r-value", ie. a value that's only valid for use on the right-hand side of an assignment. It has a value, but it's meaningless to try to change that value.

Basically, t.member is returning a copy of your 'member' struct. You're then immediately modifying that copy, and at the end of the method that copy is discarded.

Make a pointer to your struct instead, then just dereference it when you want to change a part of it.

Example:

struct myStruct {
    int a,
        b;
};

@interface myClass : NSObject {
myStruct *testStruct;
}

@property myStruct *testStruct;

Then to change a part of myStruct just do myClassObject.testStruct->a = 55;

Change the synthesize line to:

@synthesize member = _member;

Then you can assign values in one line of code:

_member.a = 10;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!