地址:http://www.cnblogs.com/GarveyCalvin/p/4160077.html 说的很详细,很好。
结构体其实就是自定义一个数据类型,如果结构体里面的变量也是结构体的话,也是支持用点符合获取下一级, 其中占用内存的就是里面定义的变量总和。
下面是常用的结构表达形式, 其中结构体有3种形式。
#import <Foundation/Foundation.h>
@interface StructStu : NSObject
{
struct MyPoint{
int x;
int y;
};
struct MyPoint myPoint;
}
-(void) showMyPoint;
@end
#import "StructStu.h"
@implementation StructStu
-(void) showMyPoint{
myPoint.x = 10;
myPoint.y = 20;
NSLog(@"x: %d y:%d",myPoint.x,myPoint.y);
}
@end
StructStu *myStruct = [[StructStu alloc] init];
[myStruct showMyPoint];
struct MyPoint myPoint = {200,3000}; // 初始化
NSLog(@"x: %d y:%d",myPoint.x,myPoint.y);
打印的结果如下
2015-01-06 23:08:49.451 OC基础学习[748:34315] x: 10 y:20
2015-01-06 23:08:49.452 OC基础学习[748:34315] x: 200 y:3000
Program ended with exit code: 0