Dha*_*raj 10 cocoa structure properties objective-c
我想更改变量值,它是另一个类的结构的成员.但价值并没有改变.
这是代码.
//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
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的链接.
此致,Dhana.
sb.*_*sb. 22
要对"成员"实例变量进行更改,您需要完整地设置它.你应该做的事情如下:
SomeType mem = t.member;
mem.a = 10;
t.member = mem;
Run Code Online (Sandbox Code Playgroud)
问题是t.member被用作"吸气剂"(因为它不会立即跟随'='),所以t.member.a = 10;就像[t member].a = 10;
那将无法完成任何事情,因为[t member]返回一个结构,这是一个"r值",即.一个仅在作业右侧使用的值.它有一个值,但试图改变这个值是没有意义的.
基本上,t.member返回'member'结构的副本.然后,您将立即修改该副本,并在该方法结束时丢弃该副本.
| 归档时间: |
|
| 查看次数: |
7345 次 |
| 最近记录: |