NSNumber
- (NSNumber *)numberWithInt:(int)value;
- (NSNumber *)numberWithDouble:(double)value;
- (int)intValue;
- (double)doubleValue;
NSNumber可以將基本數(shù)據(jù)類型包裝起來(lái),形成一個(gè)對(duì)象憋活,這樣就可以給其發(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對(duì)象可以存儲(chǔ)一段Unicode字符辜梳。在cocoa中,所有和字符泳叠、字符串相關(guān)的處理都是使用NSString來(lái)完成作瞄。
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];
-----寫(xiě)字符串到文件----
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 判斷兩對(duì)象值的大小(按字母順序進(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 判斷兩對(duì)象值的大小(按字母順序進(jìn)行比較德绿,astring02小于astring01為真)
//4荷荤、不考慮大小寫(xiě)比較字符串1
NSString *astring01 = @"this is a String!";
NSString *astring02 = @"This is a String!";
BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;
NSLog(@"result:%d",result);
//NSOrderedDescending判斷兩對(duì)象值的大小(按字母順序進(jìn)行比較,astring02小于astring01為真)
//5移稳、不考慮大小寫(xiě)比較字符串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ū)分大小寫(xiě)比較 NSLiteralSearch:進(jìn)行完全比較蕴纳,區(qū)分大小寫(xiě) NSNumericSearch:比較字符串的字符個(gè)數(shù),而不是字符值个粱。
-----改變字符串的大小寫(xiě)-----
NSString *string1 = @"A String";
NSString *string2 = @"String";
NSLog(@"string1:%@",[string1 uppercaseString]);//uppercaseString返回轉(zhuǎn)換為大寫(xiě)的字符串
NSLog(@"string2:%@",[string2 lowercaseString]);//lowercaseString返回轉(zhuǎn)換為小寫(xiě)的字符串
NSLog(@"string2:%@",[string2 capitalizedString]);//capitalizedString返回每個(gè)單詞首字母大寫(xiě)的字符串
-----在串中搜索子串 -----
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: 從字符串的開(kāi)頭一直截取到指定的位置,但不包括該位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
//2都许、-substringFromIndex: 以指定位置開(kāi)始(包括指定位置的字符)稻薇,并包括之后的全部字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);
//3、-substringWithRange: //按照所給出的位置胶征,長(zhǎng)度塞椎,任意地從字符串中截取子串
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"]; // 如果沒(méi)查找到骂铁,則 (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)是否還包含別的字符串(前綴罩抗,后綴)-----
//01: 檢查字符串是否以另一個(gè)字符串開(kāi)頭- (BOOL) hasPrefix: (NSString *) aString;
NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
//02: 查找字符串某處是否包含給定的字符串 - (NSRange) rangeOfString: (NSString *) aString拉庵,這一點(diǎn)前面在串中搜索子串用到過(guò)
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對(duì)象包含了日期和時(shí)間钞支、時(shí)區(qū)以及一個(gè)帶有格式的字符串,它從NSDate繼承而來(lái)泣懊。
NSCalendarDate對(duì)象是immutable的伸辟,一旦被創(chuàng)建,無(wú)法修改其中的時(shí)間和日期馍刮,當(dāng)然可以修改那個(gè)帶格式的字符串和時(shí)區(qū)信夫。
以下是常用方法:
+(id)calendarDate; //創(chuàng)建當(dāng)前日期和時(shí)間以及默認(rèn)格式的NSCalendarDate對(duì)象,時(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對(duì)象--------
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í)存儲(chǔ)區(qū)匈辱,它通常成為緩沖區(qū)振湾。
NSData類提供了一種簡(jiǎn)單的方式,它用來(lái)設(shè)置緩沖區(qū)亡脸、將文件的內(nèi)容讀入緩沖區(qū)押搪,或?qū)⒕彌_區(qū)的內(nèi)容寫(xiě)到一個(gè)文件树酪。
對(duì)于32位應(yīng)用程序,NSDATA緩存區(qū)最多可以存儲(chǔ)2GB的數(shù)據(jù)大州。
我們既可定義不變緩沖區(qū)(NSData類)续语,也可定義可變的緩沖區(qū)(NSMutableData類)。
下面代碼展示了如何將文件的內(nèi)容讀入內(nèi)存緩沖區(qū)厦画,然后再將緩沖區(qū)的內(nèi)容寫(xiě)入到另一個(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];