OC基礎數據類型之字符串,數組和字典

 //-------------字符串-----------------程序員

    //字符串的初始化:api

    //1:數組

    NSString *string = @"我是非凡程序員!";app

    NSLog(@"%@",string);學習

    

    //2:atom

    NSString *string2 = [NSString stringWithFormat:@"我在非凡學習了%i個月",1];spa

    NSLog(@"%@",string2);3d

    

    

    //輸出字符串長度:orm

    long length = [string length];對象

    NSLog(@"長度:%lu",length);

    

    

    //字符串比較: is Equal To

    NSString *string3 =@"非凡程序員";

    if([string3 isEqualToString:@"非凡員工"])//返回BOOL類型

    {

        NSLog(@"相等");

    }

    else

    {

        NSLog(@"不相等");

    }

    

    //字符串的比較  Compare //返回值是 101

    NSString *str = @"123";

    NSString *str1 = @"456";

    //str > str1 輸出1str < str1 輸出-1sr = str1 輸出0

    //字符如果字母,區別大小寫,轉成ASC

    NSLog(@"%li",[str compare:str1]);

    

    //字母不區分大小寫,其他與compare 相同

    NSLog(@"%li",[str caseInsensitiveCompare:str1]);

    

    

    //字符串轉化:

    NSString *string4 = @"3.141";

    float f = [string4 floatValue];

    NSLog(@"%f",f);

    NSLog(@"%.2f",f);

//    float i = [string4 intValue];

//    NSLog(@"%i",i);

    

    

    //字符串的截取;

    NSString *string5 =@"abcdefg";

    //1.從開始位置截取到下標爲3的字符串(不包括3;

    NSString *stringnews = [string5 substringToIndex:3];

    NSLog(@"%@",stringnews);

    

    //2。從該位置截取到結尾:

    stringnews =[string5 substringFromIndex:5];

    NSLog(@"%@",stringnews);

    

    //3取一個範圍 第一個數字是起始位置,第二個數字是要取的長度:

    NSRange range ={1,5};

    NSString *result = [string5 substringWithRange:range];

    NSLog(@"%@",result );

    

    

    //字符串擴展:

    NSString *string6 =@"abCdEF";

    //轉大寫:

    NSString *upper =[string6 uppercaseString];

    NSLog(@"%@",upper);

    //轉小寫:

    NSString *lower =[string6 lowercaseString];

     NSLog(@"%@",lower);

    //首字母大寫,其他轉小些:

    NSString *ul =[string6 capitalizedString];

    NSLog(@"%@",ul);

    

    

    //------------------可變字符串---------------:

    //1.OC中,直接賦值爲不可變類型,通常可變類型都會須要申請內存空間

    //申請一塊空間,返回一個空字符串

    NSMutableString *echoI = [NSMutableString string];

    

    //2.返回一個空字符串,申請一個初始存儲空間,可自動增加。

    //NSMutableString *echoIII = [NSMutableString stringWithCapacity:10];

    

    //3.直接初始化

    NSMutableString *mutableString = [[NSMutableString alloc] initWithString :@"你們好"];

    NSLog(@"%@",mutableString);

    

    //追加字符串 append

    [mutableString  appendString:@"Hello"];

    //刪除字符串 delete

    //[echoII deleteCharactersInRange: ]

    

    //查找相關的字符串

    NSRange rangeI =  [mutableString  rangeOfString:@"H"];

    //location表明找到的開始位置,length表明找到的長度,最小爲0

    NSLog(@"%lu , %lu",  rangeI.length , rangeI.location );

    

    //替換字符串 方法一

    NSString *muStr= [mutableString  stringByReplacingOccurrencesOfString:@"el" withString:@"111111"];

    NSLog(@"%@",muStr);

    

    //替換: 方法二

    NSRange range1 = {2,1};

    

    [mutableString replaceCharactersInRange:range1 withString:@"很差"];

    NSLog(@"%@",mutableString);

    

    

    //1.增長

    //1-1追加值

    [mutableString appendString:@"下午"];

    NSLog(@"%@",mutableString);

    //1-2

    [mutableString insertString:@"上午" atIndex:0];

    NSLog(@"%@",mutableString);

    

    

    //查找

    NSRange Range2 =[mutableString rangeOfString:@"上午"];

    NSLog(@"%ld",Range2.length);

    

    

    //刪除

    

    [mutableString deleteCharactersInRange:Range2];

    NSLog(@"%@",mutableString);


//---------------數組------------------

    //數組即相同數據類型的有序集合,數組有下標,下標從0開始

    

    //定義數組:不可變 不可變是不可改變相關數組的序列

    NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];

    NSArray *arrayI = @[ @"1", @"2" ];

    

    

    //輸出數組:

    for (int i=0;i<4;i++)

    {

        NSLog(@"%@",array[i]);

    }

    NSLog(@"%@", arrayI[1] );

    

    //求數組長度:

    long len =[array count];

    NSLog(@"數組的長度是%lu",len);

    

    //根據下標找元素內容:

    NSLog(@"第二個位置是%@",[array objectAtIndex:2]);

    

    // 找一個元素的位置:

    long l=[array indexOfObject:@"d"];

    NSLog(@"l=%lu",l);

    

    //判斷數組是否有某元素:

    if([array containsObject:@"e"])

    {

        NSLog(@"包含");

        

    }

    else

    {

        NSLog(@"不包含");

    }

    

    //將數組寫入到文件中:

    [array writeToFile:@"/Users/a11/Desktop/test.plist" atomically:YES];

    

    //將文件讀取到數組:

    NSArray *readArr =[NSArray arrayWithContentsOfFile:@"/Users/a11/Desktop/test.plist"];

    NSLog(@"%@",readArr);

    

    //將一個數組複製到另外一個數組:

    NSArray *array2 =[NSArray arrayWithArray:array];

    NSLog(@"array=%@",array2);

    

    

    //數組的比較:

    NSArray *array3 =[NSArray arrayWithObjects:@"x",@"y",@"z", nil];

    if([array isEqualToArray:array3])

    {

        NSLog(@"array=array3");

    }

    else

    {

        NSLog(@"不相等");

    }

    

    //-----------------可變數組----------------

    //1.初始化一個可變數組

    //1-1可變須要申請一塊內存空間

    NSMutableArray *muArray = [NSMutableArray array];

    

    //1-2申請初始的內存空間,返回一個空Array

    NSMutableArray *muArrayI = [NSMutableArray arrayWithCapacity:10];

    

    //給數組添加對象

    //1-1在末尾增長

    [muArray addObject:@"a"];

    [muArray addObject:@"b"];

    NSLog(@"%@",muArray);

    

    //1-2在任意位置插入值:

    [muArray insertObject:@"攪局" atIndex:2];

    

    

    //把不可變的Array變成可變的Array

    NSMutableArray *muArrayII= [NSMutableArray arrayWithArray:muArray];

    

    //若是初始長度爲2,那追加一個數據到可變數組裏此事的長度爲3,也就是最大下標就是2

    

    //追加用add

    [muArrayII addObject:@"3"];

    

    //獲得相關的數組的長度[muArrayII count]

    NSLog(@"%@, %li", muArrayII[2], [muArrayII count]);

    

    //刪除用remove

    [muArrayII removeObject:@"2"];//移除某一個元素

    //[muArrayII removeAllObjects];//移除所有

    

    //替換某個下標的數據 某個數

    [muArrayII replaceObjectAtIndex:1 withObject:@"20"];

    

    //查找某個下標的數據 遍歷查找 muArrayII[下標] C的寫法

    //查找的第一種方法

    for( int i = 0 ; i < [muArrayII count]; i++ ){

        NSLog(@"for:%@", muArrayII[i] );

    }

    // NSLog(@"%@, %li", muArrayII[1], [muArrayII count]);

    

    //查找的第二種方法

    for(NSString *object in muArrayII){

        NSLog(@"%@",object);


    }


//----------------字典---------------

    //初始化

    NSDictionary *dict = @{@"key1":@"value1",@"key2":@"value2"};

    NSLog(@"%@",dict);

    

    //NSNumber做爲健,字符串做爲健,數組做爲健,字典做爲健

    NSDictionary *dict2 = @{@1:@"value1",@"string":@"stringValue",@[@"array",@"array1"]:@"arrayValue",@{@"key1":@"value1"}:@"value"};

    NSLog(@"%@",dict2);

    

    //--------------可變字典----------------

    

    NSMutableDictionary *muDict = [NSMutableDictionary dictionaryWithCapacity:10];

    [muDict setObject:@"楊茹" forKey:@"1-1"];

    [muDict setObject:@"down" forKey:@"1-2"];

    [muDict setObject:@"left" forKey:@"1-3"];

    [muDict setObject:@"right" forKey:@"1-4"];

    

    //1.初始化

    //1-1鍵值經過兩個數組設置值。注意:兩個數組的個數要相同

    NSMutableDictionary *muDictI=[[NSMutableDictionary alloc]initWithObjects:@[@"left",@"right"] forKeys:@[@"1",@"2"]];

    //1-2 經過鍵值同時設置的方式設置,注意這裏是雙數個數的對象,不然會出現鍵值不對應,是值-鍵-值-鍵的關係。

    NSMutableDictionary *muDictII = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];

    //1-3定義一段內存空間

    NSMutableDictionary *muDictIII = [[NSMutableDictionary alloc]initWithCapacity:10];

    

    //不可變字典轉換爲可變字典

    NSMutableDictionary *muDict2 = [NSMutableDictionary dictionaryWithDictionary:dict];

    

    //經過count計算鍵值對個數

    NSLog(@"%li", [muDictI count]);

    

    //根據健取值

    id string = [muDict objectForKey:@"1-1"];

    NSLog(@"%@",string);

    NSLog(@"%@",muDict);

    

    //增長

    [muDict setObject:@"forward" forKey:@"1-1"];

    NSLog(@"%@",muDict);

    

    //刪除

    // 1. 刪除全部

    //[muDict removeAllObjects];

    

    // 2.根據一個鍵刪除對應的鍵和值

    [muDict removeObjectForKey:@"1-1"];

    NSLog(@"%@",muDict);

    

    //3.根據多個鍵刪除

    [muDict removeObjectsForKeys:@[@"1-1",@"1-2"]];

    

    //注意:一個鍵對應一個值,所以,經過某個鍵改變或增長對應的值的時候已經將以前的值覆蓋掉

    


    

    

    //-------------NSNumber----------

    //NSNumber 類型能夠是Idintfloatdoublechar

    NSNumber *number = [[NSNumber alloc]initWithInt:2];

    NSLog(@"%@",number);

    

    NSNumber *number2 = @4;

    NSLog(@"%@",number2);

    

    

    //---------------NSSet------------

    /*

     NSSetNSArray對比:

     NSSet中的元素能夠重複,NSSArray不能重複

     NSSet是無序的,NSSArray是有序的。

     */


    NSSet *set = [NSSet setWithObject:@"20"];

    NSLog(@"%@",set);

相關文章
相關標籤/搜索