Foundation框架的几个基础类

Foundation框架里面的几个基础类

NSString & NSMutableString:

    NSString:不可变字符串类。

        常用方法:

                    - (unichar)characterAtIndex:(NSUInteger)index;返回index位置对应的字符

                    - (int)intValue;字符串转为基本数据类型

                    - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement;

                    - (NSString *)substringFromIndex:(NSUInteger)from;从指定位置from开始(包括指定位置的字符)到尾部

                    - (NSString *)substringToIndex:(NSUInteger)to;从字符串的开头一直截取到指定的位置to,但不包括该位置的字符

                    - (NSString *)substringWithRange:(NSRange)range;按照所给出的NSRange从字符串中截取子串

                    -(NSArray *)componentsSeparatedByString:(NSString *)separator;将一个字符串按指定的分隔符分隔成多个子串保存在数组中

    NSMutableString:继承自NSString,可变字符串类。

        常用方法:(方法一般都没有返回值,而是直接在原有字符串所占的内存空间中执行增删改查,不会新分配内存空间)

                      通过调用string方法, 创建一个空的NSMutableString

                    - (void)appendString:(NSString *)aString;//拼接aString到最后面

                    - (void)appendFormat:(NSString *)format, ...;//拼接一段格式化字符串到最后面

                    - (void)deleteCharactersInRange:(NSRange)range;//删除某一个范围的字符串

                    - (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc; //在loc这个位置中插入aString

                    - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString; 使用aString替换range范围内的字符串

        注意:如果将一个字符串常量赋值给NSMutableString类型的变量,该变量的值也是不可变的。

NSArray & NSMutableArray:将一个对象添加到数组中时,对象的retainCount会加1;当一个对象从数组中移除是retainCount会减1;当数组销毁时数组中的所有对

                                                    象的retainCount也都会减1

    NSArray:不可变数组,只能存放对象,有序

        常见用法:

                - (NSUInteger)count; 获取集合元素个数 

                - (id)objectAtIndex:(NSUInteger)index; 获得index位置的元素

                - (BOOL)containsObject:(id)anObject;是否包含某一个元素

                - (id)lastObject; 返回最后一个元素 

                - (id)firstObject; 返回最后一个元素 

                - (NSUInteger)indexOfObject:(id)anObject; 查找anObject元素在数组中的位置(如果找不到,返回-1)NSNotFound 

                - (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range; 在range范围内查找anObject元素在数组中的位置

                - (NSString *)componentsJoinedByString:(NSString *)separator; 这是NSArray的方法, 用separator作拼接符将数组元素拼接成一个字符串

                    

        简写形式:

                定义一个数组:

                    NSArray *array = @[@1,@2,@3];

                访问一个数组元素:

                    array[index];

    NSMutableArray:继承自NSArray,可变数组,只能存放对象

        常见用法:

                - (void)addObjectsFromArdObray:(NSArray *)array; 添加otherArray的全部元素到当前数组中 

                - (void)insertObject:(id)anObject atIndex:(NSUInteger)index; 在index位置插入一个元素

                - (void)removeLastObject; 删除最后一个元素 

                - (void)removeAllObjects; 删除所有的元素

                - (void)removeObjectAtIndex:(NSUInteger)index; 删除index位置的元素 

                - (void)removeObject:(id)object; 删除特定的元素 

                - (void)removeObjectsInRange:(NSRange)range; 删除range范围内的所有元素 

                - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject; 用anObject替换index位置对应的元素

                - (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2; 交换idx1和idx2位置的元素

        注意:

                NSMutableArray *array = @[@"bob", @"steve", @"john"];//这种方式创建的是不可变的数组

NSDictionary & NSMutableDictionary:

    NSDictionary :不可变字典,存放键值对,键和值都必须是对象,无序

        常见用法:

                - (id)objectForKey:(id)aKey; 根据key取出value

                - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;将字典保存在文件中

        简写形式:

                    NSDictionary *dictionary = @{@"zs" : @"zhangsan", @"ls" : @"lisi", @"ww" : @"Wangwu"}; //创建一个字典

                    dictionary [@"zs"] = @"zhangsan"; //获取字典的值

    NSMutableDictionary:可变字典

        常见用法:

                - (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey; 添加一个键值对(会把aKey之前对应的值给替换掉) 

                - (void)removeObjectForKey:(id)aKey;通过aKey删除对应的value

                - (void)removeAllObjects;删除所有的键值对

NSRange:

    表示一个范围的结构体类,包含两个成员变量,一个location一个length

     定义:

            typedef struct _NSRange {

                NSUInteger location;

                NSUInteger length;

            } NSRange;

        赋值:

                NSRange range = {5,8};

NSPoint & NSSize & NSRict:

    NSPoint:也是一个结构体类类,表示一个点,包含两个成员变量x和y

        定义:

                struct CGPoint {

                      CGFloat x;

                      CGFloat y;

                };

     NSSize :还是一个结构体类,表示一个矩形区域,包含两个成员变量width和hight

        定义:

                struct CGSize {

                    CGFloat width;

                    CGFloat height;

                };

    NSRict:又是一个结构体类,表示一个指定的矩形区域,包含两个成员变量NSPoint对象和NSSize对象 

        定义:

                struct {

                    NSPoint origin;

                    NSSize size;

                };

NSValue & NSNumber:

    NSValue:将结构体类型的数据分装成一个对象,方便存放在数组中

        常用方法:

                    + (NSValue *)valueWithPoint:(NSPoint)point;//将NSPoint类型的机构体包装成NSValue对象

                    - (NSPoint)pointValue;//取出包装前的NSPoint结构体的值

    NSNumber:将基本数据类型分装成一个NSNumber类型的对象方便存储在数组中。

        定义:当要转换的基本数据类型是一个常量时用:@常量值  分装成对象;当要转换的基本数据类型是一个变量时用:@(变量名)  分装成对象.

                NSNumber *num = @10;

                NSNumber *num = @10.2;

                int a = 10;

                NSNumber *num = @(a);

NSDate:

    用法:1)  2)  3)

            //1)定义NSDate

            NSDate *d1 = [NSDate date];

            //2)定义日期时间格式化的类

            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

            formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";//HH : 24小时制;hh : 12小时制;Z : 时区

            //3)把Date转换为dataStr

            NSString *dateStr = [formatter stringFromDate:d1];

 

原文地址:https://www.cnblogs.com/CoderCLsun/p/5838137.html