main.m
//
// main.m
// NSArray
//
// Created by Rayln Guan on 9/4/13.
// Copyright (c) 2013 Rayln Guan. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
#pragma mark 数组的基本用法
NSArray *arr = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];
//得到数组长度
NSUInteger count = [arr count];
NSLog(@"%zi", count); //返回3
//返回是否包含字符
BOOL iscontain = [arr containsObject:@"1"];
NSLog(@"bool: %i", iscontain); // 返回0
//返回最后的元素
NSString *last = [arr lastObject];
NSLog(@"last: %@", last); //返回c
//根据标签返回元素
NSString *index = [arr objectAtIndex:1];
NSLog(@"index: %@",index); //返回b
//查找位置
NSUInteger indexObj = [arr indexOfObject:@"c"];
NSLog(@"%zi", indexObj); //返回2
//Array在内存中的管理
Student *stu1 = [[Student alloc] init];
Student *stu2 = [[Student alloc] init];
//此处stu1, stu2的retainCount = 1
NSArray *array = [NSArray arrayWithObjects:stu1, stu2, nil];
//此处的stu1, stu2的retainCount = 2
//当stu1, stu2被销毁。
[stu1 release];
[stu2 release];
#pragma mark Array的list对象全部调用方法
//Array的list对象全部调用方法
[array makeObjectsPerformSelector:@selector(test)];
[array makeObjectsPerformSelector:@selector(test2:) withObject:@"params1"];
#pragma mark 遍历数组
//遍历数组1
for (NSUInteger i = 0; i < array.count; i++) {
NSLog(@"%@", [array objectAtIndex:i]);
}
//遍历数组2
for (Student *stu in array) {
NSLog(@"forEach--%@", stu);
}
//遍历数组3-推荐使用block, 利用block进行for循环调用
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// obj是当前对象,idx是当前位置, stop是是否停止
//停止循环遍历
*stop = YES;
}];
//遍历数组4-迭代器遍历
NSEnumerator *enumer = [array objectEnumerator];
//NSEnumerator *enumer = [array reverseObjectEnumerator] 获取反序迭代器
id obj = nil;
while (obj = [enumer nextObject]) {
NSLog(@"Enumer-%@:", obj);
}
//取得没有被遍历的所有对象
NSArray *arrayEnum = [enumer allObjects]; //因此这将输出[],如果在while之前遍历,将是所有对象
#pragma mark -
#pragma mark 派生出新的array
//newArray派生出新的类,而原本的array不受影响
NSArray *newArray = [array arrayByAddingObject:[[Student alloc] init]];
NSArray *newArray2 = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"a", @"b", nil]];
//截取范围内的Array
NSRange range = NSMakeRange(1, 2);
NSArray *newArray3 = [newArray2 subarrayWithRange:range];
NSLog(@"%@", newArray3); //返回2个元素
#pragma mark 数组的其他用法
//用一个分隔符把数组拼接成字符串
NSString *str = [array componentsJoinedByString:@"-"];
NSLog(@"join:%@", str);
//生成xml文件
[arr writeToFile:@"/Users/rayln/Desktop/text.xml" atomically:YES];
//从文件读取数据
NSArray *array1 = [NSArray arrayWithContentsOfFile:@"/Users/rayln/Desktop/text.xml"];
}
return 0;
}
Student.h
//
// Student.h
// NSArray
//
// Created by Rayln Guan on 9/4/13.
// Copyright (c) 2013 Rayln Guan. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
- (void) test;
- (void) test2:(NSString *)params;
@end
Student.m
//
// Student.m
// NSArray
//
// Created by Rayln Guan on 9/4/13.
// Copyright (c) 2013 Rayln Guan. All rights reserved.
//
#import "Student.h"
@implementation Student
- (void)test{
NSLog(@"调用test方法");
}
- (void)test2:(NSString *)params{
NSLog(@"调用test2方法,参数为%@", params);
}
- (void)dealloc{
NSLog(@"Student被销毁!");
[super dealloc];
}
@end