• 02-结构体的几个小应用


    1、查找某个字符串在str中的范围 (使用 rangeOfString:@"")

     1 #import <Foundation/Foundation.h>
     2 //查找某个字符串在str中的范围
     3 void test()
     4 {
     5     NSString *str = @"i love you";
     6     NSRange range = [str rangeOfString:@"ov"];
     7     //如果找不到:length = 0, location = NSNotFound = -1
     8     NSRange range1 = [str rangeOfString:@"good"];
     9     NSLog(@"ov.location = %ld, ov.length = %ld", range.location, range.length);
    10     //这里location使用%d输出,结果就是-1
    11     NSLog(@"good.location = %ld, good.length = %ld", range1.location, range1.length);
    12 }
    13 int main(int argc, const char * argv[])
    14 {
    15     test();
    16     return 0;
    17 }

    输出结果:

    2、CGRect结构体变量的赋值

     1 #import <Foundation/Foundation.h>
     2 void test()
     3 {
     4     CGPoint p1 = CGPointMake(10, 10);
     5     CGPoint p2 = CGPointMake(20, 20);
     6     CGSize s1 = CGSizeMake(100, 50);
     7     CGSize s2 = CGSizeMake(200, 80);
     8     
     9     CGRect r1 = CGRectMake(0, 0, 100, 50);
    10     CGRect r2 = {{0, 0}, {100, 50}};
    11     // 在给CGRect变量赋值时,可以直接使用CGPoint变量和CGSize变量
    12     CGRect r3 = {p1, s2};
    13     NSString *str = NSStringFromRect(r3);
    14     NSLog(@"%@", str);
    15 }
    16 int main(int argc, const char * argv[])
    17 {
    18     test();
    19     return 0;
    20 }

    当出现第10行这种情况:CGRect r2 = {{0, 0}, {100, 50}};  point坐标为{0, 0}的时候,可以使用CGPointZero来替换。CGPointZero表示的是坐标原点(x==0, y==0)。

    代码:

     1 void test1()
     2 {
     3     CGRect r1 = {{0, 0}, {100, 80}};
     4     CGRect r2 = {CGPointZero, {100, 80}};
     5     
     6     NSString *str1 = NSStringFromRect(r1);
     7     NSString *str2 = NSStringFromRect(r2);
     8     NSLog(@"%@", str1);
     9     NSLog(@"%@", str2);
    10 }
    11 int main(int argc, const char * argv[])
    12 {
    13     test1();
    14     return 0;
    15 }

    同理,size坐标为{0, 0}等价于CGSizeZero。

    综上可知:CGPointZero == CGPointMake(0, 0);  表示坐标原点

           CGSizeZero == CGSizeMake(0, 0);    表示尺寸为0

         CGRectZero == CGRectMake(0, 0, 0, 0);  

    3、利用BOOL CGPointEqualToPoint(CGPoint point1, CGPoint point2) 判断两个点是否是同一个点

    代码:

     1 #import <Foundation/Foundation.h>
     2 
     3 int main(int argc, const char * argv[])
     4 {
     5     CGPoint point1 = CGPointMake(10, 10);
     6     CGPoint point2 = CGPointMake(10, 10);
     7     CGPoint point3 = CGPointMake(10, 20);
     8     int a = CGPointEqualToPoint(point1, point2);
     9     int b = CGPointEqualToPoint(point1, point3);
    10     NSLog(@"a = %d", a);
    11     NSLog(@"b = %d", b);
    12     return 0;
    13 }

    由函数CGPointEqualToPoint(CGPoint point1, CGPoint point2); 可以推知以下函数:

      CGSizeEqualToSize(CGSize size1, CGSize size2); 判断两个尺寸是否相同

      CGRectEqualToRect(CGRect rect1, CGRect rect2);

    4、利用CGRectContainsPoint(CGRect rect, CGPoint point); 判断一个点是否在一个区域内

     1 //判断一个点是否在一个区域内
     2 void test1()
     3 {
     4     CGRect rect = CGRectMake(40, 30, 100, 50);
     5     CGPoint point = CGPointMake(50, 45);
     6     int s = CGRectContainsPoint(rect, point);
     7     NSLog(@"s = %d", s);
     8 }
     9 int main(int argc, const char * argv[])
    10 {
    11     test1();
    12     return 0;
    13 }

  • 相关阅读:
    dubbo服务provider方打印警告日志,getDeserializer
    [学习]sentinel中的DatatSource(一) ReadableDataSource
    ppt字体
    dubbo异常filter
    术语
    [转]五步git操作搞定Github中fork的项目与原作者同步
    markdown学习资料
    springboot properties
    aop备忘
    git技巧
  • 原文地址:https://www.cnblogs.com/xdl745464047/p/4004961.html
Copyright © 2020-2023  润新知