從頭開始-07.Foundation架構常用結構體

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   使用   ar   for   div   

一、Foundation架構常用結構體NSRange\CGRange、NSPoint\CGPoint、NSSize\CGSize、 NSRect\CGRect 的使用

 1.  基本使用:

  //NSRange的使用    NSRange r1 = NSMakeRange(2, 4);  //第一個參數為.location 第二個參數為.length        NSString *str = @"學習OC";        NSRange range = [str rangeOfString:@"學習"];  //尋找某個字串在str字串中的位置和長度        NSLog(@"loc = %ld, len = %ld",range.location, range.length);        NSRange range1 = [str rangeOfString:@"找不到"];  //尋找某個字串在str字串中的位置和長度        NSLog(@"loc = %d, len = %ld",range1.location, range1.length);  //如果找不到,location 的傳回值為-1,要用%d輸出location 的值才會有負號,預設的%ld是沒有符號              //CGPoint的使用    CGPoint point1 = NSMakePoint(10, 10);    NSPoint point2 = CGPointMake(10, 10); //平常經常使用這種方法定義一個CGPoint 對象            //NSSzie的使用    NSSize size1 = CGSizeMake(10, 20);    CGSize size2 = NSMakeSize(10, 20);            //CGRect 的使用    CGRect rect1 = CGRectMake(0, 0, 50, 50);    CGRect rect2 = {point1, size1};            //列印結構體的方法    NSString *s = NSStringFromPoint(point1);    NSLog(@"%@",s);        NSString *s1 = NSStringFromRect(rect1);    NSLog(@"%@",s1);

2. 相關的其他方法

//判斷結構體NSPoint\CGPoint、NSSize\CGSize、 NSRect\CGRect 包含的值是否相同的方法    CGPointEqualToPoint(point1, point2);    CGRectEqualToRect(rect1, rect2);    CGRectEqualToRect(rect1, rect2);        //判斷某個rect是否包含某個點,必須引用架構CoreGraphic.framework,這樣才能調用 CGRectContainsPoint方法    BOOL b1 = CGRectContainsPoint(rect1, point1);    NSLog(@"%d",b1);    return 0;

二、Foundation架構常用類

1.NSString

    //建立字串    NSString *s1 = @"test";    NSString *s2 = [[NSString alloc]initWithFormat:@"test is %c",‘c‘];        //此方法可以將c語言字串轉化為OC字串    NSString *s3 = [[NSString alloc]initWithUTF8String:"name"];        //OC字串轉化為C語言字串    const char *cs = [s3 UTF8String];        NSString *s4 = [[NSString alloc] initWithContentsOfFile:@"/Users/lpmac/Desktop/my.text" encoding:NSUTF8StringEncoding error:nil];    [NSString stringWithContentsOfFile:@"/Users/lpmac/Desktop/my.text" encoding:NSUTF8StringEncoding error:nil];                NSURL *url = [[NSURL alloc]initWithString:@"file:///Users/lpmac/Desktop/my.text"];    //類方法    [NSURL URLWithString:@"file:///Users/lpmac/Desktop/my.text"];    NSString *s5 = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];     //類方法    [NSString stringWithContentsOfFile:@"file:///Users/lpmac/Desktop/my.text" encoding:NSUTF8StringEncoding error:nil];    NSLog(@"%@",s5);

2.NSMutableString的使用(動態字串)

    NSMutableString * mutableString1 = [NSMutableString stringWithFormat:@"test"];    //拼接字串    [mutableString1 appendString:@"append str"];        //刪除    NSRange range = [mutableString1 rangeOfString:@"test"];    [mutableString1 deleteCharactersInRange:range];    NSLog(@"%@",mutableString1);

3.NSArray 和 NSMutableArray

      //數組的建立    NSArray *array = [NSArray arrayWithObject:@"test"];    NSArray *array1 = [NSArray arrayWithObjects:@"test1",@"test2",@"test2",nil];    NSArray *array2 = @[@"test1",@"test2",@"test2"]; //提倡快速寫法        //數組訪問    [array1 objectAtIndex:0];    array1[0]; //提倡        //數組的遍曆    for (int i=0; i<array.count; i++) {        NSLog(@"%@",array[i]);    }
    //快速遍曆     for (id obj in array2) {                NSUInteger loc = [array2 indexOfObject:obj];         NSLog(@"%@,%ld",obj,loc);           }            [array2 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {          NSLog(@"%@,%ld",obj,idx);        if (idx == 1) {            *stop = YES;        }    }];

NSMutableArray 是動態數組不可使用文法@[] 來初始化

從頭開始-07.Foundation架構常用結構體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.