1.NSArray
1.1 创建方法(5种)
1.2 求数组长度
1.3 根据指定下标获取元素值
1.4 根据元素值求下标
1.5 遍历(2种)
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建方法
NSArray *array1=[NSArray array]; //空数组,没有意思。
NSArray *array2=[NSArray arrayWithObject:@"one"];//只有一个元素的数组
NSArray *array3=[NSArray arrayWithObjects:@"one", @"two",@"three",nil];//有多个元素的数组。
NSLog(@"array3:%@",array3);
NSArray *array4=@[@"one",@"two",@"three"]; //优化后的方法,最常用
NSLog(@"array4:%@",array4);
NSArray *array5=[NSArray arrayWithArray:array4]; //生成数组副本
NSLog(@"%p,%p",array5,array4);
//求数组元素个数
NSUInteger length=array4.count;
NSLog(@"array4的元素个数:%lu",length);
//求数组指定下标对应的元素值
NSString *str=[array4 objectAtIndex:1];
NSLog(@"下标为1的元素:%@",str);
NSLog(@"%@",array4[1]); //优化后的方法,常用
str = [array4 lastObject]; //返回最后一个元素
NSLog(@"最后一个元素:%@",str);
str =[array4 firstObject]; //返回第一个元素
NSLog(@"第一个元素:%@",str);
//根据元素值求其在数组中的下标
NSUInteger index= [array4 indexOfObject:@"two"];
NSLog(@"two的下标:%lu",index);
index=[array4 indexOfObject:@"5"];
NSLog(@"%lu",index);
if(index>=array4.count)
{
NSLog(@"该元素不在数组中");
}
//遍历
for(int i=0;i<array4.count;i++) //面向过程循环遍历
{
NSLog(@"%@",array4[i]);
}
NSLog(@"--------");
for(NSString* str in array4) //面向对象循环遍历
{
NSLog(@"%@",str);
}
NSArray *array6=@[@"onr",@"two",@"three",@"two"];
for(NSString *str in array6)
{
if([str isEqualToString:@"two"]==YES)
NSLog(@"%@",str);
}
}
return 0;
}
运行结果
2016-08-26 10:57:18.782 cz_day21_03[1965:69922] array3:(
one,
two,
three
)
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] array4:(
one,
two,
three
)
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] 0x100200ab0,0x100200250
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] array4的元素个数:3
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] 下标为1的元素:two
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] two
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] 最后一个元素:three
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] 第一个元素:one
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] two的下标:1
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] 9223372036854775807
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] 该元素不在数组中
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] (
one,
two,
three
)
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] one
2016-08-26 10:57:18.783 cz_day21_03[1965:69922] two
2016-08-26 10:57:18.784 cz_day21_03[1965:69922] three
2016-08-26 10:57:18.784 cz_day21_03[1965:69922] --------
2016-08-26 10:57:18.784 cz_day21_03[1965:69922] one
2016-08-26 10:57:18.784 cz_day21_03[1965:69922] two
2016-08-26 10:57:18.784 cz_day21_03[1965:69922] three
2016-08-26 10:57:18.784 cz_day21_03[1965:69922] two
2016-08-26 10:57:18.784 cz_day21_03[1965:69922] two
Program ended with exit code: 0
练习
建一个Student类
.h文件
#import <Foundation/Foundation.h>
@interface CZStudent : NSObject
@property NSString* name;
@property int age;
-(id)initWithName:(NSString*)name andAge:(int)age;
+(id)studentWithName:(NSString*)name andAge:(int)age;
-(void)show;
@end
.m文件
#import "CZStudent.h"
@implementation CZStudent
-(id)initWithName:(NSString*)name andAge:(int)age
{
self = [super init];
if (self) {
self.name=name;
self.age=age;
}
return self;
}
+(id)studentWithName:(NSString *)name andAge:(int)age
{
__autoreleasing CZStudent *stu=[[CZStudent alloc]initWithName:name andAge:age];
return stu;
}
-(void)show
{
NSLog(@"name:%@,age:%d",self.name,self.age);
}
-(NSString*)description //重写方法
{
return [NSString stringWithFormat:@"name:%@,age:%d",self.name,self.age];
}
@end
main函数
#import <Foundation/Foundation.h>
#import "CZStudent.h"
//将基本数据类型的数据10、3.14、‘a’,还有学生类的对象放入一个数组中,并遍历数组
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSNumber *n=[NSNumber numberWithInt:10];
NSNumber *d=[NSNumber numberWithFloat:3.14];
CZStudent*stu=[CZStudent studentWithName:@"xiaoming" andAge:18];
NSArray *array=@[n,d,@"a",stu];
for(id obj in array)
{
NSLog(@"%@",obj);
}
}
return 0;
}
汉字转换汉语拼音
NSMutableString *str=[NSMutableString stringWithString:@"计算机语言"]; //转换的必须是可变字符串
//带音标
if(CFStringTransform((__bridge CFMutableStringRef)str,0, kCFStringTransformMandarinLatin, NO))
{
NSLog(@"%@",str);
}
//不带音标
if(CFStringTransform((__bridge CFMutableStringRef)str, 0,kCFStringTransformStripDiacritics,NO))
{
NSLog(@"%@",str);
}
1.6 排序
建一个Integer类
.h文件
#import <Foundation/Foundation.h>
@interface CZInteger : NSObject
@property int integer;
-(id)initWithInteger:(int)integer;
+(id)integerWithInteger:(int)integer;
-(void)show;
-(NSComparisonResult)compareInteger:(CZInteger*)other; //声明排序规则
@end
.m文件
#import "CZInteger.h"
@implementation CZInteger
-(id)initWithInteger:(int)integer
{
if(self=[super init])
{
self.integer=integer;
}
return self;
}
+(id)integerWithInteger:(int)integer
{
__autoreleasing CZInteger* i=[[CZInteger alloc]initWithInteger:integer];
return i;
}
-(void)show
{
NSLog(@"%d",self.integer);
}
-(NSString*)description
{
return [NSString stringWithFormat:@"%d",self.integer];
}
-(NSComparisonResult)compareInteger:(CZInteger *)other //升序规则,>,<变更一下就变成降序规则
{
if(self.integer<other.integer)
{
return NSOrderedAscending;
}
else if(self.integer>other.integer)
{
return NSOrderedDescending;
}
else
return NSOrderedSame;
}
@end
main函数
#import <Foundation/Foundation.h>
#import "CZInteger.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
CZInteger *i1=[CZInteger integerWithInteger:10];
CZInteger *i2=[CZInteger integerWithInteger:3];
CZInteger *i3=[CZInteger integerWithInteger:8];
CZInteger *i4=[CZInteger integerWithInteger:17];
CZInteger *i5=[CZInteger integerWithInteger:5];
NSArray *array=@[i1,i2,i3,i4,i5];
NSArray *sorted=[array sortedArrayUsingSelector:@selector(compareInteger:)];
NSLog(@"%@",sorted);
}
return 0;
}
运行结果
2016-08-26 14:30:21.719 cz_day21_05[2873:183915] (
3,
5,
8,
10,
17
)
Program ended with exit code: 0
练习
建一个Student类
.h文件
#import <Foundation/Foundation.h>
@interface CZStudent : NSObject
@property NSString* name;
@property int age;
@property NSMutableString *pinYin;
-(id)initWithName:(NSString*)name andAge:(int)age;
+(id)studentWthName:(NSString*)name andAge:(int)age;
-(NSComparisonResult)compareAge:(CZStudent*)other;
-(NSComparisonResult)compareName:(CZStudent *)other;
@end
.m文件
#import "CZStudent.h"
@implementation CZStudent
-(id)initWithName:(NSString*)name andAge:(int)age
{
self = [super init];
if (self) {
self.name=name;
self.pinYin=[NSMutableString stringWithString:self.name];
CFStringTransform((__bridge CFMutableStringRef)self.pinYin, 0, kCFStringTransformMandarinLatin, NO);//将汉字转换成拼音
self.age=age;
}
return self;
}
+(id)studentWthName:(NSString *)name andAge:(int)age
{
__autoreleasing CZStudent* stu=[[CZStudent alloc]initWithName:name andAge:age];
return stu;
}
-(NSString*)description
{
return [NSString stringWithFormat:@"name:%@,age:%d",self.name,self.age];
}
-(NSComparisonResult)compareAge:(CZStudent*)other
{
if(self.age<other.age)
{
return NSOrderedAscending;
}
else if(self.age>other.age)
{
return NSOrderedDescending;
}
else
return NSOrderedSame;
}
-(NSComparisonResult)compareName:(CZStudent *)other
{
return [self.pinYin compare:other.pinYin];
}
@end
main函数
#import <Foundation/Foundation.h>
#import "CZStudent.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
CZStudent* s1=[CZStudent studentWthName:@"张三" andAge:19];
CZStudent* s2=[CZStudent studentWthName:@"李四" andAge:10];
CZStudent* s3=[CZStudent studentWthName:@"王五" andAge:28];
CZStudent* s4=[CZStudent studentWthName:@"赵六" andAge:3];
CZStudent* s5=[CZStudent studentWthName:@"钱七" andAge:22];
NSArray *array=@[s1,s2,s3,s4,s5];
//年龄排序
NSArray *sorted=[array sortedArrayUsingSelector:@selector(compareAge:)];
for(id obj in sorted)
{
NSLog(@"%@",obj);
}
//姓名排序
NSArray *sortedName=[array sortedArrayUsingSelector:@selector(compareName:)];
NSLog(@"---------");
for(id obj in sortedName)
{
NSLog(@"%@",obj);
}
}
return 0;
}
运行结果
016-08-26 15:37:32.846 cz_day21_06[3367:209237] name:赵六,age:3
2016-08-26 15:37:32.847 cz_day21_06[3367:209237] name:李四,age:10
2016-08-26 15:37:32.847 cz_day21_06[3367:209237] name:张三,age:19
2016-08-26 15:37:32.847 cz_day21_06[3367:209237] name:钱七,age:22
2016-08-26 15:37:32.847 cz_day21_06[3367:209237] name:王五,age:28
2016-08-26 15:37:32.847 cz_day21_06[3367:209237] ---------
2016-08-26 15:37:32.847 cz_day21_06[3367:209237] name:李四,age:10
2016-08-26 15:37:32.847 cz_day21_06[3367:209237] name:钱七,age:22
2016-08-26 15:37:32.847 cz_day21_06[3367:209237] name:王五,age:28
2016-08-26 15:37:32.847 cz_day21_06[3367:209237] name:赵六,age:3
2016-08-26 15:37:32.847 cz_day21_06[3367:209237] name:张三,age:19
Program ended with exit code: 0
1.7 副本中的数组元素,深浅拷贝生成方法
#import <Foundation/Foundation.h>
#import "CZStudent.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
CZStudent* s1=[CZStudent studentWthName:@"张三" andAge:18];
CZStudent* s2=[CZStudent studentWthName:@"李四" andAge:20];
CZStudent* s3=[CZStudent studentWthName:@"王五" andAge:19];
NSArray *stu=@[s1,s2,s3];
NSArray *copied=[[NSArray alloc]initWithArray:stu copyItems:NO];//写成NO是引用计数拷贝
NSLog(@"s1:%p,stu[0]:%p,copied[0]%p",s1,stu[0],copied[0]);
//深拷贝
NSArray *copiedDeep=[[NSArray alloc]initWithArray:stu copyItems:YES];
NSLog(@"s1:%p,stu[0]:%p,copiedDeep[0]%p",s1,stu[0],copied[0]);
}
return 0;
}
运行结果
2016-08-26 16:13:53.164 cz_day21_08[3770:220267] s1:0x100700320,stu[0]:0x100700320,copied[0]0x100700320
2016-08-26 16:13:53.165 cz_day21_08[3770:220267] s1:0x100700320,stu[0]:0x100700320,copiedDeep[0]0x100700320
Program ended with exit code: 0
3.NSMutableArray
3.1 可变数组
3.2 创建方法(3种)
3.3 添加方法(2种)
3.4 修改方法(2种)
3.5 删除方法(6种)
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建方法
NSMutableArray *array1=[NSMutableArray array]; //空数组,有意义
NSMutableArray *array2=[NSMutableArray arrayWithCapacity:100]; //预估值
NSMutableArray *array3=@[@"one",@"two"]; //会退化成NSArray
NSMutableArray *array4=[NSMutableArray arrayWithObjects:@"one",@"two",@"three", nil];
NSLog(@"array4:%@",array4);
//添加元素
[array4 addObject:@"four"]; //在最后追加一个元素
NSLog(@"末尾加元素:%@",array4);
[array4 insertObject:@"five" atIndex:1];
NSLog(@"在指定下标加元素:%@",array4);
//修改
[array4 replaceObjectAtIndex:1 withObject:@"aaa"];//修改指定下标元素
NSLog(@"修改指定下标元素:%@",array4);
NSArray *replace =@[@"five",@"six",@"seven",@"eight",@"nine",@"ten"];
[array4 replaceObjectsInRange:NSMakeRange(1,2) withObjectsFromArray:replace];//批量修改
NSLog(@"批量修改:%@",array4);
//删除方法
[array4 removeLastObject]; //删除最后一个元素
NSLog(@"删除最后一个元素:%@",array4);
[array4 removeObjectAtIndex:0]; //指定下标元素删除
NSLog(@"指定下标元素删除:%@",array4);
[array4 removeObject:@"six"];//指定元素删除
NSLog(@"指定元素删除:%@",array4);
[array4 removeObjectsInRange:NSMakeRange(3, 2)]; //指定范围删除
NSLog(@"指定范围删除:%@",array4);
NSArray *del=@[@"seven",@"three"]; //指定批量元素删除
[array4 removeObjectsInArray:del];
NSLog(@"指定批量元素删除:%@",array4);
[array4 removeAllObjects]; //清空数组
NSLog(@"清空数组:%@",array4);
}
return 0;
}
输出结果
2016-08-26 17:02:16.981 cz_day21_09[4164:236372] array4:(
one,
two,
three
)
2016-08-26 17:02:16.982 cz_day21_09[4164:236372] 末尾加元素:(
one,
two,
three,
four
)
2016-08-26 17:02:16.982 cz_day21_09[4164:236372] 在指定下标加元素:(
one,
five,
two,
three,
four
)
2016-08-26 17:02:16.982 cz_day21_09[4164:236372] 修改指定下标元素:(
one,
aaa,
two,
three,
four
)
2016-08-26 17:02:16.982 cz_day21_09[4164:236372] 批量修改:(
one,
five,
six,
seven,
eight,
nine,
ten,
three,
four
)
2016-08-26 17:02:16.982 cz_day21_09[4164:236372] 删除最后一个元素:(
one,
five,
six,
seven,
eight,
nine,
ten,
three
)
2016-08-26 17:02:16.982 cz_day21_09[4164:236372] 指定下标元素删除:(
five,
six,
seven,
eight,
nine,
ten,
three
)
2016-08-26 17:02:16.983 cz_day21_09[4164:236372] 指定元素删除:(
five,
seven,
eight,
nine,
ten,
three
)
2016-08-26 17:02:16.983 cz_day21_09[4164:236372] 指定范围删除:(
five,
seven,
eight,
three
)
2016-08-26 17:02:16.983 cz_day21_09[4164:236372] 指定批量元素删除:(
five,
eight
)
2016-08-26 17:02:16.983 cz_day21_09[4164:236372] 清空数组:(
)
Program ended with exit code: 0
练习
#import <Foundation/Foundation.h>
#import "CZStudent.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
CZStudent* s1=[CZStudent studentWithName:@"s1" andAge:18];
CZStudent* s2=[CZStudent studentWithName:@"s2" andAge:18];
CZStudent* s3=[CZStudent studentWithName:@"s3" andAge:18];
CZStudent* s4=[CZStudent studentWithName:@"s4" andAge:18];
CZStudent* s5=[CZStudent studentWithName:@"s5" andAge:18];
CZStudent* s6=[CZStudent studentWithName:@"s6" andAge:18];
CZStudent* s7=[CZStudent studentWithName:@"s7" andAge:18];
CZStudent* s8=[CZStudent studentWithName:@"s8" andAge:18];
NSMutableArray *Class1=[NSMutableArray arrayWithObjects:s1,s2, nil];
NSMutableArray *Class2=[NSMutableArray arrayWithObjects:s3,s4, nil];
NSMutableArray *Class3=[NSMutableArray arrayWithObjects:s5,s6, nil];
NSMutableArray *Class4=[NSMutableArray arrayWithObjects:s7,s8, nil];
NSMutableArray *College1=[NSMutableArray arrayWithObjects:Class1,Class2, nil];
NSMutableArray *College2=[NSMutableArray arrayWithObjects:Class3,Class4, nil];
NSMutableArray *School=[NSMutableArray arrayWithObjects:College1,College2, nil];
for(id obj in School)
{
NSLog(@"%@",obj);
}
for(id obj1 in School)
for(id obj2 in obj1)
for(id obj3 in obj2)
NSLog(@"%@",obj3);
}
return 0;
}
运行结果
2016-08-26 17:50:44.070 cz_day21_每日一练[4595:250550] (
(
"name:s1,age:18",
"name:s2,age:18"
),
(
"name:s3,age:18",
"name:s4,age:18"
)
)
2016-08-26 17:50:44.071 cz_day21_每日一练[4595:250550] (
(
"name:s5,age:18",
"name:s6,age:18"
),
(
"name:s7,age:18",
"name:s8,age:18"
)
)
2016-08-26 17:50:44.071 cz_day21_每日一练[4595:250550] name:s1,age:18
2016-08-26 17:50:44.071 cz_day21_每日一练[4595:250550] name:s2,age:18
2016-08-26 17:50:44.071 cz_day21_每日一练[4595:250550] name:s3,age:18
2016-08-26 17:50:44.071 cz_day21_每日一练[4595:250550] name:s4,age:18
2016-08-26 17:50:44.071 cz_day21_每日一练[4595:250550] name:s5,age:18
2016-08-26 17:50:44.071 cz_day21_每日一练[4595:250550] name:s6,age:18
2016-08-26 17:50:44.071 cz_day21_每日一练[4595:250550] name:s7,age:18
2016-08-26 17:50:44.071 cz_day21_每日一练[4595:250550] name:s8,age:18
Program ended with exit code: 0