iOS - OC NSPoint 位置

本文详细介绍了NSPoint结构体的创建、赋值及调用方法,并演示了如何将NSPoint与NSString相互转换。

前言

  • 结构体,这个结构体用来表示事物的一个坐标点。

        typedef CGPoint NSPoint;
    
        struct CGPoint {
            CGFloat x;
            CGFloat y;
        };
    
        typedef struct CGPoint CGPoint;

1、NSPoint 结构体变量的创建与调用

    // NSPoint 结构体变量的创建与赋值
            
        // 先定义变量,再赋值
        NSPoint point1;
        point1.x = 6;
        point1.y = 1;
            
        // 定义时直接赋值
        NSPoint point2 = {7, 2};
        
        // 给指定成员赋值
        NSPoint point3 = {.y = 3, .x = 8};
        
        // 使用函数赋值
        NSPoint point4 = NSMakePoint(9, 4);
        
        // 使用等价的结构体定义,等价于 CGPoint point5 = CGPointMake(10, 5);
        NSPoint point5 = CGPointMake(10, 5);
            
    // NSPoint 结构体变量值的调用
            
        NSLog(@"point1: %.0f, %.0f", point1.x, point1.y);
        NSLog(@"point2: %.0f, %.0f", point2.x, point2.y);
        NSLog(@"point3: %.0f, %.0f", point3.x, point3.y);
        NSLog(@"point4: %.0f, %.0f", point4.x, point4.y);
        NSLog(@"point5: %.0f, %.0f", point5.x, point5.y);

2、NSPoint 与 NSString 的相互转换

    // NSPoint 转 NSString
    NSString *stringFronPoint = NSStringFromPoint(point5);
        
    // NSString 转 NSPoint
    NSPoint point6 = NSPointFromString(stringFronPoint);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值