用法總結:NSNumber、NSString、NSData
NSNumber可以將基本資料型別包裝起來,形成一個物件,這樣就可以給其傳送訊息,裝入NSArray中等等。
NSNumber * intNumber=[NSNumber numberWithInt:100];
NSNumber *floatNumber=[NSNUmber numberWithFloat:100.00];
int i=[intNumber intValue];
if([intNumber isEqualToNumber:floatNumber]) ....
astring =@"This is a String!";
NSLog(@"astring:%@",astring);
[astringrelease];
NSLog(@"astring:%@",astring);
[astring release];
//4、建立臨時字串
astring =[NSString stringWithCString:"This is atemporary string"];
NSLog(@"astring:%@",astring);
int j = 2;
NSString *astring =[[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.Thisis %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 aString!"];
NSLog(@"astring:%@",astring);
NSString *path =@"astring.text";
[astring writeToFile: path atomically:YES];
[astringrelease];
-----比較兩個字串-----
//1、用C比較:strcmp函式
charstring1[] = "string!";
char string2[] ="string!";
if(strcmp(string1, string2) = =0)
{
NSLog(@"1");
}
//2、isEqualToString方法
NSString *astring01 = @"Thisis a String!";
NSString *astring02 = @"Thisis a String!";
BOOL result =[astring01 isEqualToString:astring02];
NSLog(@"result:%d",result);
//3、compare方法(comparer返回的三種值:NSOrderedSame,NSOrderedAscending,NSOrderedDescending)
NSString *astring01 = @"Thisis a String!";
NSString *astring02 = @"Thisis a String!";
BOOL result =[astring01 compare:astring02] == NSOrderedSame; //NSOrderedSame判斷兩者內容是否相同
NSLog(@"result:%d",result);
NSString*astring01 = @"This is a String!";
NSString *astring02 = @"thisis a String!";
BOOL result =[astring01 compare:astring02] == NSOrderedAscending;
NSLog(@"result:%d",result);
//NSOrderedAscending判斷兩物件值的大小(按字母順序進行比較,astring02大於astring01為真)
NSString *astring01 = @"this is aString!";
NSString *astring02 = @"Thisis a String!";
BOOL result =[astring01 compare:astring02] == NSOrderedDescending;
NSLog(@"result:%d",result);
//NSOrderedDescending判斷兩物件值的大小(按字母順序進行比較,astring02小於astring01為真)
//4、不考慮大小寫比較字串1
NSString *astring01 = @"thisis a String!";
NSString *astring02 = @"Thisis a String!";
BOOL result =[astring01 caseInsensitiveCompare:astring02] == NSOrderedSame;
NSLog(@"result:%d",result);
//NSOrderedDescending判斷兩物件值的大小(按字母順序進行比較,astring02小於astring01為真)
//5、不考慮大小寫比較字串2
NSString *astring01 = @"thisis a String!";
NSString *astring02 = @"Thisis a String!";
BOOL result = [astring01compare:astring02
options:NSCaseInsensitiveSearch | NSNumericSearch] = =NSOrderedSame;
NSLog(@"result:%d",result);
//NSCaseInsensitiveSearch:不區分大小寫比較NSLiteralSearch:進行完全比較,區分大小寫 NSNumericSearch:比較字串的字元個數,而不是字元值。
-----改變字串的大小寫-----
NSString*string1 = @"A String";
NSString *string2 =@"String";
NSLog(@"string1:%@",[string1uppercaseString]);//uppercaseString返回轉換為大寫的字串
NSLog(@"string2:%@",[string2 lowercaseString]);//lowercaseString返回轉換為小寫的字串
NSLog(@"string2:%@",[string2 capitalizedString]);//capitalizedString返回每個單詞首字母大寫的字串
-----在串中搜尋子串 -----
NSString *string1 = @"This is a string";
NSString *string2 =@"string";
NSRange range = [string1rangeOfString:string2];
int location =range.location;
int leight =range.length;
NSString *astring =[[NSString alloc] initWithString:[NSStringstringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"astring:%@",astring);
[astring release];
-----抽取子串 -----
//1、-substringToIndex: 從字串的開頭一直擷取到指定的位置,但不包括該位置的字元
NSString *string1 = @"This isa string";
NSString *string2 =[string1 substringToIndex:3];
NSLog(@"string2:%@",string2);
//2、-substringFromIndex: 以指定位置開始(包括指定位置的字元),幷包括之後的全部字元
NSString *string1 = @"This isa string";
NSString *string2 =[string1 substringFromIndex:3];
NSLog(@"string2:%@",string2);
//3、-substringWithRange: //按照所給出的位置,長度,任意地從字串中擷取子串
NSString *string1 = @"This isa string";
NSString *string2 =[string1 substringWithRange:NSMakeRange(0,4)];
NSLog(@"string2:%@",string2);
for(NSString*filename indirenum) {
if([[filename pathExtension] isEqualToString:@"jpg"]){
[filesaddObject:filename];
}
}
NSLog(@"files:%@",files);
//5、列舉
NSEnumerator *filenum;
filenum = [filesobjectEnumerator];
while (filename = [filenumnextObject]) {
NSLog(@"filename:%@",filename);
}
NSLog(@"oldArray:%@",oldArray);
NSEnumerator*enumerator;
enumerator = [oldArrayobjectEnumerator];
id obj;
while(obj = [enumeratornextObject]) {
[newArrayaddObject: obj];
}
[newArraysortUsingSelector:@selector(compare:)];
NSLog(@"newArray:%@",newArray);
[newArrayrelease];
-----切分陣列-----
//1、從字串分割到陣列-componentsSeparatedByString:
NSString *string = [[NSStringalloc] initWithString:@"One,Two,Three,Four"];
NSLog(@"string:%@",string);
NSArray *array= [string componentsSeparatedByString:@","];
NSLog(@"array:%@",array);
[stringrelease];
//2、從陣列合並元素到字串-componentsJoinedByString:
NSArray *array = [[NSArrayalloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
NSString *string = [arraycomponentsJoinedByString:@","];
NSLog(@"string:%@",string);
-----從目錄搜尋副檔名為jpg的檔案-----
//NSFileManager *fileManager= [NSFileManager defaultManager];
NSString *home;
home = @"../Users/";
NSDirectoryEnumerator*direnum;
direnum = [fileManagerenumeratorAtPath: home];
NSMutableArray*files = [[NSMutableArray alloc]init];
//列舉
NSString *filename;
while (filename = [direnumnextObject]) {
if([[filenamepathExtension] hasSuffix:@"jpg"]){
[filesaddObject:filename];
}
}
//擴充套件路徑
NSString *Path = @"~/NSData.txt";
NSString *absolutePath =[Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePathstringByAbbreviatingWithTildeInPath]);
//副檔名
NSString *Path =@"~/NSData.txt";
NSLog(@"Extension:%@",[PathpathExtension]);
-----查詢與替換-----
- (NSString *)stringByReplacingCharactersInRange:(NSRange)rangewithString:(NSString
*)replacement
+ (id)string;
- (void)appendString:(NSString *)string;
- (void)appendFormat:(NSString *)format, ...;
//stringWithCapacity:
NSMutableString*String;
String = [NSMutableStringstringWithCapacity:40];
-----在已有字串後面新增字元-----
//appendString:and
appendFormat:
NSMutableString*String1 = [[NSMutableString alloc] initWithString:@"This is aNSMutableString"];
//[String1 appendString:@", I will be addingsome character"];
[String1 appendFormat:[NSStringstringWithFormat:@", I will be adding some character"]];
NSLog(@"String1:%@",String1);
----- 在已有字串中按照所給出範圍刪除字元----
//deleteCharactersInRange:
NSMutableString*String1 = [[NSMutableString alloc] initWithString:@"This is aNSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0,5)]; // 刪除指定範圍(location=0,length=5)的字串
NSLog(@"String1:%@",String1);
----在已有字串後面在所指定的位置中插入給出的字串-----
//-insertString: atIndex:
NSMutableString *String1 =[[NSMutableString alloc] initWithString:@"This is aNSMutableString"];
[String1 insertString:@"Hi! "atIndex:0];
NSLog(@"String1:%@",String1);
[String1insertString:@"and StringEnd", atIndex:[String1length]]; // 在可變字串的最後插入
----將已有的空符串換成其它的字串-----
//-setString:
NSMutableString *String1 =[[NSMutableString alloc] initWithString:@"This is aNSMutableString"];
[String1 setString:@"Hello Word!"];
NSLog(@"String1:%@",String1);
----查詢-----
NSRange subRange =[String1 rangeOfString:@"isa"]; // 如果沒查詢到,則(subRange.location == NSNotFound)為真。
----按照所給出的範圍替換的原有的字元-----
//-setString:
NSMutableString *String1 =[[NSMutableString alloc] initWithString:@"This is aNSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0,4)withString:@"That"]; // 用於NSMutableString
NSLog(@"String1:%@",String1);
- (NSUInteger)replaceOccurrencesOfString:(NSString*)target withString:(NSString*)replacement options:(NSStringCompareOptions)opts range:(NSRange)searchRange
----判斷字串內是否還包含別的字串(字首,字尾)-----
//01: 檢查字串是否以另一個字串開頭- (BOOL) hasPrefix: (NSString *)aString;
NSString *String1 =@"NSStringInformation.txt";
[String1hasPrefix:@"NSString"] = = 1? NSLog(@"YES") :NSLog(@"NO");
[String1 hasSuffix:@".txt"] == 1 ? NSLog(@"YES") :NSLog(@"NO");
//02: 查詢字串某處是否包含給定的字串 -(NSRange) rangeOfString: (NSString *)aString,這一點前面在串中搜尋子串用到過
NSRange subRange;
subRange = [string1 rangeOfString:@"stringA"]; //查詢字串string1中是否包含“stringA”。返回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物件包含了日期和時間、時區以及一個帶有格式的字串,它從NSDate繼承而來。
NSCalendarDate物件是immutable的,一旦被建立,無法修改其中的時間和日期,當然可以修改那個帶格式的字串和時區。
以下是常用方法:
+(id)calendarDate; //建立當前日期和時間以及預設格式的NSCalendarDate物件,時區為機器設定好的時區。
+(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; // 返回是日的第幾個小時(0-23)
-(void)setCalendarFormate:(NSString*)format
--------建立NSCalendarDate物件--------
NSCalendarDate*now;
now = [NSCalendarDatecalendarDate];
NSTimeZone *pacific =[NSTimeZone timeZoneWithName:@"PST"];
NSCalendarDate *hotTime= [NSCalendarDate dateWithYear:2011 month:2 day:3 hour:14 minute:0 second:0 timeZone:pacific];
NSData
使用檔案時,需要頻繁地將資料讀入一個臨時儲存區,它通常成為緩衝區。
NSData類提供了一種簡單的方式,它用來設定緩衝區、將檔案的內容讀入緩衝區,或將緩衝區的內容寫到一個檔案。
對於32位應用程式,NSDATA快取區最多可以儲存2GB的資料。
我們既可定義不變緩衝區(NSData類),也可定義可變的緩衝區(NSMutableData類)。
下面程式碼展示瞭如何將檔案的內容讀入記憶體緩衝區,然後再將緩衝區的內容寫入到另一個檔案中。
<span style="color:#66066;">NSData</span><span> </span><span style="color:#66660;">*</span><span>fileData</span><span style="color:#66660;">;</span> <span style="color:#66066;">NSFileManager</span><span> </span><span style="color:#66660;">*</span><span>fileManager </span><span style="color:#66660;">=</span><span> </span><span style="color:#66660;">[[</span><span style="color:#66066;">NSFileManager</span><span> alloc</span><span style="color:#66660;">]</span><span>init</span><span style="color:#66660;">];</span> <span>fileData </span><span style="color:#66660;">=</span><span> </span><span style="color:#66660;">[</span><span>fileManager contentsAtPath</span><span style="color:#66660;">:</span><span>path</span><span style="color:#66660;">];</span><span> </span> <span style="color:#66660;">[</span><span>fileManager createFileAtPath</span><span style="color:#66660;">:</span><span>path2 contents</span><span style="color:#66660;">:</span><span>fileData attributes</span><span style="color:#66660;">:</span><span style="color:#0088;">nil</span><span style="color:#66660;">];</span><span> </span><span style="color:#8800;">//採用預設的屬性值</span>
型別轉換 NSData -> NSString:
NSString *strData =[[NSString alloc]initWithData:fileDataencoding:NSASCIIStringEncoding];
型別轉換 NSString-> NSData:
NSData *fileData2 = [strDatadataUsingEncoding:NSUTF8StringEncoding];
NSMutableData
相關文章
- NSString, NSData和Byte的相互轉換
- 轉 IOS---NSdata 與 NSString,Byte陣列,UIImage 的相互轉換iOS陣列UI
- object c基礎, 基本型別(NSString,char*NSDate,NSData),集合NSArray,NSMutableArray,NSDictionary,NSMutableDiction...Object型別
- pandas用法總結
- layui用法總結UI
- less用法總結
- Promise用法總結Promise
- sed用法總結
- Autotrace 用法總結
- Quartz:基本用法總結quartz
- axios用法總結iOS
- Const 用法總結
- xPath 用法總結整理
- STL:set用法總結
- STL:map用法總結
- STL:vector用法總結
- STL:list用法總結
- jq命令用法總結
- sed命令用法總結
- 【Git】stash 用法總結Git
- iframe元素用法總結
- adb 用法總結
- uniDAC用法總結
- type的用法總結
- oracle hints用法總結Oracle
- sqlldr的用法總結SQL
- IOS FoundationKit (NSString) 知識彙總iOS
- React屬性用法總結React
- 【Less】Less基本用法總結
- git reset rebase 用法總結Git
- jquery validate用法總結jQuery
- Java習慣用法總結Java
- Htaccess檔案用法總結
- ORACLE 中ROWNUM用法總結!Oracle
- ORACLE 中ROWNUM用法總結Oracle
- ORACLE 繫結變數用法總結Oracle變數
- NSData 所有APIAPI
- Butter Knife常用用法總結