Objective-C中NSNumber與NSDictionary的用法簡介_IOS

來源:互聯網
上載者:User

NSNumber的常用方法

在Objective-c中有int的資料類型,那為什麼還要使用數字對象NSNumber?這是因為很多類(如NSArray)都要求使用對象,而int不是對象。
NSNumber就是數字對象我們可以使用NSNumber對象來建立和初始化不同類型的數字對象。
NSNumber
   + (NSNumber *)numberWithInt:(int)value;
   + (NSNumber *)numberWithDouble:(double)value;
   - (int)intValue;
   - (double)doubleValue;
   .....................(對於每個基本類型,類方法都為這它分配了一個NSNumber對象,並將其設定為指定的值,這些方法都是以numberWith開始的,之後是類型,如numberWithFloat,numberWithLong,numberWithInteger.....)
封裝後取出來的方法如下:

下面就拿int做個demo:

複製代碼 代碼如下:

void number() { 
    // 將int類型的10 封裝成 一個NSNumber對象 
    NSNumber *number = [NSNumber numberWithInt:10]; 
    NSLog(@"number=%@", number); 
     
    NSMutableArray *array = [NSMutableArray array]; 
    // 添加數值到數組中 
    [array addObject:number]; 
     
    // 取出來還是一個NSNumber對象,不支援自動解包(也就是不會自動轉化為int類型) 
    NSNumber *number1 = [array lastObject]; 
     
    // 將NSNumber轉化成int類型 
    int num = [number1 intValue]; 
    NSLog(@"num=%i", num); 

NSDictionary一些常用用法

複製代碼 代碼如下:

NSArray * skyAArrays = [NSArray arrayWithObjects:@"A天空1號",@"A天空2號",@"A天空3號",nil];
    NSArray * skyBArrays = [NSArray arrayWithObjects:@"B天空1號",@"B天空2號",@"B天空3號",nil];
    NSArray * skyCArrays = [NSArray arrayWithObjects:@"C天空1號",@"C天空2號",@"C天空3號",nil];
   
    // NSArray * skyArray = [NSArray arrayWithObjects:skyAArrays,skyBArrays,skyCArrays, nil];
   
   //字典中所有的key
    NSArray * keys = [NSArray arrayWithObjects:@"name",@"sex",@"age",nil];
   //字典中所有跟key對應的value
    NSArray * values = [NSArray arrayWithObjects:@"liuhui",@"男",[NSNumbernumberWithInt:36],nil];
   //建立字典對象方法1
    NSDictionary * myDic = [[NSDictionary alloc]initWithObjects:values forKeys:keys];
    NSLog(@"my dic = %@",myDic);
   // 建立字典對象方法2    
    NSDictionary * yourDic = [[NSDictionary alloc] initWithObjectsAndKeys:skyAArrays,@"A",skyBArrays,@"B",skyCArrays,@"C",nil];
    NSLog(@"your dic = %@",yourDic);
    
    NSLog(@"%@",[yourDic objectForKey:@"A"]);
    // - (NSArray *)allKeys; 返回的是 NSArray類型,方便用 objectAtIndex取出一個個key
    NSLog(@"%@",[yourDic allKeys]);
    NSLog(@"%@",[yourDic allValues]);
   
   //添加資料(setObject 一般沒有一種key才添加,有同名的key而用這種方法,會覆蓋掉),注意:id key  是成對出現的 
    [mutableDictionary setObject:@"good lucky"forKey:@"why"]; 
    [mutableDictionary setObject:@"bye bye" forKey:@"how"]; 


   //刪除指定索引值的資料 
    [mutableDictionary removeObjectForKey:..]; 
   //刪除所有資料 
    [mutableDictionary removeAllObjects]; 

    //字典的普通遍曆(無序)
    for (int i =0; i < [yourDic count]; i++) {    
        NSLog(@"key = value <====> %@ = %@",[[yourDic allKeys] objectAtIndex:i],[yourDic objectForKey:[[yourDic allKeys]objectAtIndex:i]]);
    }
   
    // 字典的快速遍曆 取出來的obj一定是key
    for (id obj in yourDic) {   
        NSLog(@"%@",obj);
        id value = [yourDic objectForKey:obj];  
        NSLog(@"%@",value);
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.