I want to change the variable value which is a member of a structure of another class.
But the value is not getting changed.
Here is the code.
//Structure..
typedef struct {
int a;
double b;
} SomeType;
//Class which has the structure as member..
@interface Test2 : NSObject {
// Define some class which uses SomeType
SomeType member;
}
@property SomeType member;
@end
@implementation Test2
@synthesize member;
@end
//Tester file, here value is changed..
@implementation TesstAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
Test2 *t = [[Test2 alloc]init];
t.member.a = 10;
//After this the value still shows 0
}
@end
I tried out with the below link.
Structure as a class member in Objective C
Regards,
Dhana.
To make a change to your ‘member’ instance variable, you need to set it in its entirety. You should do something like:
The problem is that
t.memberis being used as a “getter” (since it’s not immediately followed by an ‘=’), sot.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.memberis 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.