[oc學習筆記]不可變數組,oc學習筆記

來源:互聯網
上載者:User

[oc學習筆記]不可變數組,oc學習筆記

     
NSArray *aa = [[NSArray alloc]init]; NSArray *aaa = [NSArray array]; //聲明單個內容數組 NSArray *aaaa = [NSArray arrayWithObject:@"sd"]; //聲明多個內容數組 NSArray *arr = [NSArray arrayWithObjects:@"SDS",@"Dsad", nil]; //數組的長度 NSLog(@"%zi",arr.count); //數組中某個下標對應的對象 NSLog(@"%@",[arr objectAtIndex:1]); //數組中最後一個對應的對象 NSLog(@"%@",[arr lastObject]); //數組中某個元素對應的下標 NSLog(@"%zi",[arr indexOfObject:@"Dsad"]); //寫入檔案 NSString *path = @"/Users/XuLee/Desktop/oc/abc.txt"; [arr writeToFile:path atomically:YES]; //讀檔案必須是特定格式 NSString *ff = [NSArray arrayWithContentsOfFile:path]; //把數組的對象中間加一個東西串連成一個字串 NSString *fg = [arr componentsJoinedByString:@"-"]; //數組的遍曆 //普通遍曆方式 for (int i = 0; i < arr.count; i++) { id obj = [arr objectAtIndex:i]; NSLog(@"%@",obj); } //快速遍曆 for (id obj in arr ) { NSLog(@"%@",obj); } //代碼塊遍曆 [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) // obj為元素 idx 為下標 stop相當於break stop為yes時 迴圈會結束 { NSLog(@"%@",obj); //stop 賦值形式為 *stop = yes or no }]; //迭代器遍曆 //將數組中各個元素裝入迭代器中 NSEnumerator * vv = [arr objectEnumerator]; id obj = nil; while (obj = [vv nextObject]) { NSLog(@"%@",obj); } //反向迭代 NSEnumerator *vvv = [arr reverseObjectEnumerator]; while (obj = [vvv nextObject]) { NSLog(@"%@",obj); } //讓數組統一調用某方法 Student *stu1 = [[Student alloc]init]; stu1.name = @"fff"; Student *stu2 = [[Student alloc]init]; stu2.name = @"hhh"; NSArray *aee = [NSArray arrayWithObjects:stu1,stu2,nil]; [aee makeObjectsPerformSelector:@selector(sayHi)]; //讓數組統一調用某一帶參數的方法 [aee makeObjectsPerformSelector:@selector(study:) withObject:@"math"];
-(NSString *)description{    NSString *aa = [NSString stringWithFormat:@"%@",_name];    return aa;}-(void)sayHi{    NSLog(@"my name %@",_name);}-(void)study:(NSString *)ss{    NSLog(@"%@ study %@",_name,ss);}
@property (nonatomic,strong)NSString *name;-(void)sayHi;-(void)study:(NSString *)ss;

 

相關文章

聯繫我們

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