美文网首页
IOS结构体、联合、枚举

IOS结构体、联合、枚举

作者: 我不白先生 | 来源:发表于2020-09-08 14:13 被阅读0次

2.结构体
2.1是程序员自己创建的一种数据类型
2.2语法
2.2.1结构体的创建
2.2.2 使用结构体类型定义变量
2.2.3结构体变量的操作
2.2.3.1初始化:整体进行
2.2.3.2赋值:只能通过点运算符,逐个进行
2.2.3.3访问:只能通过点运算符,逐个进行
2.3字符对齐规则
2.3.1找到结构体中占内存最大的基本数据类型的成员变量
2.3.2以该成员所占字节数为单位,为每个成员变量分配存储空间
2.3.3从第二个成员开始,将待分配的字节编号除以该成员所占的字节数。如果余数不为0,则空出该字节不用;如果余数为0,则将该字节分配给该变量

结构体Size4.png
2.4使用结构体的目的是为了封装细节,简化编程。
struct Example
{
    int i;//成员变量
    char ch;
    double d;
};
typedef struct
{
    int I;
    char ch;
    double d;
    
}Example1;
typedef struct
{
    char ch;
    int I;
}Size1;
typedef struct
{
    Size1 s;
    short int si;
}Size4;
typedef struct
{
    char *name;
    int age1;
    int ID;
    char *address;
}Student;

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(void)method1;//语法
-(void)method2;//字符对齐规则使用结构体的目的
-(void)method3;//使用结构体的目的
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self method1];
    [self method2];
    [self method3];
}
-(void)method1
{
    long int a;
    a = 10;
    self.outputLabel.text = [NSString stringWithFormat:@"%ld",a];
    struct Example s;
    s.i = 10;//点是成员运算符
    s.ch = 'a';//结构体变量s只能逐个成员进行赋值,不能整体赋值
    s.d = 3.14;
    self.outputLabel.text = [NSString stringWithFormat:@"%d,%c,%g",s.i,s.ch,s.d];//结构体变量s只能逐个成员进行访问,不能整体访问
    struct Example s1 = {40,'b',3.14};
    self.outputLabel.text = [NSString stringWithFormat:@"%d,%c,%g",s1.i,s1.ch,s1.d];//初始化必须整体进行
    
    Example1 s2 = {50,'c',2.68};
    self.outputLabel.text = [NSString stringWithFormat:@"%d,%c,%g",s2.i,s2.ch,s2.d];
}
-(void)method2
{
    self.outputLabel.text = [NSString stringWithFormat:@"%lu",sizeof(Size1)];
     self.outputLabel.text = [NSString stringWithFormat:@"%lu",sizeof(Size4)];
}
-(void)method3
{
    char *name1 = "张三";
    int age1 = 18;
    int ID1 = 1000;
    char *address1 = "江苏南京";
    char *name2 = "李四";
    int age2 = 20;
    int ID2 = 1001;
    char *address2 = "广东深圳";
    //...
    Student stu1 = {"张三",18,1000,"江苏南京"};
}

3.联合
3.1也是程序员创建的一种数据类型
3.2语法
3.3 联合中所有成员变量共用一块儿存储空间,这块空间的大小是最大的成员呢变量的字节数。

#import "ViewController.h"
union U
{
    int x;
    int y;
    int z;
};
typedef union
{
    double d;
    int I;
}U1;
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    union U u1;
    u1.x = 100;
    self.outputLabel.text = [NSString stringWithFormat:@"x=%d\ny=%d\nz=%d",u1.x,u1.y,u1.z];
    u1.y = 200;
    self.outputLabel.text = [NSString stringWithFormat:@"x=%d\ny=%d\nz=%d",u1.x,u1.y,u1.z];
    
    U1 u2={3.14};//初始化时,只能给第一个成员变量赋值
    self.outputLabel.text = [NSString stringWithFormat:@"d=%g\ni=%d",u2.d,u2.i];
}

4.枚举
4.1也是程序员创建的一种数据类型
4.2没有成员变量,而是枚举常量

#import "ViewController.h"
enum Week
{
    Monday,Tuesday,Wednesday,Thurday,Friday,Saturday,Sunday,
};
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(NSString *)stringOfWeek:(enum Week)day;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.outputLabel.text = [NSString stringWithFormat:@"%d\n%d\n%d\n%d",Monday,Tuesday,Wednesday,Sunday];
    self.outputLabel.text = [self stringOfWeek:Wednesday];
}
-(NSString *)stringOfWeek:(enum Week)day
{
    switch (day) {
        case Monday:
            return @"星期一";
            break;
        case Tuesday:
            return @"星期二";
            break;
        case Wednesday:
            return @"星期三";
            break;
        case Thurday:
            return @"星期四";
            break;
        case Friday:
            return @"星期五";
            break;
        case Saturday:
            return @"星期六";
            break;
        case Sunday:
            return @"星期天";
            break;
        default:
            break;
    }
}

@end

代码太多了用方法封装
常量太多了用枚举来封装
变量太多了用结构体来封装
练习小球跳转

#import "ViewController.h"

typedef enum
{
    LEFTUP,LEFTDOWN,RIGHTUP,RIGHTDOWN,
}Direction;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)move:(UIButton *)sender {
    CGRect rect = sender.frame;
    rect.size.width = 100;
    rect.size.height = 100;
    int screenWidth =[[UIScreen mainScreen]bounds].size.width;
    int screenHeight =[[UIScreen mainScreen]bounds].size.height;
    int maxWidth = screenWidth - rect.size.width;
    int maxHeight = screenHeight - rect.size.height;
    
    static Direction direction = RIGHTDOWN;
    switch (direction)
    {
        case RIGHTDOWN:
            rect.origin.x += 10;
            rect.origin.y += 10;
            if(rect.origin.x > maxWidth)
            {
                direction = LEFTDOWN;
                rect.size.width = screenWidth - rect.origin.x;
            }
            if(rect.origin.y > maxHeight)
            {
                direction = RIGHTUP;
            }
            break;
        case LEFTDOWN:
            rect.origin.x -= 10;
            rect.origin.y += 10;
            if(rect.origin.x < 0)
            {
                direction = RIGHTDOWN;
            }
            if(rect.origin.y > maxHeight)
            {
                direction = LEFTUP;
                
            }
            break;
        case RIGHTUP:
            rect.origin.x += 10;
            rect.origin.y -= 10;
            if(rect.origin.x > maxWidth)
            {
                direction = LEFTUP;
            }
            if(rect.origin.y < 0)
            {
                direction = RIGHTDOWN;
            }
            break;
        case LEFTUP:
            rect.origin.x -= 10;
            rect.origin.y -= 10;
            if(rect.origin.x <  0)
            {
                direction = RIGHTUP;
            }
            if (rect.origin.y < 0)
            {
                direction = LEFTDOWN;
            }
            break;
        default:
            break;
    }
    
    
    sender.frame = rect;
}

```![圆.png](https://img.haomeiwen.com/i13471358/5ef899bc632b9809.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

相关文章

网友评论

      本文标题:IOS结构体、联合、枚举

      本文链接:https://www.haomeiwen.com/subject/apddektx.html