在網(wǎng)上看到的,感覺可以膀捷,轉(zhuǎn)過來整理了一下迈嘹。
NSNumber
+(NSNumber *)numberWithInt:(int)value;
+(NSNumber *)numberWithDouble:(double)value;
-(int)intValue;
-(double)doubleValue;
NSNumber可以將基本數(shù)據(jù)類型包裝起來,形成一個(gè)對象全庸,這樣就可以給其發(fā)送消息秀仲,裝入NSArray中等等。
NSNumber * intNumber=[NSNumber numberWithInt:100]壶笼;
NSNumber *floatNumber=[NSNUmber
numberWithFloat:100.00];
int i=[intNumber intValue];
if([intNumber isEqualToNumber:floatNumber]) ....
NSNumber繼承NSObject 神僵,可以使用比較 compare: isEqual等消息
NSString
一個(gè)NSString對象可以存儲一段Unicode字符。在cocoa中覆劈,所有和字符保礼、字符串相關(guān)的處理都是使用NSString來完成沛励。
NSObject -> NSString // NSString繼承自NSObject
+(id) stringWithContentsOfFile:path encoding:enc error:err;
+(id) stringWithContentsOfURL:url encoding:enc error:err;
+(id) stringWithString:nsstring; //創(chuàng)建一個(gè)新的字符串,并將其設(shè)置為nsstring
-(id)initWithFormat:(NSString *)format, ... ;
-(id)initWithString:nsstring; //將分配的字符串設(shè)置為nsstring
-(BOOL)isEqualToString:(NSString *)string;
-(BOOL)hasPrefix:(NSString *)string;
-(int)intValue;
-(double)doubleValue;
-(NSString *)stringByAppendingString:(NSString *)string; // 給一個(gè)字符串附加一個(gè)字符串string炮障。
-(NSString *)stringByAppendingFormat:(NSString *)string;
-(NSString *)stringByDeletingPathComponent;
創(chuàng)建字符串的方法
1目派、創(chuàng)建常量字符串
NSString *astring = @"This is a String!";
2、先創(chuàng)建一個(gè)空的字符串胁赢,然后賦值企蹭;
alloc和init組合則適合在函數(shù)之間傳遞參數(shù),用完之后需要手工release
NSString *astring = [[NSString alloc] init];
astring = @"This is a String!";
NSLog(@"astring:%@",astring);
[astring release];
3智末、在以上方法中谅摄,提升速度:initWithString方法
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
[astring release];
4、創(chuàng)建臨時(shí)字符串
NSString *astring;
astring = [NSString stringWithCString:"This is a temporary string"];
NSLog(@"astring:%@",astring);
OR
NSString * scriptString = [NSString stringWithString:@" tell application \"Mail\"\r"];
5吹害、創(chuàng)建格式化字符串:占位符(由一個(gè)%加一個(gè)字符組成)
int i = 1;
int j = 2;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];
NSLog(@"astring:%@",astring);
[astring release];
從文件讀取字符串
NSString *path = @"astring.text";
NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"astring:%@",astring);
[astring release];
寫字符串到文件
NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
NSLog(@"astring:%@",astring);
NSString *path = @"astring.text";
[astring writeToFile: path atomically: YES];
[astring release];
比較兩個(gè)字符串
1螟凭、用C比較:strcmp函數(shù)
char string1[] = "string!";
char string2[] = "string!";
if(strcmp(string1, string2) = = 0)
{
NSLog(@"1");
}
2、isEqualToString方法
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);
3它呀、compare方法(comparer返回的三種值:NSOrderedSame,NSOrderedAscending棒厘,NSOrderedDescending)
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedSame; //NSOrderedSame 判斷兩者內(nèi)容是否相同
NSLog(@"result:%d",result);
NSString *astring01 = @"This is a String!";
NSString *astring02 = @"this is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;
NSLog(@"result:%d",result);
//NSOrderedAscending 判斷兩對象值的大小(按字母順序進(jìn)行比較纵穿,astring02大于astring01為真)
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;
NSLog(@"result:%d",result);
//NSOrderedDescending 判斷兩對象值的大小(按字母順序進(jìn)行比較,astring02小于astring01為真)
4奢人、不考慮大小寫比較字符串1
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;
NSLog(@"result:%d",result);
//NSOrderedDescending判斷兩對象值的大小(按字母順序進(jìn)行比較谓媒,astring02小于astring01為真)
5、不考慮大小寫比較字符串2
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 compare:astring02
options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;
NSLog(@"result:%d",result);
//NSCaseInsensitiveSearch:不區(qū)分大小寫比較 NSLiteralSearch:進(jìn)行完全比較何乎,區(qū)分大小寫 NSNumericSearch:比較字符串的字符個(gè)數(shù)句惯,而不是字符值。
改變字符串的大小寫
NSString *string1 = @"A String";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//uppercaseString返回轉(zhuǎn)換為大寫的字符串
NSLog(@"string2:%@",[string2 lowercaseString]);//lowercaseString返回轉(zhuǎn)換為小寫的字符串
NSLog(@"string2:%@",[string2 capitalizedString]);//capitalizedString返回每個(gè)單詞首字母大寫的字符串
在串中搜索子串
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
[astring release];
抽取子串
1支救、-substringToIndex: 從字符串的開頭一直截取到指定的位置抢野,但不包括該位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
2、-substringFromIndex: 以指定位置開始(包括指定位置的字符)各墨,并包括之后的全部字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);
3指孤、-substringWithRange: //按照所給出的位置,長度贬堵,任意地從字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
NSLog(@"string2:%@",string2);
4恃轩、快速枚舉
for(NSString *filename in direnum) {
if([[filename pathExtension] isEqualToString:@"jpg"]){
[files addObject:filename];
}
}
NSLog(@"files:%@",files);
5、枚舉
NSEnumerator *filenum;
filenum = [files objectEnumerator];
while (filename = [filenum nextObject]) {
NSLog(@"filename:%@",filename);
}
@"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];
NSLog(@"oldArray:%@",oldArray);
NSEnumerator *enumerator;
enumerator = [oldArray objectEnumerator];
id obj;
while(obj = [enumerator nextObject]) {
[newArray addObject: obj];
}
[newArray sortUsingSelector:@selector(compare:)];
NSLog(@"newArray:%@", newArray);
[newArray release];
切分?jǐn)?shù)組
1黎做、從字符串分割到數(shù)組- componentsSeparatedByString:
NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];
NSLog(@"string:%@",string);
NSArray *array = [string componentsSeparatedByString:@","];
NSLog(@"array:%@",array);
[string release];
2叉跛、從數(shù)組合并元素到字符串- componentsJoinedByString:
NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
NSString *string = [array componentsJoinedByString:@","];
NSLog(@"string:%@",string);
從目錄搜索擴(kuò)展名為jpg的文件
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *home;
home = @"../Users/";
NSDirectoryEnumerator *direnum;
direnum = [fileManager enumeratorAtPath: home];
NSMutableArray *files = [[NSMutableArray alloc] init];
枚舉
NSString *filename;
while (filename = [direnum nextObject]) {
if([[filename pathExtension] hasSuffix:@"jpg"]){
[files addObject:filename];
}
}
擴(kuò)展路徑
NSString *Path = @"~/NSData.txt";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
文件擴(kuò)展名
NSString *Path = @"~/NSData.txt";
NSLog(@"Extension:%@",[Path pathExtension]);
查找與替換
-(NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement
-(NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
NSMutableString(可修改的字符串)
NSObject -> NSString -> NSMutableString
Common NSMutableString methods
+(id)string;
-(void)appendString:(NSString *)string;
-(void)appendFormat:(NSString *)format, ...;
給字符串分配容量
//stringWithCapacity:
NSMutableString *String;
String = [NSMutableString stringWithCapacity:40];
在已有字符串后面添加字符
//appendString: and appendFormat:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
//[String1 appendString:@", I will be adding some character"];
[String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];
NSLog(@"String1:%@",String1);
在已有字符串中按照所給出范圍刪除字符
//deleteCharactersInRange:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)]; //刪除指定范圍(location=0,length=5)的字符串
NSLog(@"String1:%@",String1);
在已有字符串后面在所指定的位置中插入給出的字符串
//-insertString: atIndex:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];
NSLog(@"String1:%@",String1);
[String1 insertString:@"and StringEnd", atIndex:[String1 length]];//在可變字符串的最后插入
將已有的空符串換成其它的字符串
//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);
查找
NSRange subRange = [String1 rangeOfString:@"is a"];
// 如果沒查找到蒸殿,則 (subRange.location == NSNotFound)為真筷厘。
按照所給出的范圍替換的原有的字符
//-setString:
NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"]; // 用于NSMutableString
NSLog(@"String1:%@",String1);
在給定的范圍內(nèi)查找并替換
-(NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)opts range:(NSRange)searchRange
判斷字符串內(nèi)是否還包含別的字符串(前綴挽铁,后綴)
1、檢查字符串是否以另一個(gè)字符串開頭- (BOOL) hasPrefix: (NSString *) aString;
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
2敞掘、查找字符串某處是否包含給定的字符串 - (NSRange) rangeOfString: (NSString *) aString叽掘,這一點(diǎn)前面在串中搜索子串用到過
NSRange subRange;
subRange = [string1 rangeOfString:@"string A"]; //查找字符串 string1中是否包含“string A”。返回NSRange類型玖雁。
if(subRange.location == NSNotFound)
NSLog(@"String not found ");
else NSLog(@"string is at index %lu, length is %lu", subRange.location, subRange.length);
NSDate
NSCalendarDate
- NSCalendarDate對象包含了日期和時(shí)間更扁、時(shí)區(qū)以及一個(gè)帶有格式的字符串,它從NSDate繼承而來赫冬。
- NSCalendarDate對象是immutable的浓镜,一旦被創(chuàng)建,無法修改其中的時(shí)間和日期劲厌,當(dāng)然可以修改那個(gè)帶格式的字符串和時(shí)區(qū)膛薛。
以下是常用方法:
+(id)calendarDate; //創(chuàng)建當(dāng)前日期和時(shí)間以及默認(rèn)格式的NSCalendarDate對象,時(shí)區(qū)為機(jī)器設(shè)置好的時(shí)區(qū)补鼻。
+(id)dateWithYear:(int)year
month:(unsigned)month
day:(unsigned)day
hour:(unsigned)hour
minute:(unsigned)minute
second:(unsigned)second
timeZone:(NSTimeZone *)aTimeZone
-(int)dayOfCommonEra; //得到從公元1年算起哄啄,有多少天
-(int)dayOfMonth; //返回是月的第幾天(1-31)
-(int)dayOfWeek; //返回是周的第幾天 (0-6)
-(int)dayOfYear; //返回是年的第幾天(1-366)
-(int)hourOfDay; // 返回是日的第幾個(gè)小時(shí)(0-23)
-(void)setCalendarFormate:(NSString *)format
創(chuàng)建NSCalendarDate對象
NSCalendarDate *now;
now = [NSCalendarDate calendarDate];
NSTimeZone *pacific = [NSTimeZone timeZoneWithName:@"PST"];
NSCalendarDate *hotTime = [NSCalendarDate dateWithYear:2011 month:2 day:3 hour:14 minute:0 second:0 timeZone:pacific];
NSData
使用文件時(shí),需要頻繁地將數(shù)據(jù)讀入一個(gè)臨時(shí)存儲區(qū)风范,它通常成為緩沖區(qū)咨跌。
- NSData類提供了一種簡單的方式,它用來設(shè)置緩沖區(qū)硼婿、將文件的內(nèi)容讀入緩沖區(qū)锌半,或?qū)⒕彌_區(qū)的內(nèi)容寫到一個(gè)文件。
- 對于32位應(yīng)用程序寇漫,NSDATA緩存區(qū)最多可以存儲2GB的數(shù)據(jù)刊殉。
我們既可定義不變緩沖區(qū)(NSData類),也可定義可變的緩沖區(qū)(NSMutableData類)州胳。
下面代碼展示了如何將文件的內(nèi)容讀入內(nèi)存緩沖區(qū)记焊,然后再將緩沖區(qū)的內(nèi)容寫入到另一個(gè)文件中。
NSData *fileData;
NSFileManager *fileManager = [[NSFileManager alloc]init];
fileData = [fileManager contentsAtPath:path];
[fileManager createFileAtPath:path2 contents:fileData attributes:nil]; //采用默認(rèn)的屬性值
類型轉(zhuǎn)換 NSData -> NSString:
NSString *strData = [[NSString alloc]initWithData:fileData encoding:NSASCIIStringEncoding];
類型轉(zhuǎn)換 NSString -> NSData:
NSData *fileData2 = [strData dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData...(待續(xù)陋葡。亚亲。。)