10

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.

Community
  • 1
  • 1
Dhanaraj
  • 987
  • 1
  • 10
  • 26

3 Answers3

22

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.

sb.
  • 1,140
  • 1
  • 9
  • 13
  • Oh, `t.member.a` is just fine for _reading_ the value of `member.a`. It's exactly the same as `[t member].a`. – sb. Mar 11 '10 at 20:07
  • 1
    Do you know what that sound is, highness? Those are the shrieking C# developers. If you don't believe me, just wait. They always grow louder when they're about to use dot notation in Objective-C! http://www.youtube.com/watch?v=Tfo3nEZHNLk – Steve Oct 31 '11 at 00:13
  • 2
    @sb. why `t.member` is returning a copy of the struct? – Fred Collins Feb 21 '12 at 06:13
  • 2
    So.. how efficient is this assignment if your struct contains thousands of primitives and you just change one? – quantumpotato Oct 23 '12 at 23:25
  • 1
    @quantumpotato Not extremely efficient since the whole structure has to be copied on the stack, but if you really have a struct that contains a thousand fields efficiency is probably the least of your concerns. In that case you could have your method return a pointer to the struct and assign it directly with `[t member]->a = 10`. – Taum Nov 04 '13 at 16:45
4

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;

Dmacpro
  • 491
  • 4
  • 11
1

Change the synthesize line to:

@synthesize member = _member;

Then you can assign values in one line of code:

_member.a = 10;

jaredsinclair
  • 12,687
  • 5
  • 35
  • 56