//内存管理原则(配对原则):只要出现了new,alloc,retain方法,就要配对出现release,autorelease
//对象存入到自动释放池中,当这个池子被销毁的时候他会对池子中所有的对象进行一次release操作
//怎么把对象存到池子中
autorelease
------------------------------------------------------------------------------------
//当出现类循环应用的时候,只需要把一方面的Strong引用改为weak,并且在.h文件中使用@class 类名的方式,通知.h文件类的存在
//ARC简单,不用程序员在去管理内存
//1.强指针 Strong
//2.弱指针 weak
//只要有强指针指向一个对象,那么系统就不会回收该对象
//只要没有强指针指向对象,系统立即回收该对象
//弱指针不影响,对象被回收
//默认情况下,所有的指针都是强指针类型
// [super dealloc];在ARC机制中 不能够在调用
// releae retain 在ARC机制中不能够在去手动调用
------------------------------------------------------------------------------------
//ARC机制 strong 对应 手动内存管理的retain关键字,(一定能够都是应用在对象类型变量上)
//ARC机制中的 weak 对应 手动内存管理的assign关键字,(一定能够都是应用在对象类型变量上)
@property (nonatomic,strong)Car * car;//强指针类型的对象,会影响对象回收
@property (nonatomic,weak)Car * car2;// 弱指针类型的对象,不会影响对象的回收
//@property (nonatomic,assign)Car * car3;//造成迷惑,会导致许多程序员搞不清这个变量到底是stong类型的还是weak类型
@property (nonatomic,strong)NSString * name;
@property (nonatomic,strong)NSString * idCard;
//ARC机制下 基本数据类型的@property参数使用,与手动内存管理完全一致
@property (nonatomic,assign)int age;
------------------------------------------------------------------------------------
/*
使用继承关系来扩充一个类,有一个弊病,高耦合性
category(分类,类别)
能够帮我们扩充一个类的功能
*/
//只要是在原类中生成的成员变量都可以在分类中直接访问
//如果分类中定义实现了与原类中相同的方法,那么原类中的方法相当于被覆盖掉了
//在实际的开发中,最好不要出现方法覆盖
//不能在分类中生成员变量
//{
// int _weight;
//}
------------------------------------------------------------------------------------
//block 指向函数的指针比较像
//SEL
//数据类型
//1.作为参数传递
//2.作为函数的返回值
//3.声明成变量
函数指针的申明
/*
void (*myPoint)() = someMethod; //(void someMethod(){...})
myPoint();
*/
//block就是弥补了 指向函数的指针,不能够直接保存一个函数体(代码块)
void (^myBlock)() = ^{
NSLog(@"test");
};
myBlock();
int (^sumBlock)(int a, int b) = ^int (int a, int b) {
return a + b;
};
int result = sumBlock(10,20);
NSLog(@"result = %d",result);
代码块作为参数
#import <Foundation/Foundation.h>
typedef int (^calculateBlock)(int a,int b);
@interface Calculate : NSObject
- (int)calculateWithNumber1:(int)number1 andNumber2:(int)number2 andCalculate:(calculateBlock)calculate;
@end
- (int)calculateWithNumber1:(int)number1 andNumber2:(int)number2 andCalculate:(calculateBlock)calculate
{
//经常变化的功能,在设计当中叫做封装变化
return calculate(number1,number2);
}
//如果想要改变,block代码块之外的变量值,就必须在变量前加入
//__block关键字
__block int x = 0;
int (^sumBlock)(int a, int b) = ^int (int a, int b) {
int result = (a * b);
x = result;
return result;
};
//当在开发中,你发现一个方法中的算法,可以有多种实现,你一时还不能确定用哪种更好,你就可以吧方法中其中一个参数定义成block方式
//
Calculate * cal = [[Calculate alloc] init];
int sum = [cal calculateWithNumber1:10 andNumber2:20 andCalculate:sumBlock];
NSLog(@"sum = %d",sum);
NSLog(@"x = %d",x);
------------------------------------------------------------------------------------
//不需要许多的类去遵守的协议,也就是一个类特有的协议我们可以把它直接在这个类的.h文件中直接声明出来
@protocol MiddleStudentProtocol <NSObject>
- (void)skipClass;
@end
//要求你创建的PErson对象必须是遵循了 PersonProtocol
Person<PersonProtocol> * p2 = [[Person alloc] init];
[p2 walk];
------------------------------------------------------------------------------------
/*
SEL数据类型是用来包装方法的
*/
//使用@selector就能够把一个方法包装成 SEL数据类型
Person * p = [[Person alloc] init];
SEL s1 = @selector(eat);
[p performSelector:s1];
SEL s2 = @selector(call:);
[p performSelector:s2 withObject:@"135047654"];
SEL s3 = @selector(findName);
NSString * str = [p performSelector:s3];
NSLog(@"%@",str);
------------------------------------------------------------------------------------
//1.获取字符串的长度
NSString * str = @"abcd";
NSUInteger length = [str length]; //str.length;
NSLog(@"字符串长度 %lu",length);
//2.根据索引获得单个字符
NSString * str2 = @"www.arvon.cn";
//如果你给定的索引值,超出了字符串的长度,就会导致异常Range or index out of bounds
//如果你给入的是一个负数那么获得的值就是一个乱码
unichar c = [str2 characterAtIndex:2];//索引值是从 0 开始的
NSLog(@"获得字符 %c",c);
//3.根据索引获得字符串的一个子串
NSString * str3 = @"www.arvon.com";
//substringFromIndex 从给定的索引开始(包含该索引位置)截取到字符串末尾
NSString * tmpStr = [str3 substringFromIndex:3];
NSLog(@"substringFromIndex %@ ",tmpStr);
//substringToIndex截取字符串到给定索引的位置结束,(不包含该索引位置);qishisuo
NSString * tmpStr2 = [str3 substringToIndex:3];
NSLog(@"substringToIndex %@",tmpStr2);
NSLog(@"str3 %@",str3);
//4.截取字符串的某一段/Volumes/aplle/Desktop/共享课堂/0320/代码/01-NSString类/01-NSString类.xcodeproj
NSRange rang = {4,6};
//location (起始索引的位置,包含该索引) length(要截取的长度)
NSString * tmpStr3 = [str3 substringWithRange:rang];
NSLog(@"substringWithRange %@",tmpStr3);
//5.获得一个字符串的索引位置
NSString * tmpStr4 = @"it";//@"arvon";
NSRange tmpRange = [str3 rangeOfString:tmpStr4];
//可以使用NSStringFromRange 把一个Range结构体转换成字符串
// NSLog(@"location = %d,length = %d",tmpRange.location,tmpRange.length);
//NSNotFound 没有找到
if (tmpRange.location == NSNotFound)
{
NSLog(@"tmpStr4 不在 Str3中");
}else
{
NSLog(@"rangeOfString %@",NSStringFromRange(tmpRange));
}
//6.获的一个字符串索引范围
NSRange tmpRange2 = [str3 rangeOfString:@"arvon" options:NSCaseInsensitiveSearch];
NSLog(@"rangeOfString1 %@",NSStringFromRange(tmpRange2));
------------------------------------------------------------------------------------
//1.判断字符串是否为空
NSString * str = @"";
if (str == nil || str.length == 0)
{
NSLog(@"字符串为空");
}
//2.判断字符串是否已指定的内容开头
NSString * str2 = @"www.arvon.cn";
BOOL isPerfix = [str2 hasPrefix:@"xww"];
NSLog(@"hasPerfix %@",isPerfix ? @"YES" : @"NO" );
//3.判断字符串是否以指定的内容结尾
//在开发中中常用在 判断文件格式
//.txt .avi,.rmvb mkv doc mp3,mp4 pdf
NSString * str3 = @"www.arvon.cn.txtttt";
BOOL isSuffix = [str3 hasSuffix:@".txt"];
NSLog(@"hasSuffix %@",isSuffix ? @"YES" : @"NO" );
//4.判断两个字符串是否相等
//字符串池,{ abc }
NSString * pstr1 = @"abc";
NSString * pstr2 = @"abc";
NSString * pstr3 = pstr1;
NSLog(@"pstr1 %p",pstr1);
NSLog(@"pstr2 %p",pstr2);
if (pstr1 == pstr2)
{
NSLog(@"== 判断 pstr1 与 pstr2相等");
}
if (pstr1 == pstr3)
{
NSLog(@"== 判断 pstr1 与 pstr3相等");
}
NSString * pstr5 = [NSString stringWithFormat:@"%@",@"abcd"];
NSLog(@"pstr5 %@",pstr5);
//使用 ==号 判断两个字符串 实际上是判断的字符串地址是否相同
//如果地址相同的话,两个字符串相等
NSLog(@"pstr5 %p",pstr5);
if (pstr5 == pstr3)
{
NSLog(@"== 判断 pstr5 与 pstr1相等");
}
//在实际的开发中,切记不要使用== 号去判断两个字符串是否相等
//
//判断两个字符串是否相等,必须使用NSString提供给我们的方法
//isEqualToString //abc
//1.判断两个字符串指针地址是否相等,如果相等直接返回yes
//2.取出字符串中的每一个字符进行比较,
BOOL isEqual = [pstr5 isEqualToString:pstr3];
NSLog(@"isEqual %@",isEqual ? @"YES" : @"NO" );
BOOL isEqual2 = [pstr5 myIsEqual:pstr3];
NSLog(@"isEqual2 %@",isEqual2 ? @"YES" : @"NO" );
/*
- (void)setName:(NSString *)name
{
//内存管理set方法中,判断两个字符串是否相等,一定不能够使用 isEqualToString方法
//因为,我们就是要判断两个字符串的指真地址是否相等
if (_name != name)
{
[_name release];
_name = [name reatain];
}
}
*/
//6.compare 是isEqualToString的增强版本
//Ascii值大小 a 小于 b
NSString * strTmp1 = @"abc"; //a 97 b 98
NSString * strTmp2 = @"bbc";
NSComparisonResult result = [strTmp1 compare:strTmp2];
// NSLog(@"result %d",result);
switch (result) {
case NSOrderedAscending:
NSLog(@"NSOrderedAscending");
break;
case NSOrderedSame:
NSLog(@"两个字符串相等");
break;
case NSOrderedDescending:
NSLog(@"NSOrderedDescending");
break;
default:
break;
}
------------------------------------------------------------------------------------
//基本数据类型(int float,double char)
//1.int类型换换成字符串
int a = 10;
NSString * str1 = [NSString stringWithFormat:@"%d",a];
NSLog(@"str1 %@",str1);
//2.float -> NSString
float f = 2.4;
NSString * str3 = [NSString stringWithFormat:@"%.1f",f];
NSLog(@"str3 %@",str3);
//3.double - >NSstring
double d = 65.9898;
NSString * str4 = [NSString stringWithFormat:@"%.4f",d];
NSLog(@"str4 %@",str4);
//4.char - > NSString
char c = ‘c‘;
NSString * str5 = [NSString stringWithFormat:@"%c",c];
NSLog(@"str5 %@",str5);
//5.NSString -> int
NSString * str6 = @"1";
int i1 = [str6 intValue];
NSLog(@"i1 %d",i1);
//5.NSString -> float
NSString * str7 = @"9.8";
float f1 = [str7 floatValue];
NSLog(@"f1 %.1f",f1);
//6.NSString -> double
NSString * str8 = @"8.88";
double d1 = [str8 doubleValue];
NSLog(@"d1 %.2f",d1);
//注意:当你要把字符串转换成某种基本数据类型的时候,一定要确定你要转化你的数据是正确
NSString * str9 = @"i386";
int i2 = [str9 intValue];
NSLog(@"is %d",i2);
//7.将字符串中的字母转换成大写
NSString * str10 = @"china";
NSString * tmpStr1 = [str10 uppercaseString];
NSLog(@"tmpStr1 %@ str10 %@",tmpStr1,str10);
//8.将字符中的字母转换成小写
NSString * str11 = @"CHINA";
NSString * tmpStr2 = [str11 lowercaseString];
NSLog(@"tmpStr2 %@",tmpStr2);
//9.将首字母大写
NSString * str12 = @"china";
NSString * tmpStr3 = [str12 capitalizedString];
NSLog(@"tmpStr3 %@",tmpStr3);
------------------------------------------------------------------------------------
//1.多个数据拼接成一个字符串
int year = 1988;
int month = 4;
int day = 15;
NSString * date = [NSString stringWithFormat:@"%d年%d月%d日",year,month,day];
NSLog(@"date %@",date);
NSString * address = @"昌平区,金燕龙";
NSString * dateAndAddrss = [NSString stringWithFormat:@"%@地址:%@",date,address];
NSLog(@"dateAndAddress %@",dateAndAddrss);
//2.字符串的末尾追加新的字符
NSString * str1 = @"www.";
NSString * str2 = [str1 stringByAppendingString:@"arvon.cn"];
NSLog(@"str2 %@ str1 %@",str2,str1);
//3.在制定的范围追加字符串
NSString * str3 = @"www.cn";
NSRange range = {4,0};
// 4 location代表的时从哪个索引开始插入,length 代表的意思可以覆盖几个字符
NSString * str4 = [str3 stringByReplacingCharactersInRange:range withString:@"arvon."];
NSLog(@"str4 %@",str4);
//4.使用新的字符,替换原有的字符
NSString * str5 = @"www.arvon.cn";
NSString * str6 = [str5 stringByReplacingOccurrencesOfString:@"w" withString:@"a"];
NSLog(@"str6 %@",str6);
//5.在字符串中删除特定的某些字符 胡汉三 hhs
NSString * str7 = @"www---.itcatst.--cn";
NSString * str8 = [str7 stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"str8 %@",str8);
//6.去掉字符串中的空格
NSString * str9 = @"www .itcat .cn";
NSString * str10 = [str9 stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"str10 %@",str10);
------------------------------------------------------------------------------------
//将字符串写入到文件
NSString * content = @"浙江冰雹,如汤圆大小,习近平万岁";
NSString * path = @"/Users/aplle/Desktop/共享课堂/mytest.txt";
//1.参数1 要给入文件目录(并且要包括文件名称)
//2.atomically : YES
//3.NSUTF8StringEncoding 国际通用的编码格式
//4.NSError 是用来记录错误信息的
NSError * error;
BOOL isWriteSuccess = [content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
if(isWriteSuccess)
{
NSLog(@"文件创建成功");
}
else
{
NSLog(@"error %@",error);
}
------------------------------------------------------------------------------------
//要给出文件路径
//你写入文件的时候,使用的是什么编码格式,读取的时候就要给我用什么编码格式
NSString * path = @"/Users/aplle/Desktop/共享课堂/mytest.txt";
NSError * error;
NSString * str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error != nil)
{
NSLog(@"error message %@",error);
}
else
{
NSLog(@"str content %@",str);
}
------------------------------------------------------------------------------------
//1.创建可变字符串
//NSMutableString 继承自 NSString
//NSMutableString 用油NSString所有的属性以及方法
// NSMutableString * muStr = [[NSMutableString alloc] init];
NSMutableString * muStr = [[NSMutableString alloc] initWithCapacity:0];
//是初始化字符串内容
[muStr setString:@"www"];
NSLog(@"mustr %p",muStr);
//动态的给字符串末尾追加新值
[muStr appendString:@".cn"];
//在指定的索引位置,插入字符串
[muStr insertString:@".arvon" atIndex:3];
//删除指定范围的字符串
//你要删除的长度,一定不能够超出字符串的的长度,导致异常Range or index out of bounds
NSRange range = {3,7};
// [muStr deleteCharactersInRange:range];
[muStr replaceCharactersInRange:range withString:@"传智播客"];
NSLog(@"muStr %@",muStr);
------------------------------------------------------------------------------------
//1.创建一个NSRange结构体 表示范围
NSRange range = {4,5};
NSRange range2 = NSMakeRange(4, 5);
NSString * strRange = NSStringFromRange(range);
NSLog(@"strRange %@",strRange);
//2.NSSize 等同于 CGSize 有两个值组成 weight表示宽度 height高度
NSSize size = {21,19};
NSSize size2 = NSMakeSize(21, 19);
NSString *strSize = NSStringFromSize(size2);
NSLog(@"strSize %@",strSize);
//3.NSPoint 等同CGPoint 表示一个点的坐标位置
NSPoint point = {20,40};
NSPoint point2 = NSMakePoint(20, 40);
NSString * strPoint = NSStringFromPoint(point);
NSLog(@"strPoint %@",strPoint);
//4.NSRect
NSRect rect = {40,60,100,200};
NSRect rect2 = NSMakeRect(0, 10, 100, 100);
NSString * strRect = NSStringFromRect(rect);
NSLog(@"strRect %@",strRect);
------------------------------------------------------------------------------------
//集合中,不能存储基本数据类型,如果你想要把基本数据类型存储到数组,集合中,就必须把它转换成基本数据类型的包装类
@autoreleasepool {
//int float double char
int a = 10;
// [NSString stringWithFormat:@"%d",a];
//1.使用NSNumber 包装 int数据类型
NSNumber * number1 = [NSNumber numberWithInt:a];
NSLog(@"%@",number1);
//2.直接转换成字符串
NSString * strInt = [number1 stringValue];
NSLog(@"strInt %@",strInt);
//3.float包装成NSNumber
NSNumber * number2 = [NSNumber numberWithFloat:98.98];
NSLog(@"number2 %@",[number2 stringValue]);
//4.包装类转换成基本数据类型
int b = [number1 intValue];
NSLog(@"%d",b);
------------------------------------------------------------------------------------
//1.创建集合
//NSArray 是不可变数组,一旦创建完成就不能够对数组进行,添加,删除等操作
NSArray * array = [[NSArray alloc] init];
NSLog(@"%@",array);
//2.通过构造方法的方式创建一个NSArray
//在创建一个NSArray的时候,集合的最后一个元素一定是nil
NSArray * array1 = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
NSLog(@"%@",array1);
//3.数组中可以存储不同类型的对象
NSNumber * number = [NSNumber numberWithInt:10];
NSArray * array2 = [[NSArray alloc] initWithObjects:@"one",@"two",number, nil];
NSLog(@"array2 %@",array2);
//4.数组实际上存储的时对象的地址,同样也可以存储数组的地址
NSArray * a1 = [[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
NSArray * a2 = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
NSArray * a3 = [[NSArray alloc] initWithObjects:a1,a2, nil];
NSLog(@"a3 %@",a3);
//5.存储自定义的对象
//最好都给我重写 description方法
Person * p1 = [[Person alloc] initWithName:@"xiaozhe" andAge:20];
Person * p2 = [[Person alloc] initWithName:@"hell" andAge:18];
Person * p3 = [[Person alloc] initWithName:@"marray" andAge:38];
NSArray * array3 = [[NSArray alloc] initWithObjects:p1,p2,p3, nil];
NSLog(@"array3 %@",array3);
//6.数组中存储基本数据类型,
//如果你要在数组中存储基本数据类型,请给我包装好了之后在去存 NSNumber
//注意不要把nil值存储到NSArray中,会导致数据丢失
NSString * str = nil;
NSArray * array4 = [[NSArray alloc] initWithObjects:@"2",str,[NSNumber numberWithInt:23], nil];
NSLog(@"array4 %@",array4);
//7.创建数组的快捷方式
NSArray * karray = @[@"a",@"b",@"c"];
NSLog(@"karray %@",karray);
//8.快速获得一个数组中的元素
NSString * kstr = karray[0];
NSLog(@"kstr %@",kstr);
//9.从集合中取出数据
NSArray * array = [[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
//数组的index时从0开始的
NSString * str = [array objectAtIndex:0];
NSLog(@"str %@",str);
//10.获得数组的元素个数
NSUInteger arrayCount = [array count];
NSLog(@"arrayCount %d",arrayCount);
//判断数组中是否存在某个对象
Person * p1 = [[Person alloc] initWithName:@"xiaozhe" andAge:20];
Person * p2 = [[Person alloc] initWithName:@"nihao" andAge:30];
NSArray * array = @[p1,p2];
BOOL isContain = [array containsObject:p1];
------------------------------------------------------------------------------------
//1.使用for循环遍历数组
NSArray * array = @[@"one",@"two",@"three"];
for (int i = 0; i < array.count; i++)
{
NSString * str = array[i];
NSLog(@"array[%d] = %@",i,str);
}
//1.数据类型
//BOOL id NSObject * (NSString * NSArray *) block SEL
//2.流程控制语句
// 增强for循环
/*
for (NSObject * obj in array)
{
}
*/
//你的array.count值是多少,他就会循环多少次,每一次都取出数据中的一个元素
//存储到str中
//2.使用增强for循环的方式遍历数组
for (NSString * str in array)
{
NSLog(@"str = %@",str);
}
//3.注意事项
//如果你的数组中村粗了多种不同类型的数据,那么最好不要调用某个对象特有的方法,会导致程序崩溃
//在实际的开发中,一个数组往往只负责存储一种数据类型
Person * p1 = [[Person alloc] init];
NSArray * array2 = @[@"one",[NSNumber numberWithInt:10],p1];
for (NSString * str in array2)
{
// NSLog(@"array2 str %@",str);
// NSLog(@"str %ld",str.length);
}
//3.枚举器
NSArray * array3 = @[@"one",@"two",@"three"];
NSEnumerator * enumerateor = [array3 objectEnumerator];
NSString * value;
while (value = [enumerateor nextObject]) {
NSLog(@"enum str %@",value);
}
------------------------------------------------------------------------------------
//1.使用sortedArrayUsingSelector
//也是最简单的排序方式
//数组是按照你存入元素的顺序存储的
NSArray * array = @[@"b",@"d",@"a",@"z"];
NSLog(@"排序前 array %@",array);
array = [array sortedArrayUsingSelector:@selector(compare:)];
// NSArray * array1 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"排序后 array %@",array);
//2.使用block方式排序
NSArray * array2 = @[@"z",@"4",@"b",@"3",@"x"];
NSLog(@"array2 排序前 %@",array2);
array2 = [array2 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
/*
NSComparisonResult retuslt = [obj1 compare:obj2];
return retuslt;
*/
//obj1 compare obj2 就是正序排序
// return [obj1 compare:obj2];
//obj2 compare obj1 就是倒序排序
return [obj2 compare:obj1];
}];
NSLog(@"array2 排序后 %@",array2);
Person * p1 = [[Person alloc] initWithName:@"xiaozhe" andAge:20 andYear:@"1990"];
Person * p2 = [[Person alloc] initWithName:@"alex" andAge:18 andYear:@"2990"];
Person * p3 = [[Person alloc] initWithName:@"merry" andAge:25 andYear:@"1890"];
NSArray * array3 = @[p1,p2,p3];
NSLog(@"array3 排序前 %@",array3);
//3.使用 给数组排序
//如果你向给你自己定义的对象排序,必须根据某一个属性来排序,
//sortDescriptorWithKey 参数要的就是你对象中,要依据哪个属性来排序,你就把哪个属性的名字当成key传入
//ascending YES表示正序 NO表示倒叙
NSSortDescriptor * d1 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
NSSortDescriptor * d2 = [NSSortDescriptor sortDescriptorWithKey:@"year" ascending:NO];
//如果你要使用多个属性进行排序,默认在前面的NSSortDescriptor优先级比较高
NSArray * descripts = @[d2,d1];
array3 = [array3 sortedArrayUsingDescriptors:descripts];
NSLog(@"array 3 排序后 %@",array3);
//4.
NSArray * array4 = @[p1,p2,p3];
NSLog(@"array4 排序前 %@",array4);
array4 = [array4 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
Person * p1 = obj1;
Person * p2 = obj2;
//year
return [p1.year compare:p2.year];
}];
NSLog(@"array4 排序后 %@",array4);
------------------------------------------------------------------------------------
//NSMutableArray 继承至 NSArray
// NSArray * array = @[];
//1.创建可变数组
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:0];
//使用addObject 动态给数组中增加元素
/*
[array addObject:@"one"];
[array addObject:@"two"];
[array addObject:@"three"];
[array addObject:@"one"];
*/
NSString * str1 = @"one";
NSString * str2 = @"two";
NSString * str3 = @"three";
//数组中可以存储,同一个对象多次
[array addObject:str1];
[array addObject:str2];
[array addObject:str3];
[array addObject:str1];
//2.指定对象插入的位置
[array insertObject:str1 atIndex:2];
//3.删除 会通过对象,删除数组中所有的同一个地址的对象
[array removeObject:str1];
//4.通过索引的方式删除对象,超出了数组的count值,那么就会导致异常 index beyond bounds
[array removeObjectAtIndex:0];
[array addObject:str2];
[array addObject:str3];
[array addObject:str1];
//5.删除数组中所有的元素
[array removeAllObjects];
NSLog(@"array %@",array);
[array addObject:str2];
[array addObject:str3];
[array addObject:str1];
//1.for换遍历
for (int i = 0; i < array.count; i++)
{
NSString * str = [array objectAtIndex:i];
//在遍历数组的时候,千万不要给数组中,增加,或者删除元素
// [array removeObject:str1];
NSLog(@"str %@",str);
}
//2.增强for循环
for (NSString * str in array)
{
// [array removeObject:str1];
//如果你在增强for循环中,删除元素,会直接导致异常出现
NSLog(@"str %@",str);
}
//3.枚举器
NSEnumerator * enumerator = [array objectEnumerator];
NSString * value;
while (value = [enumerator nextObject]) {
//如果你在枚举器中,删除元素,会直接导致异常出现
// [array removeObject:str1];
NSLog(@"str %@",value);
}
//4.要通过遍历的方式确定删除哪个元素怎么办
NSMutableArray * array2 = [[NSMutableArray alloc] init];
[array2 addObject:@"1"];
[array2 addObject:@"2"];
[array2 addObject:@"3"];
[array2 addObject:@"4"];
[array2 addObject:@"5"];
NSMutableArray * tmp = [[NSMutableArray alloc] init];
for (NSString * str in array2)
{
if ([str isEqualToString:@"3"])
{
[tmp addObject:str];
}
}
NSLog(@"array2 %@",array2);
NSLog(@"tmp %@",tmp);
//遍历临时数组
for (int i = 0; i < tmp.count; i++)
{
NSString * str = [tmp objectAtIndex:i];
//从原数组中删除,临时数组中存储的对象
[array2 removeObject:str];
}
NSLog(@"array2 %@",array2);
------------------------------------------------------------------------------------
//1.创建字段,不可变的
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"xiaozhe",@"name", nil];
NSLog(@"dic %@",dic);
//2.快捷创建方式
NSDictionary * dic2 = @{ @"one":@"1",@"two":@"2"};
NSLog(@"dic2 %@",dic2);
//3.字典中可以存任意数据类型
// 字典的顺序不是自然顺序
NSArray * array = @[@"one",@"two"];
NSDictionary * dic3 = @{
@"one":@"1",
@"num":[NSNumber numberWithInt:10],
@"aaa":dic2,
@"bbb":dic,
@"ar1":array
};
NSLog(@"dic3 %@",dic3);
//4.获得字典的长度
NSLog(@"count %ld",dic3.count);
//5.从字典中取值
NSString * str = [dic3 objectForKey:@"one"];
NSLog(@"str %@",str);
NSDictionary * dicTmp = [dic3 objectForKey:@"aaa"];
NSLog(@"dicTmp %@",dicTmp);
NSArray * arrayTmp = [dic3 objectForKey:@"ar1"];
NSLog(@"arrayTmp %@",arrayTmp);
//6.遍历
//取出所有的key值
NSArray * allkeys = [dic3 allKeys];
NSLog(@"allkeys %@",allkeys);
for (int i = 0; i < allkeys.count; i++)
{
NSString * key = [allkeys objectAtIndex:i];
//如果你的字典中存储的多种不同的类型,那么最好用id类型去接受它
id obj = [dic3 objectForKey:key];
NSLog(@"obj %@",obj);
}
//2.枚举器
NSEnumerator * enumerator = [dic3 objectEnumerator];
id value;
while (value = [enumerator nextObject]) {
NSLog(@"value %@",value);
}
------------------------------------------------------------------------------------
//1.创建一个可变长度字典
NSMutableDictionary * muDic = [[NSMutableDictionary alloc] initWithCapacity:0];
//2.向字典中村如数据
[muDic setObject:@"1" forKey:@"one"];
[muDic setObject:@"2" forKey:@"two"];
[muDic setObject:@"3" forKey:@"three"];
NSLog(@"muDic %@",muDic);
//3.删除
[muDic removeObjectForKey:@"one"];
NSLog(@"muDic %@",muDic);
//4.全部删除
[muDic removeAllObjects];
NSLog(@"muDic %@",muDic);
Student * stu1 = [[Student alloc] initWithName:@"xiaoher" andAge:20];
Student * stu2 = [[Student alloc] initWithName:@"alex" andAge:50];
Student * stu3 = [[Student alloc] initWithName:@"xiaoli" andAge:10];
[muDic setObject:stu1 forKey:@"s1"];
[muDic setObject:stu2 forKey:@"s2"];
[muDic setObject:stu3 forKey:@"s3"];
//在向字典中存储数据的时候,一定要保证key值是唯一的
// [muDic setObject:stu3 forKey:@"s3"];
// [muDic setObject:stu3 forKey:@"s3"];
// [muDic setObject:stu3 forKey:@"s3"];
NSLog(@"muDic %@",muDic);
//1.使用for循环遍历字典
NSArray * allkeys = [muDic allKeys];
for (int i = 0; i < allkeys.count; i++)
{
NSString * key = [allkeys objectAtIndex:i];
Student * stu = [muDic objectForKey:key];
NSLog(@"stu %@",stu);
};
//2.使用枚举器
NSEnumerator * enumerator = [muDic objectEnumerator];
Student * tmp;
while (tmp = [enumerator nextObject]) {
NSLog(@"tmp %@",tmp);
}
------------------------------------------------------------------------------------
//NSArray 自然顺序
//NSSet是无序的
//注意:这个是最为重要的功能 NSSet中不能够存储重复的数据,可以用它来去除重复的值
NSString * str1 = @"one";
NSString * str2 = @"two";
NSString * str3 = @"three";
NSSet * set = [[NSSet alloc] initWithObjects:str1,str2,str3,str1, nil];
NSLog(@"set %@",set);
//count
NSLog(@"count %ld",set.count);
BOOL isContains = [set containsObject:str1];
if (isContains)
{
NSLog(@"YES");
}
else
{
NSLog(@"NO");
}
//4.遍历
NSEnumerator * enumerator = [set objectEnumerator];
NSString * value;
while (value = [enumerator nextObject]) {
NSLog(@"value %@",value);
}
------------------------------------------------------------------------------------
NSString * str1 = @"one";
NSString * str2 = @"two";
//1 创建一个可变集合
NSMutableSet * muSet = [[NSMutableSet alloc] init];
//2.增加值
[muSet addObject:str1];
[muSet addObject:str2];
NSLog(@"muSet %@",muSet);
//3.删除
[muSet removeObject:str1];
NSLog(@"muSet %@",muSet);
//4.删除所有
// [muSet removeAllObjects];
NSLog(@"muSet %@",muSet);
//5.遍历
NSEnumerator * en = [muSet objectEnumerator];
NSString * value;
while (value = [en nextObject]) {
NSLog(@"value %@",value);
}
------------------------------------------------------------------------------------
//1.NSArray 转换成 NSMutableArray
NSArray * array = @[@"one",@"two",@"three"];
NSMutableArray * muArray = [NSMutableArray arrayWithArray:array];
NSLog(@"muarray %@",muArray);
//2.NSDictonary 转换成 NSMutableDictionary
NSDictionary * dic = @{@"one":@"1",@"two":@"2"};
NSMutableDictionary * muDic = [NSMutableDictionary dictionaryWithDictionary:dic];
NSLog(@"mudic %@ ",muDic);
//3.NSset 转换成 NSMutableSet
NSSet * set = [[NSSet alloc] initWithObjects:@"one",@"two", nil];
NSMutableSet *muSet = [NSMutableSet setWithSet:set];
NSLog(@"muSet %@",muSet);
//4.NSArray 转换成NSSet
NSMutableSet * muSet2 = [NSMutableSet setWithArray:array];
NSLog(@"muSet2 %@",muSet2);
//5.NSDictionary 转化成NSArray
NSArray * allkeys = [dic allKeys];
NSLog(@"allkeys %@",allkeys);
NSArray * allValues = [dic allValues];
NSLog(@"allValues %@",allValues);
//6.字符串转换成数组
NSString * str = @"www.itacast.cn";
NSArray * strArray = [str componentsSeparatedByString:@"."];
NSLog(@"strArray %@",strArray);
------------------------------------------------------------------------------------
// retainCount 1
Person * p = [[Person alloc] init];
//1
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:0];
//当你把对象存入到,数组中的时候,数组会对这个对象进行一次 retain操作
[array addObject:p];// [p retain] retaiCount 2
//当你把一个对象移除数组中的时候,会对该对象进行一次 release操作 retainCount 1
// [array removeObject:p];
//会对数组中所有的对象,进行一次,relsease操作
// [array removeAllObjects];
//当集合对象被销毁的时候,会对其内部所有的对象进行一次 release操作
//0
[array release];
//retainCount - 0
//0
[p release];
NSLog(@"ddddd");
------------------------------------------------------------------------------------
Person * p = [[Person alloc] init];
NSMutableArray * array = [[NSMutableArray alloc] init];
//当一个对象被存入集合中的时候,默认会保存它的强指针
[array addObject:p];
//当个把一个对象从集合中,删除的时候,会释放掉这个对象的强指针
// [array removeObject:p];
// [array removeAllObjects];
array = nil;
p = nil;
NSLog(@"adfasdf");
------------------------------------------------------------------------------------
//1.文件管理器
NSFileManager * f1 = [NSFileManager defaultManager];
NSLog(@"%p",f1);
NSFileManager * f2 = [NSFileManager defaultManager];
NSLog(@"%p",f2);
NSFileManager * f3 = [[NSFileManager alloc] init];
NSLog(@"f3 %p",f3);
NSFileManager * f4 = [NSFileManager defaultManager];
NSLog(@"%p",f4);
//在整个程序没有退出之前,只需要创建一次,我们就可以把它写成单利模式
//简单的单例模式
User * u1 = [User defaultUser];
u1.age = 10;
NSLog(@"u1 %p",u1);
User * u2 = [User defaultUser];
NSLog(@"u2 %p age = %d",u2,u2.age);
User * u3 = [[User alloc] init];
NSLog(@"u3 %p",u3);
//1.组合模式,(聚合模式)
//2.单利模式
//3.策略模式(封装变化点),block
//4.向上抽象 (protocol(协议))
//5.享元设计模式(NSString池原理)
------------------------------------------------------------------------------------
/*
NSString * str = @"www.arvon.cn";
NSError * error;
BOOL isWriteSuccess = [str writeToFile:@"/Users/aplle/Desktop/test.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (isWriteSuccess)
{
NSLog(@"创建成功");
}
else
{
NSLog(@"error %@",error);
}
*/
//1.获取文件管理器
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/test.txt";
NSError * error;
NSDictionary * dic = [fileManager attributesOfItemAtPath:path error:&error];
NSLog(@"dic %@",dic);
if (error == nil)
{
NSDate * date = [dic objectForKey:NSFileCreationDate];
NSString * size = [dic objectForKey:NSFileSize];
NSLog(@" date = %@, size = %@",date,size);
}
------------------------------------------------------------------------------------
/*
//1.
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/装机";
NSError * error;
//只能够火的目标目录下,第一级目录
NSArray * array = [fileManager contentsOfDirectoryAtPath:path error:&error];
NSLog(@"array %@",array);
*/
//2
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/other";
NSError * error;
//能够逐级的获得所有子集的目录
NSArray * array = [fileManager subpathsOfDirectoryAtPath:path error:&error];
NSLog(@"array %@",array);
------------------------------------------------------------------------------------
/*
//1创建目录
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/myfolder/aaa";
NSError * error;
//withIntermediateDirectories YES 逐级创建文件夹,NO表示只能够创建一级目录
BOOL isCreateSuccess = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
if (isCreateSuccess)
{
NSLog(@"创建成功");
}
else
{
NSLog(@"error %@",error);
}
*/
//2 移动目录 移动就是剪切操作
/*
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/myfolder";
NSString * pathTo = @"/Users/aplle/Desktop/newmyfolder";
NSError * error ;
BOOL isMoveSuccess = [fileManager moveItemAtPath:path toPath:pathTo error:&error];
if (isMoveSuccess)
{
NSLog(@"移动成功");
}
else
{
NSLog(@"error %@",error);
}
*/
//3.删除
/*
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/newmyfolder";
NSError * error;
BOOL isRemoveSuccess = [fileManager removeItemAtPath:path error:&error];
if (isRemoveSuccess)
{
NSLog(@"删除成功");
}
else
{
NSLog(@"erro %@",error);
}
*/
//4.拷贝文件
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/myfolder";
NSString * pathTo = @"/Users/aplle/Desktop/newmyfolder";
NSError * error;
BOOL isCopySuccess = [fileManager copyItemAtPath:path toPath:pathTo error:&error];
if(isCopySuccess)
{
NSLog(@"拷贝成功");
}
else
{
NSLog(@"error %@",error);
}
------------------------------------------------------------------------------------
//1.获得文件
//mp3.mp4 rmvb mkv pdf doc txt
/*
NSString * path = @"/Users/aplle/Desktop/屏幕快照 .png";
NSData * data = [NSData dataWithContentsOfFile:path];
NSLog(@"%ld",data.length);
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * pathTo = @"/Users/aplle/Desktop/屏幕快照副本.png";
BOOL isCreateSuccess = [fileManager createFileAtPath:pathTo contents:data attributes:nil];
if (isCreateSuccess)
{
NSLog(@"创建成功");
}
else
{
NSLog(@"创建失败");
}
*/
/*
//2.移动文件 相当于剪切操作
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/屏幕快照 .png";
NSString * pathTo = @"/Users/aplle/Desktop/newmyfolder/屏幕屏幕.png";
NSError * error;
BOOL ismoveSuccess = [fileManager moveItemAtPath:path toPath:pathTo error:&error];
if (ismoveSuccess)
{
NSLog(@"移动成功");
}
else
{
NSLog(@"error %@",error);
}
*/
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * path = @"/Users/aplle/Desktop/屏幕快照副本.png";
NSError * error;
BOOL isremove = [fileManager removeItemAtPath:path error:&error];
if (isremove)
{
NSLog(@"删除成功");
}
else
{
NSLog(@"error %@",error);
}
------------------------------------------------------------------------------------
//音频文件,文本文件,视频文件就必须使用NSData数据类
/*
NSString * str = @"www.arvon.cn";
NSError * error;
[str writeToFile:@"/Users/aplle/Desktop/text.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
*/
/*
NSString * path = @"/Users/aplle/Desktop/text.txt";
//把目标文件加载到内存中.
//NSData 是不可变长度的Data类型,只能够通过一次加载完成所有的数据
NSData * data = [NSData dataWithContentsOfFile:path];
NSLog(@"data length %ld",data.length);
NSString * pathTo = @"/Users/aplle/Desktop/nextext.txt";
//把加载到内存中的文件,写入到指定的位置
BOOL isWriteSuccess = [data writeToFile:pathTo atomically:YES];
if (isWriteSuccess)
{
NSLog(@"写入成功");
}
else
{
NSLog(@"写入失败");
}
*/
//当需要把多个NSData数据拼接成一个数据存储的时候,就要想到使用NSMutableData这个类型
NSString * str1 = @"好好学习";
NSString * str2 = @"天天向上";
NSString * str3 = @"今天休息";
NSMutableData * muData = [[NSMutableData alloc] init];
NSData * d1 = [str1 dataUsingEncoding:NSUTF8StringEncoding];
NSData * d2 = [str2 dataUsingEncoding:NSUTF8StringEncoding];
NSData * d3 = [str3 dataUsingEncoding:NSUTF8StringEncoding];
//appendData 能够把nsdata对象加入到 muData对象中
[muData appendData:d1];
[muData appendData:d2];
[muData appendData:d3];
NSString * path = @"/Users/aplle/Desktop/传智播客校训.txt";
//NSMutableData是继承至NSData的所以可以调用writeToFile 把数据写入到一个指定的目录下
BOOL iswriteSucess = [muData writeToFile:path atomically:YES];
if (iswriteSucess)
{
NSLog(@"创建成功");
}
else
{
NSLog(@"创建失败");
}
------------------------------------------------------------------------------------
NSTimeInterval secondes = 24 * 60 * 60;
NSDate * now = [NSDate date];
//模拟昨天 24 * 60 * 60
NSDate * yesDay = [now addTimeInterval:-secondes];
//比较两个日期是否相同
BOOL isEqual = [now isEqualToDate:yesDay];
if (isEqual)
{
NSLog(@"相同");
}
else
{
NSLog(@"不相同");
}
//获得两个日期中比较早的一个
NSDate * earler = [yesDay earlierDate:now];
NSLog(@"%@",earler);
NSDate * later = [yesDay laterDate:now];
NSLog(@"later %@",later);
------------------------------------------------------------------------------------
/*格式化日期操作
NSDate * date = [NSDate date];
//表示格式化日期对象
NSDateFormatter * f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd hh:mm:ss"];//2014/03/21
//把格式与我们的日期关联起来
NSString * str = [f stringFromDate:date];
NSLog(@"str %@",str);
*/
//把字符串转换成日期
/*
NSString * str = @"2014/03/20";
NSDateFormatter * f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy/MM/dd"];
NSDate * date = [f dateFromString:str];
NSLog(@"date %@",date);
*/
//处理时区问题
NSDate * newDate = [NSDate date];
NSDateFormatter * f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString * str = [f stringFromDate:newDate];
//设置时差计算方法 //GTM
[f setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSLog(@"%@",str);
------------------------------------------------------------------------------------
//获得日期对象
NSDate * date = [NSDate date];
NSLog(@"date %@",date);
------------------------------------------------------------------------------------
1.IBAction:
1> 能保证方法可以连线
2> 相当于void
2.IBOutlet:
1> 能保证属性可以连线
3.常见错误
setValue:forUndefinedKey:]: this class is not key value coding
错误原因是:连线出问题了
4.Xcode5开始的一些建议
把用于连线的一些方法和属性声明在.m文件的类扩展中
5.frame\center\bounds
1> frame:能修改位置和尺寸
2> center:能修改位置
3> bounds:能修改尺寸(x\y一般都是0)
------------------------------------------------------------------------------------