OC集合

来源:互联网 发布:淘宝直通车默认出价 编辑:程序博客网 时间:2024/06/05 03:06

#import <Foundation/Foundation.h>

#import "Student.h"

void bl(NSMutableArray *temp){// bl是遍历


   for (id t in temp) {

       NSLog(@"%@",t);

    }

}

int main(int argc,const char * argv[])

{


    @autoreleasepool {

       //创建数组对象

       NSArray *array=[NSArrayarrayWithObjects:@"one",@"two",@"3",@"4",@"5",nil];

       // NSArray *array=@[@"one",@"two"];//同上可以用,但不建议使用,因有些手机版本用不了

       int ct=[array count];//获取数组的大小

       for (int i=0; i<ct; i++) {//遍历数组的大小

           // NSLog(@"%d=%@",i,array[i]);//同下可以用,但不建议使用,因有些手机版本用不了

           NSLog(@"遍历数组为%d=%@",i,[arrayobjectAtIndex:i]);

        }

      //遍历数组

       for (int i=0; i<ct; i++) {

           NSString *temp=[array objectAtIndex:i];

           NSLog(@"遍历数组为%d=%@",i,temp);

        }

        //forin可以快速遍历容器  只适用于容器

       for (NSString *tempin array) {

           NSLog(@"遍历数组为%@",temp);

        }

       //创建数组

        NSArray *array2=[NSArrayarrayWithObjects:[NSNumbernumberWithInt:1],[NSNumbernumberWithInt:2],nil];

       bl(array2);

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

           NSNumber *temp=[array2 objectAtIndex:i];

            //NSLog(@"%d=%@",i,temp);//等同于下排

           NSLog(@"%d=%d",i,[tempintValue]);

        }

       //可变数组

        NSLog(@"-------------可变数组");

        NSMutableArray *mary=[[NSMutableArrayalloc]initWithObjects:@"spple",@"htc",@"iphone",nil];

       //可添加元素

        [maryaddObject:@"联想"];

       bl(mary);

        [mary removeObjectAtIndex:3];

        NSLog(@"删除后的容器内容。。。。。。。");

       bl(mary);

        NSLog(@"排序后的结果---------");

        [marysortUsingSelector:@selector(compare:)];

       bl(mary);

        NSLog(@"学生对象排序-------");

        Student *st1=[StudentstudentWithName:@"dav"andScore:90];

        Student *st2=[StudentstudentWithName:@"lisha"andScore:60];

        Student *st3=[StudentstudentWithName:@"micky"andScore:80];

       NSArray *array01=[NSArrayarrayWithObjects:st1,st2,st3, nil];

       for (Student *objectin array01) {

           NSLog(@"学生姓名:%@,学生成绩%d",object.name,object.score);

        }

       //进行排序

       NSArray *array02=[array01 sortedArrayUsingSelector:@selector(myCompare:)];//调用的是array01的数据

        NSLog(@"学生对象排序后---------");

       for (Student *objectin array02) {

           NSLog(@"学生姓名:%@,学生成绩:%d",object.name,object.score);

        }

       //字典的创建

        NSLog(@"字典使用.......");

       NSDictionary *dic1=[NSDictionarydictionaryWithObjectsAndKeys:st1,@"101",st2,@"102",st3,@"250",nil];

       NSArray *keys=[dic1 allKeys];

       for (NSString *objectin keys/*dic1 也对*/ ) {

           Student *st=[dic1 objectForKey:object];

           NSLog(@"学生姓名%@,学生成绩:%d",st.name,st.score);

        }

    NSMutableDictionary *dic2=[NSMutableDictionarydictionaryWithObjectsAndKeys:st1,@"101",st2,@"102",st3,@"250",nil];

    //添加数据到字典

    Student *st4=[StudentstudentWithName:@"kelly"andScore:70];

    [dic2setObject:st4 forKey:@"109"];

    NSLog(@"添加后。。。。。。");

   for (NSString *objectin dic2) {

       Student *st=[dic2 objectForKey:object];

       NSLog(@"学生姓名:%@,学生成绩%d",st.name,st.score);

    }

    [dic2 removeObjectForKey:@"250"];

    NSLog(@"删除后.........");

   for (NSString *objectin dic2) {

       Student *st=[dic2 objectForKey:object];

       NSLog(@"学生姓名%@,学生成绩%d",st.name,st.score);

    }

    //set容器的使用

    NSLog(@"set容器的使用.........");

    NSSet *set=[NSSetsetWithObjects:@"one",@"two",@"three",nil];//创建

   NSLog(@"%@",[setanyObject]);//取值(随机)

   NSArray *setAry=[set allObjects];

   NSLog(@"%@",[setAryobjectAtIndex:1]);//循环遍历

   for (id objectin set) {

       NSLog(@"%@",object);

    }

    //判断是否包含某个元素

   if ([set member:@"one"]) {

        NSLog(@"set中包含了one元素");

    }

    //数值类nsvalue可用于将基本struct变量转换为oc对象

   NSRect rect=NSMakeRect(0,0, 320, 480);

   NSValue *val=[NSValuevalueWithBytes:&rect objCType:@encode(NSRect)];//这排代码等同于下排代码

   NSValue *val1=[NSValuevalueWithRect:rect];//这排等同于上排代码

    //rect中的内容 传到rect2

   NSRect rect2;

    [valgetValue:&rect2];

    //将结构体rect2变成nsstring,并打印

    NSLog(@"%@",NSStringFromRect(rect2));

}


原创粉丝点击