Foundation框架
NSString、NSMutableString、NSNumber(基本数据类型包装类)、NSValue(特殊类型包装类:指针、数组、结构体)、NSArray、NSMutableArray、NSDectionary、NSSet等等。
一、不可变字符串NSString类、可变字符串NSMutableString类(是不可变字符串的子类) 1、字符串对象的创建
<1>常量字符串的对象 :最基本的个格式 NSString *str = @“dshsd”;
<2>通过类方法创建字符串: +(NSString*)stringWithString:(NSString *)
<3>通过实例方法创建: -(NSString*)initWithString:(NSString* )
<4>格式化字符串方式创建: +(NSString*)stringWithFormat:(NSString*……)
<5>通过C语言字符串的创建: +(NSString*)stringWithUTF8String:(char *)
<6>通过文件的方式创建字符串:
-(NSString*)initWithContentsOfFile:(NSString *path) usedEncoding:(NSUInteger ) error:(NSError* )
<7>通过网络数据创建字符串:
+(NSString*)stringWithContentsOfURL: (NSURL* ) encoding:(NSStringEncoding ) error:(NSError* )
2、字符串的搜索
注释:NSRange是结构体,包含两个的数据为:NSUInteger Location(指定字符串的开始位置索引) 、NSUInteger Length(指定字符串的长度)
<1>搜索字符串:
-(NSRange )rangeOfString:(NSString *)
<2>查找字符串是不是以什么前缀开始:
-(BOOL)hasPrefix:(NSString *)
<3>查找字符串是不是以什么后缀开始:
-(BOOL)hasSuffix:(NSString *)
<4>在字符串中截取字串:
-(NSString*)substringWithRange:(NSRange )
或者 -(NSString*)substringFromIndex:(NSUInteger)
3、字符串的比较
<1>判断两个对象是不是同一个对象
- (BOOL)isEqual:(id)object;
或者- (BOOL)isEqualTo:(id)object;
<2>判断字符串是否相等
- (BOOL)isEqualToString:(NSString *)aString;
<3>字符串的比较
-(NSComparisonResult)compare:(NSString*)string;
返回值NSComparisonResult有以下三个: NSOrderedAscending:表示“<”, NSOrderedSame:表示”=", NSOrderedDescending:表示”>" 4、字符串与其他类型的转变
<1>其它类型转换成字符串
+(NSString*)stringWithFormat:foomat…;
<2>字符串转换为整型、实型、布尔型等
- (NSInteger)integerValue
- (int)intValue;
- (CGFloat)doubleValue;
- (double)doubleValue;
- (float)floatValue;
- (BOOL)boolValue
<4>字符串转换为C语言的字符串
-(const void *)UTF8String; 5、可变字符串NSMutableString(增、删、改、查) <1>创建一个固定容量的可变数组 + (id)stringWithCapacity:(NSUInteger)capacity; <2>在索引处插入内容 - (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc; <3>删除内容 - (void)deleteCharactersInRange:(NSRange)range; <4>添加内容(在尾部添加) - (void)appendString:(NSString *)aString; - (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); <5>完全设置成其他的字符串 - (void)setString:(NSString *)aString; <6>替换 - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString; 二、 NSNumber类 (1)将基本的数据类型包装为类类型 即基本数据类型包装类 - (id)initWithChar:(char)value; - (id)initWithUnsignedChar:(unsigned char)value; - (id)initWithShort:(short)value; - (id)initWithUnsignedShort:(unsigned short)value; - (id)initWithInt:(int)value; - (id)initWithUnsignedInt:(unsigned int)value; - (id)initWithLong:(long)value; - (id)initWithUnsignedLong:(unsigned long)value; - (id)initWithLongLong:(long long)value; - (id)initWithUnsignedLongLong:(unsigned long long)value; - (id)initWithFloat:(float)value; - (id)initWithDouble:(double)value; - (id)initWithBool:(BOOL)value; + (NSNumber *)numberWithChar:(char)value; + (NSNumber *)numberWithUnsignedChar:(unsigned char)value; + (NSNumber *)numberWithShort:(short)value; + (NSNumber *)numberWithUnsignedShort:(unsigned short)value; + (NSNumber *)numberWithInt:(int)value; + (NSNumber *)numberWithUnsignedInt:(unsigned int)value; + (NSNumber *)numberWithLong:(long)value; + (NSNumber *)numberWithUnsignedLong:(unsigned long)value; + (NSNumber *)numberWithLongLong:(long long)value; + (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value; + (NSNumber *)numberWithFloat:(float)value; + (NSNumber *)numberWithDouble:(double)value; + (NSNumber *)numberWithBool:(BOOL)value; (2)包装后的类数据按照原类型取出来 - (char)charValue; - (unsigned char)unsignedCharValue; - (short)shortValue; - (unsigned short)unsignedShortValue; - (int)intValue; - (unsigned int)unsignedIntValue; - (long)longValue; - (unsigned long)unsignedLongValue; - (long long)longLongValue; - (unsigned long long)unsignedLongLongValue; - (float)floatValue; - (double)doubleValue; - (BOOL)boolValue; 三、 NSValue类 (1)将特殊基本类型包装为对象(例如结构体、数组、指针) a.//将数组包装为类类型 + (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type; 或者+ (NSValue *)valueWithPointer:(const void *)pointer; //取出数组类型 - (void *)pointerValue; b.//将指针包装为类类型 + (NSValue *)valueWithPointer:(const void *)pointer; //取出指针类型 c.//将结构体包装为类类型(Foundation中常用的结构体:NSRange\NSRect\NSSize\NSPoint) 首先创建结构体:(x、y为坐标原点) NSRange range = NSMakeRange(location,length) NSRect rect = NSMakeRect(x, y, width, height) NSRange size = NSMakeSize(width,height) NSRect point= NSMakePoint(x, y) 将结构体当做参数 + (NSValue *)valueWithRange:(NSRange)range; + (NSValue *)valueWithPoint:(NSPoint)point; + (NSValue *)valueWithSize:(NSSize)size; + (NSValue *)valueWithRect:(NSRect)rect; //取出结构体类型 - (NSPoint)pointValue; - (NSSize)sizeValue; - (NSRect)rectValue; - (NSRange)rangeValue;
程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4737345.html,如需转载请自行联系原作者