oc语言的Foundation框架(学习笔记1)

Foundation框架

1.Foundation框架介绍

框架是由许多类、方法、函数以及文档按照一定的规则组合的起来的集合。

cocoa程序编写主要用到2个框架Foundation和Application(UIKit)。Foundation主要定义的是一些基础类,供程序员使用,Application Kit是一些用户界面设计的类,用于Mac开发使用。Foundation框架所有类都继承自NSObject对象。 

Foundation(通用的面相对象的函数库)提供字符串,数值的管理,容器及其枚举,以及一些其他的与图形用户界面没有直接关系的功能。其中用于类和常数的“NS”前缀来自于Cocoa的来源,NextSTEP。可以在Mac OS X和iOS中使用。

                 

2.NSObject常用方法

2.1 介绍:一切类的根类(基类),无父类。所有oc对象都必须直接或者间接的继承NSObject类。

2.2 常用方法

(1)比较两个对象是否为同一个对象(指针是否指向同一个内存区域)

- (BOOL)isEqual:(id)object;

(2)调用一个方法

- (id)performSelector:(SEL)aSelector;

(3)调用一个方法并传递1个参数

- (id)performSelector:(SEL)aSelector withObject:(id)object;

(4)调用一个方法并传递2个参数

- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2; 

 (5)某一个对象是否派生或属于某一类

- (BOOL)isKindOfClass:(Class)aClass;

 (6)某一个对象是否属于某类

 - (BOOL)isMemberOfClass:(Class)aClass;

 (7)某对象是否响应指定的方法

 - (BOOL)respondsToSelector:(SEL)aSelector;

 (8)返回指定对象的父类和本类

 -(Class)superclass; -(Class)class;

3. 字符串的基本概念和常用处理方法

3.1 字符串基本概念

在Foundation框架中,提供NSString类用于处理字符串对象。oc语言是建立在c语言基础上的,故为区别两者字符串,oc语言的字符串必须以@开头,引号中则是字符串的内容,如@"abc"。而且NSString对象一旦被创建不可以再修改,如需修改字符串对象,则需创建NSMutableString实例。

3.2字符串的创建

1).创建一个字符串常量  

NSString *string=@"一个字符串常量";

2).创建一个空的字符串  

NSString *string=[[NSString alloc] init];  或者  NSString *string=[NSString string];

3).快速创建一个字符串  

NSString *string=[[NSString alloc] initWithString:@"快速创建字符串"];  或者     

NSString *string=[NSString stringWithString:@"快速创建字符串"];(创建的是字符串常量,不推荐使用)

4).快速创建一个格式化字符串  

int number=2;  NSString *string=[[NSString alloc] initWithFormat:@"%d",number];

float number=2.5  NSString *string=[NSString stringWithFormat:@"浮点数%f",number];

3.3 比较字符串

1)测试字符串内容是否相同

NSString *string1=[[NSString alloc] initWithFormat:@"test"];

NSString *string2=[[NSString alloc] initWithFormat:@"test"];

if([string1 isEqualToString:string2]){NSLog(@"测试连个字符串是否相等");}

2)测试字符串内容是否相同

NSString *string1=[[NSString alloc] initWithFormat:@"test"];

NSString *string2=[[NSString alloc] initWithFormat:@"test"];

if([string1 == string2]){NSLog(@"测试连个字符串是否为同一个对象");}

3)比较字符串的先后顺序

NSString *string1=[[NSString alloc] initWithFormat:@"a"];

NSString *string2=[[NSString alloc] initWithFormat:@"b"];

NSLog(@"[string1 caseInsensitiveCompare:string2]:%ld",[string1 caseInsenstiveCompare:string2]);

4)求字符串长度

NSString *string=[[NSString alloc] initWithFormat:@"string length"];

NSUInteger *length=[string length];

3.4字符串的转换

1)改变字符大小写

NSString *hello=@"hello WORLD";

NSLog(@"%@",[hello uppercaseString]);  //全部大写

NSLog(@"%@",[hello lowercaseString]);  //全部小写

NSLog(@"%@",[hello capitalizedString]);  //首字母大写,其他均小写

2)将字符串转换成基本数据类型

NSString *string=@"3.68";

NSLog(@"%d",[string boolValue]);  //转换成BOOL类型

NSLog(@"%f",[string floatValue]);  //转换成浮点型

NSLog(@"%f",[string doubleValue]);  //转换成双精度型

NSLog(@"%d",[string intValue]);  //转换成整型

3)将字符串转换成数组

NSString *string=@"one two three four";

NSArray *array=[string componentsSeparatedByString:@""];

3.5 字符串的截取于拼接

1)截取字符串

NSString *string=[NSString stringWithFormat:@"asdfdf"];

NSString *string1=[string substringToIndex:2];//从字符开头一直截取到指定位置,不包括选中位置字符

NSString *string2=[string substringFromIndex:2];//以指定位置开始(包括指定位置字符),并包括之后的全部字符

2)根据范围截取字符串

NSRange rang;

rang.location=2;

rang.length=3;

NSString *string3==[string substringWithRange:rang];

3)拼接字符串

NSString *str1=@"1",*str2=@"2";

NSString *string=[[NSString alloc] initWithFormat:@"拼接:%@ and %@",str1,str2];

NSString *string1=[str1 stringByAppendingFormat:@"%@",str2];

NSString *string2=[str1 stringByAppendingString:str2];

3.6 查询、比较字符串对象

1.查询字符串

NSString *link=@"khoihsdfnovi";

NSRange range=[link rangeOfString:@"sdfnovi"];

if(range.location!=NSNotFound){NSLog(@"找到了");}

2.比较字符串

NSString *content=@"Hello";

NSComparisonResult result1=[content compare:@"hello"];

NSComparisonResult result2=[content compare:@"hello" options:NSLiterakSearch];//区分大小写

NSComparisonResult result3=[content compare:@"hello" options:NSCaseInsensitiveSearch  range:NSMakeRange(0,5)];//不区分大小写

3.7可变字符串

1)插入字符串

NSMutableString *str=[NSMutableString stringWithString :@"字符串"];

[str insertString:@"可变" atIndex:0];

2)删除字符串

NSMutableString *str=[NSMutableString stringWithString :@"字符符串"];

[str deleteCharactersInRange:NSMakeRange(1,2)];

3)替换字符串

NSMutableString *str=[NSMutableString stringWithString :@"字符串"];

[str replaceCharactersInRange:NSMakeRange(0,2)withString:@"猪肉"];

集合、数字对象常用处理方法和日期的常用处理方法以及异常处理内容由下一章笔记给出

原文地址:https://www.cnblogs.com/lexyg/p/7120003.html