码迷,mamicode.com
首页 > 其他好文 > 详细

NSArray和NSMutableArray

时间:2016-05-06 00:28:45      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

#import <Foundation/Foundation.h>
void one()
{
    //创建数组
    NSArray *arr1=[NSArray array];
    NSLog(@"%@",arr1);
    //创建一个只有一个元素的数组
    NSArray *arr2=[NSArray arrayWithObject:@"只有一个元素"];
    //创建一个包含多个元素的数组
    NSArray *arr3=[NSArray arrayWithObjects:@"one",@"two",@"three", nil];
    //用数组可以创建另外一个数组
    NSArray *arr4=[NSArray arrayWithArray:arr3];
    NSLog(@"%@",arr4);
    //用对象方法创建数组
    NSArray *arr5=[[NSArray alloc]initWithObjects:@"1",@"2",@"3",nil];
    //数组的简化形式
    NSArray *arr=@[@"ios",@"iosay"];
    NSLog(@"%@",arr);
    
    
    //访问数组
    NSLog(@"%lu",arr3.count);//获取长度[arr count];
    //访问数组某个元素
    NSLog(@"%@",[arr3 objectAtIndex:2]);
    //取元素的下标
    NSLog(@"%lu",[arr3 indexOfObject:@"two"]);
    //是否包含了某个元素
    BOOL loc=[arr3 containsObject:@"ios"];
    if(loc)
    {
        NSLog(@"yes");
    }
    else
    {
        NSLog(@"no");
    }
    //数组的遍历
    //for循环
    for (int i=0; i<arr3.count; i++)
    {
        NSLog(@"%@",arr3[i]);
    }
    //快速枚举法
    for(NSString *temp in arr5)
    {
        NSLog(@"%@",temp);
    }
    //使用block块
    [arr2 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"%@",obj);
    }];
}

void two()
{
    //创建一个可变数组
    NSMutableArray *marr1=[NSMutableArray array];
    [marr1 addObject:@"ios"];
    NSLog(@"%@",marr1);
    marr1[1]=@"zhang yu";
    NSLog(@"%@",marr1);
    //创建一个包含多个元素的可变数组
    NSMutableArray *marr2=[NSMutableArray arrayWithObjects:@"1",@"2",@"3", nil];
    NSLog(@"%lu",marr2.count);
    NSLog(@"%@",[marr2 objectAtIndex:1]);
    NSLog(@"%lu",[marr2 indexOfObject:@"3"]);
    BOOL loc=[marr2 containsObject:@"2"];
    if(loc)
    {
        NSLog(@"yes");
    }
    else
    {
        NSLog(@"no");
    }
    //创建一个指定长度的数组
    NSMutableArray *arr=[NSMutableArray arrayWithCapacity:3];
    //扩展
    [arr addObject:@"one"];
    [arr addObject:@"two"];
    [arr addObject:@"three"];
    [arr addObject:@"ios"];
    NSLog(@"%@",arr);
    //删除
    [arr removeObject:@"three"];
    NSLog(@"%@",arr);
}
int main(int argc, const char * argv[]) {
    @autoreleasepool
    {
        //NSDictionary快速创建
        NSDictionary *dict=@{@"zy":@"zhang yu",@"hz":@"he zhi"};
        NSLog(@"%@",dict);
        //长度
        NSLog(@"%lu",[dict count]);
        //访问key和value
        NSLog(@"%@",[dict allKeys]);
        NSLog(@"%@",[dict allValues]);
        //通过键值获取value值
        NSLog(@"%@",[dict valueForKey:@"hz"]);
        
        //遍历
        for (NSString *key in dict)
        {
            NSLog(@"key=%@  value=%@",key,[dict valueForKey:key]);
        }
        
        [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            NSLog(@"%@---->%@",key,obj);
        }];
        
        
        //NSMutableDictionary创建
        NSMutableDictionary *mdict=@{@"nh":@"ni hao",@"xm":@"xiao ming"};
        NSLog(@"%@",mdict);
        ///Volumes/ZHANG YU/sophister/Two/Mac OS/P1/NSArray数组/main.m:122:30: Incompatible pointer types initializing ‘NSMutableDictionary *‘ with an expression of type ‘NSDictionary *‘
        
    }
    return 0;
}

 

NSArray和NSMutableArray

标签:

原文地址:http://www.cnblogs.com/osay/p/5463606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!