foundation学习




一、NSString字符串的使用

1.基本用法

NSString对象,一旦创建,就不可以进行再改变,如需要一个可以更改的字符串对象,需要创建NSString的可变子类NSMutableString实例。

2.字符串的创建

2.1创建一个字符串常量:NSString*string=@hello world;

2.2创建一个空字符串:

NSString*string=[[NSStringalloc]init];

NSString*string=[NSStringstring];

3.快速创建字符串

NSString*string=[[NSStringalloc]initWithString:@“字符串”];

NSString*string=[NSStringstringWithString:@”字符串”];

4.创建格式字符串

initWithFormat,stringWithFormat方法

5.小注意

常量没有引用计数,常量创建的字符串无需要管理内存

6.比较两个字符串

6.1==”表示比较两个字符串的地址是否相同;

eg:if(string1==string2){//code...

}

6.2isEqualToString”表示比较两个字符串的值是否相同;

Eg:if(string1isEqualToString:string2){//code....

}

6.3通过类方法创建字符串(不需要管理内存的)

NSString*string=[NSStringstringWithCString:abcencoding:

NSUTF8StringEncoding];

6.4通过实例方法创建字符串(需要手动管理内存)

NSString*string=[[NSStringalloc]initWithFormat:@””];

6.5通过追加的方式创建字符串stringByAppendingString方法

6.6NSComparisonResultre=[string1compare:string2];//按字母排序进行比较

7截取字符串

7.1通过类方法:substringFormIndex:下标//从指定的位置截取到末尾

7.2substringToIndex:下标//从开始位置截取到指定的下标位置

7.3截取指定位置(NSRange(loation,length)

Eg:NSRangerg={4,5};//表示从4的位置开始,截取5个长度

等价于NSRangerg=NSMakeRange{4,5};

//调用截取方法[string1substringWithRange:rg];

rangeofstring//得到字符串的位置

8.大小写之间的转换

8.1--->lowercaseString方法

8.2----->uppercaseString方法

9.判断字符串

9.1判断字符串的开头是否与指定的字符串相同hasPrefix方法

9.2判断字符串结尾是否与指定字符串相同hasSuffix方法

9.3忽略大小写进行比较caseInsensitiveCompare

if([str1caseInsensitiveCompare:str2]==NSOrferedsame){//

}

10.分割字符串

10.1通过指定的字符串进行分割componentsSeparatedByString

NSArray*array=[string1componentsSeparateByString:@z指定的字符串”]

10.2通过指定的字符集进行分割(空格、换行)

NSArray*array=[string1componentsSeparatedByCharactersInset:NSCharacterSetEhitespaceAndNewlineCharacterSet]];

10.3通过指定的字符将数组组合成字符串

NSString*str=[arraycomponentsJoinedByString:@**;

11.替换字符串

11.1替换指点字符串

NSString*str=[str1StringByReplacingOccurrenxesOfString:@"要替换的字符串"withstring:@"替换后的字符串"];

11.2替换指点位置的字符串

NSRangemyRange=[strrangeOfString:@"字符串"];

if(myRange.laction==NSNotFound){

//code.....

}else

{

NSString*str=[String1StringByReplacingcharactersInRange:myRangewithstring:@"替换字符串"];

}

12.数据转换

NSString*str=@"100";

intintNum=[strintValue];

FloatfloatNum=[strfloatValue];

13.可变字符串NSMutableString

13.1.如何创建一个可变字符串

NSMutableString*str=[[NSMutableStringalloc]initWithCapacity:0];

13.2.重新设置setString

13.3根据下标插入字符串insetString:@""atIndex:下标值

13.4删除指定位置的字符串

NSRangege=NSMakeRange(4,5);

[string1deleteCharactersInRange:ge];

二、数组(NSArray

1.通过类方法定义数组

(无需管理内存),nil表示数组的结束

arrayWithObject:......,nil

2.通过初始化方法定义数组

initWithArray(通过数组创建数组)

initWithObject(通过对象创建数组)

3.如何操作数组

3.1根据下标获取数组的元素

objectAtIndex:下标

3.2通过元素获取对应的下标

indexOfObjects:@"字符",返回NSInteger

3.3获取最后一个元素:lastObject

3.4获取数组的长度:count

4.循环操作数组

4.1通过枚举

NSEnumerator*enu=[arry1objectEnumerator];

idobj;

While((obj==[enunextObject]))

{

//code.......

}

4.2循环for

4.3forin

for(NSString*strinarray)

{

//code.....

}

4.4反转枚举reverseObjectEnumerator

4.5排序

sortedArrayUsingSelector:@"selector(compare)"

5.可变数组(NSMutableArray

5.1添加元素(末尾)addObject:

5.2插入元素

[array1insertObject:@""atIndex:下标]

5.3移除元素

根据下标移除元素:removeObjectIndex:

移除最后一个元素:removeLastObject

5.4替换元素(根据下标替换)

replaceObjectAtIndex:下标withObject:@"替换的字符串"

通过下标交换:exchangeObjectAtIndex:jwithObjectAtIndex:j+1;

5.5查找

containsObject:@"要查找的字符";

三、字典(NSDictionary

1.如何创建字典?

NSMutableDictionary*dictionary=[NSMutableDictionarydictionaryWithCapacity:个数];

dictionaryWithObjectsAndKey将元素添加到字典中

2.如何操作字典?

2.1通过键得值objectForKey

2.2设置字典(设置没有的键是添加,反之修改)

setObject:@""forKey:@""

2.3移除指定键值数据removeObjectForKey

移除所有的数据removeAllObjects

2.4得到字典的个数count

2.5循环字典

2.5.1通过枚举keyEnumerator/ObjectEnumertor

2.5.2通过数组NSArray*array=[dicallkeys];/allValues

根据键获取值:objecForKey

四、NSDateNSDateFomatter

1.NSDate日期

NSDate*date=[NSDatedate];//得到的是美国时间

通过dateByAddingTimeInterval:8*60*60;得到北京时间

timeIntervalsinceNow//得到两个日期的时间间隔(秒)

[date1earlierDate:date2]//比较两个日期返回较早的日期

[date2lateDate:date2]//返回较晚的日期

2.NSDateFomatter日期格式器

//创建一个日期格式器

NSDateFomatter*fmt=[[NSDateFormatteralloc]init];

//设置时间格式

[fmtsetDateFormat:@"yyyy-MM-ddHH:mm:ss"];

//设置日期的样式

[fmtsetDateStryle:NSDateFormattermediumstyle];

//设置时间的样式

[fmtsetTimeStyle:NSDateFormattershortstyle]

//将日期对象转换为字符串

NSString*dt=[fmtStringFromDate:date];

//将字符串转换为日期对象dateFromString

五、NSNumber

OC的数字类型(对C数据类型的一个封装)

1.C语言--->OC

NSNumber×intNum=[NSNumbernumberWithInt:100];

2.OC--->C

intnum=[intNumintValue];

C语言的int型存放到数组中的话,运行时将出错的,内存错误,找不到C语言的内存。

3.判断一个字符串是否是由数字组成

NSString*str=@"10030";

//用于存字符串扫描指点字符

NSScanner*scan=[NSScannerscannerWithString:str1];

intval;

BOOLfg=[scanscanInt:&val]&&[scannisAtEnd];

六、NSNull/nil

NSNull*nl=[NSNullnull];

在数组中要赋空值时,运用NSNull对象,而nil表示结束。

NULLnil就是0nil是一个通用对象值,而NUll是一个通用指针。

七、NSData字节类

1.创建path

2.将文件读入data对象initWithContentsofFile:path;

3.data对象转换为字符串initWithData:data;

4.将字符串转换为data[stringdataUsingEncoding:];

八、NSTimer定时器

NSTimer*timer=[NSTimerscheduledTimeWithTimeInterval:1.0target:selfselector:@selector(方法)userInfonilrepeatsYES]

//参数的说明:TimeInterval时间间隔的秒数

target:操作的目标

selector:定时器执行的操作

userInfo:参数

repeats:是否重复

//停止[timerinvalidate];

//销毁定时器


优质内容筛选与推荐>>
1、SSH端口转发
2、算法训练想法
3、虚幻4属性系统(反射)翻译
4、bzoj 2007: [Noi2010]海拔【最小割+dijskstra】
5、Java 反射


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号