OC数组NSArray的基本使用与介绍
////main.m
//NSArray-1
//
//Created by yusian on 14-3-22.
//Copyright (c) 2014年 小龙虾论坛. All rights reserved.
//
#import <Foundation/Foundation.h>
int main()
{
/***********NSArray***********/
// 1、使用类方法arrayWithObject初始化数组,只能赋值单个元素
NSArray * a1 = ;
// NSArray * a1 = [ init];该数组永远为空
// 快速创建数组的方法,该方法后面不需要加nil,Xcode编译器特性,参照数组的赋值方法
// NSArray * a1 = @[@"yusian", @"com", @"oc"];
NSLog(@"%@", a1);
// 2、使用类方法arrayWithObjects初始化数组,可赋值任意个元素数,nil标记元素赋值结束
NSArray * a2 = ;
NSLog(@"%@", a2);
// 3、为取数组a2中元素个数的get方法,可使用点语法访问
NSLog(@"Element count of a2 is %ld", a2.count);
// 4、NSArray中元素的访问
NSLog(@"%@", );
NSLog(@"%@", a2);
/**********NSMutableArray***********/
// 1、创建可变数组的常用方法,可变数组不可使用@[];方式初始化,@[]只能用于不可变数组
NSMutableArray * ma1 = ;
NSLog(@"%@", ma1);
// 2、添加元素
;
// 3、删除元素
;
// ;
// ;
// ;
// 4、数组元素遍历
// for循环遍历
for (int i = 0; i < ma1.count; i++) {
NSLog(@"%@", ma1);
}
// for循环( id obj in ma1)
for ( id obj in ma1) {
NSLog(@"%@", obj);
}
// 数组遍历方法实现遍历
[ma1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@", obj);
if (idx == 0) {
*stop = YES;
}
}];
return 0;
}
页:
[1]