解析Objective-c 数组对象 (NSArray 和 NSMutableArray)
2014-03-31 19:58
363 查看
一、NSArray是静态数组,创建后数组内容及长度不能再修改。
实例:
[cpp] view
plaincopy
//用arrayWithObjects初始化一个不可变的数组对象。
//初始化的值之间使用逗号分开,以nil结束。
NSArray6 *city = [NSArray arrayWithObjects:@"上海",@"广州",@"重庆",nil];
for(int i=0; i < [city count];i++){
NSLog(@"%@",[city objectAtIndex:i]);
}
运行结果:
上海
广州
重庆
NSArray常用方法:
[cpp] view
plaincopy
+(id)arrayWithObjects:obj1,obj2,...nil //创建一个新的数组,obj1,obj2,.., 以nil结束。
-(BOOL)containsObject:obj //确定数组中是否包含对象obj
-(NSUInterger)count //数组中元素的个数
-(NSUInterger)indexOfObject:obj //第一个包含obj元素的索引号
-(id)ObjectAtIndex:i //存储在位置i的对象
-(void)makeObjectsPerformSelector:(SEL)selector //将selector提示的消息发送给数组中的每一个元素
-(NSArray*)sortedArrayUsingSelector:(SEL)selector //根据selector提定的比较方法对数组进行排序
-(BOOL)writeToFile:path atomically:(BOOL)flag //将数组写入指定的文件中。如果flag为YES,则需要先创建一个临时文件
二、NSMutableArray是动态数组,可以动态增加数组中的元素,同样NSMutableArray是NSArray的子类。
实例:
[cpp] view
plaincopy
//用arrayWithCapacity创建一个长度为5的动态数组
NSMutableArray *nsma = [MSMutableArray arrayWithCapacity:5];
for(int i=0;i<=50;i++) {
if( i%3 == 0 ) {
[nsma addObject:[NSNumber numberWithInteger:i]]; //用addObject给数组nsma增加一个对象
}
}
//输出数组中的元素
for(int i=0;i<[nsma count];i++) {
NSLog(@"%li",(long)[[nsma objectAtIndex] integerValue]);
}
数组排序例子:
student.h
[cpp] view
plaincopy
#import <Foundation/Foundation.h>
@interface Student: NSObject {
NSString: *name;
int age;
}
@property (copy,nonatomic) NSString* name;
@property int age;
-(void)print;
-(NSComparisonResult)compareName:(id)element;
@end
StudentTest.m
[cpp] view
plaincopy
#import <Foundation/Foundation.h>
#import "student.h"
int main( int argc, const char* agrv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Student* stu1 = [[Student alloc] init];
Student* stu2 = [[Student alloc] init];
Student* stu3 = [[Student alloc] init];
[stu1 setName:@"Sam"];
[stu1 setAge:30];
[stu2 setName:@"Lee"];
[stu2 setAge:23];
[stu3 setName:@"Alex"];
[stu3 setAge:26];
NSMutableArray *students = [[NSMutableArray alloc] init];
[students addObject:stu1];
[students addObject:stu2];
[students addObject:stu3];
NSLog(@"排序前:");
for(int i=0; i<[students count];i++) {
Student *stu4 = [students objectAtIndex:i];
NSLog(@"Name:%@,Age:%i",[stu4 name],[stu4 age]);
}
[students sortUsingSelector:@selector(compareName:)];
NSLog(@"排序后:");
for( Student *stu4 in students ) {
NSLog(@"Name:%@,Age:%i", stu4.name, stu4.age);
}
[students release];
[stu1 release];
[sut2 release];
[stu3 release];
[pool drain];
return 0;
}
结果:
[plain] view
plaincopy
排序前:
Name:Sam,Age:30
Name:Lee,Age:23
Name:Alex,Age:26
排序后:
Name:Alex,Age:26
Name:Lee,Age:23
Name:Sam,Age:30
NSMutableArray常用方法:
[cpp] view
plaincopy
+(id)array //创建一个空数组
+(id)arrayWithCapacity:size //创建一个容量为size的数组
-(id)initWithCapacity:size //初始化一个新分配的数,指定容量为size
-(void)addObject:obj //将obj增加到数组中
-(void)insertObject:obj atIndex:i //将obj插入数据的i元素
-(void)replaceObjectAtIndex:i withObject:obj //用obj替换第i个索引的对象
-(void)removeObject:obj //从数组中删除所有是obj的对象
-(void)removeObjectAtIndex:i //从数组中删除索引为i的对象
-(void)sortUsingSelector:(SEL)selector //用selector指示的比较方法进行排序
//
// main.m
// 11-OCFundation框架NSString学习
//
// Created by 谷建鹏 on 14-3-31.
// Copyright (c) 2014年 com.csdn. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
/*******************字符串的创建**************/
//1)创建字符串的常量
NSString *str =@"大家好才是真的好";
NSLog(@"str = %@",str);
//2)创建空字符串
NSString *str2 = [[NSString alloc] init];
NSString *str3 = [NSString string];
NSLog(@"str2 = %@,str3 = %@",str2,str3);
//3)快速创建字符串
NSString *str4 = [NSString stringWithString:@"hello csdn"]; //类方法
NSString *str5 = [[NSString alloc] initWithString:@"初始化字符串"]; //实例方法来实现 初始化
NSLog(@"str4 = %@,str5 = %@",str4,str5);
//4)快速创建一个格式化的字符串
NSString *str6 = [[NSString alloc] initWithFormat:@"pi = %f",3.1415926];
NSString *str7 = [NSString stringWithFormat:@"pi = %f",3.1415926];
NSLog(@"str6 = %@,str7 = %@",str6,str7);
//5)测试字符串是否相同
NSString *str8 = @"csdn";
NSString *str9 = [NSString stringWithString:@"csdn"];
if ([str8 isEqualToString:str9]) {
NSLog(@"the string same");
}
//6)测试字符串是否为同一个对象
if (str8 == str9) {
NSLog(@"str8 == str9!");
}
//7)比较字符串的大小
// 原理: 字符串的每一个字母按照字典中出现的顺序比较,返回一个枚举类型的结果
// 用的是:caseInsensitiveCompare 方法,返回值是:NSComparisonResult
//NSComparisonResult 是一个枚举类型
//假设有两个字符串 分别为:a b
//NSOrderedAscending = -1L, 表示升序 如果 a < b 返回值-1
//NSOrderedSame 相同 如果 a = b 返回值 0
//NSOrderedDescending 降序 如果 a > b 返回值 1
NSString *str10=@"aaddaba";
NSString *str11=@"aaaaa";
//字符串的比较
NSComparisonResult result = [str10 caseInsensitiveCompare:str11];
NSLog(@"result :%ld",result);
//8)求字符串的长度
NSString *str12 = @"asfsadfasdfasdf";
NSLog(@"str12 length = %d",[str12 length]);
//9)字符串的大小写转换的问题
NSString *str13 = @"heLlo woRld!";
NSLog(@"str12 uppercaseString = %@",[str13 uppercaseString]);
NSLog(@"str12 lowercaseString = %@",[str13 lowercaseString]);
NSLog(@"str12 capitalizedString = %@",[str13 capitalizedString]);
//10)字符串和数值型进行转换
NSString *str14 = @"3.1415926";
//转换为int
NSLog(@"str14转int:%d",[str14 intValue]);
//转换为float
NSLog(@"str14转float:%f",[str14 floatValue]);
//转换为double
NSLog(@"str14转double:%lf",[str14 doubleValue]);
//转换为bool类型
NSLog(@"str14转bool:%d",[str14 boolValue]);
//11)字符串的分隔函数,将特定形式的字符串转换为数组
NSString *str15 = @"aaa,bbb,ccc,ddd,eee,ffff";
//思考:NSStrign *str15 = @"aaa,1#bbb,2#ccc,3#ddddd,4";
NSArray *arr = [str15 componentsSeparatedByString:@","];
NSLog(@"数组的第0个元素是:%@", [arr firstObject]);
//12)字符串的截取函数:
NSString *str16 = @"asfasdfasdf";
//从开始位置(0)截取到指定位置(不包含指定位置)
NSLog(@"str16 substring to loc :%@",[str16 substringToIndex:5]);
//从指定位置开始(包含指定位置),截取到字符串的末尾
NSLog(@"str16 substring loc to end :%@",[str16 substringFromIndex:5]);
//13)根据范围(NSRange)来截取字符串
NSRange range = {3,5};
NSLog(@"str16 substring with range :%@",[str16 substringWithRange:range]);
//14)拼接字符串,将新字符串插入到老字符串之后
NSString *str17 = @"abc";
NSString *str18 = @"def";
//使用格式化的字符串进行拼接
NSString *str19 = [str17 stringByAppendingFormat:@"%@",@"昂视方式大发顺丰"];
NSString *str20 = [NSString stringWithFormat:@"%@%@",str17,str18];
NSLog(@"str19 append format : %@",str19);
NSLog(@"str20 stringwithformat : %@",str20);
//使用指定字符串进行拼接
NSLog(@"str19,1tr20 拼接成:%@",[str19 stringByAppendingString:str20]);
//15)查找指定的字符串出现的位置
NSString *str21 = @"dasfdsafasd.giffljsadlfjasla.pngsdfdf";
NSRange range2 = [str21 rangeOfString:@".gif"];
//NSNotFound 最大的整数,如果没有找到,则返回最大的整数
if (range2.location != NSNotFound) {
NSLog(@".png seach result :%@",NSStringFromRange(range2));
}else{
NSLog(@"Not Found!");
}
//思考题:判断字符串是否是一个邮箱
//16)判断字符串的前缀和后缀
NSString *str22 = @"http://www.csdn.net";
//判断前缀
if ([str22 hasPrefix:@"http://"]) {
NSLog(@"网址合法");
}
//判断是否是图片的代码
NSString *str23 = @"aaa.png";
//判断后缀
if ([str23 hasSuffix:@".png"]||[str23 hasSuffix:@".jpg"]||[str23 hasSuffix:@".gif"]) {
NSLog(@"这是一张图片");
}
}
return 0;
}
实例:
[cpp] view
plaincopy
//用arrayWithObjects初始化一个不可变的数组对象。
//初始化的值之间使用逗号分开,以nil结束。
NSArray6 *city = [NSArray arrayWithObjects:@"上海",@"广州",@"重庆",nil];
for(int i=0; i < [city count];i++){
NSLog(@"%@",[city objectAtIndex:i]);
}
运行结果:
上海
广州
重庆
NSArray常用方法:
[cpp] view
plaincopy
+(id)arrayWithObjects:obj1,obj2,...nil //创建一个新的数组,obj1,obj2,.., 以nil结束。
-(BOOL)containsObject:obj //确定数组中是否包含对象obj
-(NSUInterger)count //数组中元素的个数
-(NSUInterger)indexOfObject:obj //第一个包含obj元素的索引号
-(id)ObjectAtIndex:i //存储在位置i的对象
-(void)makeObjectsPerformSelector:(SEL)selector //将selector提示的消息发送给数组中的每一个元素
-(NSArray*)sortedArrayUsingSelector:(SEL)selector //根据selector提定的比较方法对数组进行排序
-(BOOL)writeToFile:path atomically:(BOOL)flag //将数组写入指定的文件中。如果flag为YES,则需要先创建一个临时文件
二、NSMutableArray是动态数组,可以动态增加数组中的元素,同样NSMutableArray是NSArray的子类。
实例:
[cpp] view
plaincopy
//用arrayWithCapacity创建一个长度为5的动态数组
NSMutableArray *nsma = [MSMutableArray arrayWithCapacity:5];
for(int i=0;i<=50;i++) {
if( i%3 == 0 ) {
[nsma addObject:[NSNumber numberWithInteger:i]]; //用addObject给数组nsma增加一个对象
}
}
//输出数组中的元素
for(int i=0;i<[nsma count];i++) {
NSLog(@"%li",(long)[[nsma objectAtIndex] integerValue]);
}
数组排序例子:
student.h
[cpp] view
plaincopy
#import <Foundation/Foundation.h>
@interface Student: NSObject {
NSString: *name;
int age;
}
@property (copy,nonatomic) NSString* name;
@property int age;
-(void)print;
-(NSComparisonResult)compareName:(id)element;
@end
StudentTest.m
[cpp] view
plaincopy
#import <Foundation/Foundation.h>
#import "student.h"
int main( int argc, const char* agrv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Student* stu1 = [[Student alloc] init];
Student* stu2 = [[Student alloc] init];
Student* stu3 = [[Student alloc] init];
[stu1 setName:@"Sam"];
[stu1 setAge:30];
[stu2 setName:@"Lee"];
[stu2 setAge:23];
[stu3 setName:@"Alex"];
[stu3 setAge:26];
NSMutableArray *students = [[NSMutableArray alloc] init];
[students addObject:stu1];
[students addObject:stu2];
[students addObject:stu3];
NSLog(@"排序前:");
for(int i=0; i<[students count];i++) {
Student *stu4 = [students objectAtIndex:i];
NSLog(@"Name:%@,Age:%i",[stu4 name],[stu4 age]);
}
[students sortUsingSelector:@selector(compareName:)];
NSLog(@"排序后:");
for( Student *stu4 in students ) {
NSLog(@"Name:%@,Age:%i", stu4.name, stu4.age);
}
[students release];
[stu1 release];
[sut2 release];
[stu3 release];
[pool drain];
return 0;
}
结果:
[plain] view
plaincopy
排序前:
Name:Sam,Age:30
Name:Lee,Age:23
Name:Alex,Age:26
排序后:
Name:Alex,Age:26
Name:Lee,Age:23
Name:Sam,Age:30
NSMutableArray常用方法:
[cpp] view
plaincopy
+(id)array //创建一个空数组
+(id)arrayWithCapacity:size //创建一个容量为size的数组
-(id)initWithCapacity:size //初始化一个新分配的数,指定容量为size
-(void)addObject:obj //将obj增加到数组中
-(void)insertObject:obj atIndex:i //将obj插入数据的i元素
-(void)replaceObjectAtIndex:i withObject:obj //用obj替换第i个索引的对象
-(void)removeObject:obj //从数组中删除所有是obj的对象
-(void)removeObjectAtIndex:i //从数组中删除索引为i的对象
-(void)sortUsingSelector:(SEL)selector //用selector指示的比较方法进行排序
//
// main.m
// 11-OCFundation框架NSString学习
//
// Created by 谷建鹏 on 14-3-31.
// Copyright (c) 2014年 com.csdn. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
/*******************字符串的创建**************/
//1)创建字符串的常量
NSString *str =@"大家好才是真的好";
NSLog(@"str = %@",str);
//2)创建空字符串
NSString *str2 = [[NSString alloc] init];
NSString *str3 = [NSString string];
NSLog(@"str2 = %@,str3 = %@",str2,str3);
//3)快速创建字符串
NSString *str4 = [NSString stringWithString:@"hello csdn"]; //类方法
NSString *str5 = [[NSString alloc] initWithString:@"初始化字符串"]; //实例方法来实现 初始化
NSLog(@"str4 = %@,str5 = %@",str4,str5);
//4)快速创建一个格式化的字符串
NSString *str6 = [[NSString alloc] initWithFormat:@"pi = %f",3.1415926];
NSString *str7 = [NSString stringWithFormat:@"pi = %f",3.1415926];
NSLog(@"str6 = %@,str7 = %@",str6,str7);
//5)测试字符串是否相同
NSString *str8 = @"csdn";
NSString *str9 = [NSString stringWithString:@"csdn"];
if ([str8 isEqualToString:str9]) {
NSLog(@"the string same");
}
//6)测试字符串是否为同一个对象
if (str8 == str9) {
NSLog(@"str8 == str9!");
}
//7)比较字符串的大小
// 原理: 字符串的每一个字母按照字典中出现的顺序比较,返回一个枚举类型的结果
// 用的是:caseInsensitiveCompare 方法,返回值是:NSComparisonResult
//NSComparisonResult 是一个枚举类型
//假设有两个字符串 分别为:a b
//NSOrderedAscending = -1L, 表示升序 如果 a < b 返回值-1
//NSOrderedSame 相同 如果 a = b 返回值 0
//NSOrderedDescending 降序 如果 a > b 返回值 1
NSString *str10=@"aaddaba";
NSString *str11=@"aaaaa";
//字符串的比较
NSComparisonResult result = [str10 caseInsensitiveCompare:str11];
NSLog(@"result :%ld",result);
//8)求字符串的长度
NSString *str12 = @"asfsadfasdfasdf";
NSLog(@"str12 length = %d",[str12 length]);
//9)字符串的大小写转换的问题
NSString *str13 = @"heLlo woRld!";
NSLog(@"str12 uppercaseString = %@",[str13 uppercaseString]);
NSLog(@"str12 lowercaseString = %@",[str13 lowercaseString]);
NSLog(@"str12 capitalizedString = %@",[str13 capitalizedString]);
//10)字符串和数值型进行转换
NSString *str14 = @"3.1415926";
//转换为int
NSLog(@"str14转int:%d",[str14 intValue]);
//转换为float
NSLog(@"str14转float:%f",[str14 floatValue]);
//转换为double
NSLog(@"str14转double:%lf",[str14 doubleValue]);
//转换为bool类型
NSLog(@"str14转bool:%d",[str14 boolValue]);
//11)字符串的分隔函数,将特定形式的字符串转换为数组
NSString *str15 = @"aaa,bbb,ccc,ddd,eee,ffff";
//思考:NSStrign *str15 = @"aaa,1#bbb,2#ccc,3#ddddd,4";
NSArray *arr = [str15 componentsSeparatedByString:@","];
NSLog(@"数组的第0个元素是:%@", [arr firstObject]);
//12)字符串的截取函数:
NSString *str16 = @"asfasdfasdf";
//从开始位置(0)截取到指定位置(不包含指定位置)
NSLog(@"str16 substring to loc :%@",[str16 substringToIndex:5]);
//从指定位置开始(包含指定位置),截取到字符串的末尾
NSLog(@"str16 substring loc to end :%@",[str16 substringFromIndex:5]);
//13)根据范围(NSRange)来截取字符串
NSRange range = {3,5};
NSLog(@"str16 substring with range :%@",[str16 substringWithRange:range]);
//14)拼接字符串,将新字符串插入到老字符串之后
NSString *str17 = @"abc";
NSString *str18 = @"def";
//使用格式化的字符串进行拼接
NSString *str19 = [str17 stringByAppendingFormat:@"%@",@"昂视方式大发顺丰"];
NSString *str20 = [NSString stringWithFormat:@"%@%@",str17,str18];
NSLog(@"str19 append format : %@",str19);
NSLog(@"str20 stringwithformat : %@",str20);
//使用指定字符串进行拼接
NSLog(@"str19,1tr20 拼接成:%@",[str19 stringByAppendingString:str20]);
//15)查找指定的字符串出现的位置
NSString *str21 = @"dasfdsafasd.giffljsadlfjasla.pngsdfdf";
NSRange range2 = [str21 rangeOfString:@".gif"];
//NSNotFound 最大的整数,如果没有找到,则返回最大的整数
if (range2.location != NSNotFound) {
NSLog(@".png seach result :%@",NSStringFromRange(range2));
}else{
NSLog(@"Not Found!");
}
//思考题:判断字符串是否是一个邮箱
//16)判断字符串的前缀和后缀
NSString *str22 = @"http://www.csdn.net";
//判断前缀
if ([str22 hasPrefix:@"http://"]) {
NSLog(@"网址合法");
}
//判断是否是图片的代码
NSString *str23 = @"aaa.png";
//判断后缀
if ([str23 hasSuffix:@".png"]||[str23 hasSuffix:@".jpg"]||[str23 hasSuffix:@".gif"]) {
NSLog(@"这是一张图片");
}
}
return 0;
}
相关文章推荐
- Objective-C数组 NSArray与NSMutableArray的区别 NSDictionary与 NSMutableDictionary 的区别 == int,NSInteger,NSUIn
- Objective-C语法之NSArray和NSMutableArray
- Objective-C数组 NSArray与NSMutableArray的区别 NSDictionary与 NSMutableDictionary 的区别 == int,NSInteger,NSUIn
- objective-c之NSArray和NSMutableArray区别和转换
- Objective-C 之 NSArray 与 NSMutableArray 的区别
- Objective-C语法之NSArray和NSMutableArray
- objective-c中的NSArray和NSMutableArray
- Objective-C语法之NSArray和NSMutableArray
- Objective-C NSString NSMutableString NSArray NSMutableArray
- Objective-c - NSArray(NSMutableArray)
- 黑马程序员——Objective-C学习之不可变与可变集合(NSArray\NSMutableArray)
- ios开放——Objective-C语法之NSArray和NSMutableArray
- Objective-C语法之NSArray和NSMutableArray
- Objective-C语法之NSArray和NSMutableArray
- Objective-C中的NSArray和NSMutableArray
- Objective-C Copy语法(一)对于Objective-C 提供的类对象NSString/NSMutableString; NSArray/NSMutableArray...
- Objective-C: Foundation——NSArray、NSMutableArray
- Objective-C语法之NSArray和NSMutableArray
- Objective-C数组 NSArray与NSMutableArray的区别 NSDictionary与 NSMutableDictionary 的区别 == int,NSInteger,NSUIn
- Objective-C学习-数组 NSArray 和 NSMutableArray