1)。object c 基本型別
ID:指向OC物件的指標;
CLASS;
SEL:是OC來定義方法的關鍵字,你可以把他當做一個函式指標來使用;
BOOL:YES,NO;TRUE,FLASE;
空值處理不當是引起程式錯誤的重要原因。
nil:給物件賦值
NULL:給指標賦值
NSNULL: 用於集合操作;
2)常量的宣告和預處理巨集
常量的宣告1
#define kdetailkey @"detail text"
#define dd 30.0
#define cc pi*3/180.0
常量的宣告2
typedef enum
{
aa=100,
bb,
cc ,
}TagSystemViews ;
常量的宣告3
靜態常量宣告:static NSString *lc=@"test";
預處理巨集
預處理巨集宣告:
#define network 1
#define NSLog()
3).#import包含標頭檔案
4)#pragram mark分隔程式碼塊
#pragram mark -
#pragram mark network api
5) 記憶體管理retain,release,引用計數進行自動記憶體管理;在alloc,retain,copy 的時候物件引用計數增加一,在release時減一,當引用計數為0時,物件的記憶體就被回收,佔用記憶體釋放。
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// create two objects my MyClass.
SomeClass *object = [[SomeClass alloc] init];
NSLog(@"1,object retain count is : %d",[object retainCount]); // counter is 1
[object retain]; // increment the count, now count is 2
NSLog(@"2,object retain count is : %d",[object retainCount]);
[object release]; // decrement the count, now count is 1
NSLog(@"3,object retain count is : %d",[object retainCount]);
object = nil; // set to nil, now count is 0, now object will deallocate.
NSLog(@"4,object retain count is : %d",[object retainCount]);
NSLog(@"5,object: %@",object);
[pool release];
return 0;
6>字串操作NSString
私有字串常量:static NSString *const k=@"myconstant";
公有字串常量:.h 定義:extern NSString *const k;/ #define NSString *const k;
.m 實現:k=@"info.png";
獲取子字串: rangOfString 獲取小字串在大字串中的長度範圍;
substringToIndex:i 獲取開始到i長度的子字元長度;
substringFromIndex:i 獲取i開始到末尾的子字元長度;
substringWithRange:i,J ,獲取i開始到J的子字元長度;
NSString *filename=@"config.ini";
NSString *path=@"users/dev/desktop/config.ini";
NSRange iStart=[path rangeOfString:filename];
NSRange iStart2=[path rangeOfString:filename options:NSCaseInsensitiveSearch];
if (iStart.length>0) {
NSString *substr=[path substringToIndex:iStart.location-1];
NSString *ex=[path substringFromIndex:iStart.location+1];
合格,格式化字串
stringWithFormat
NSString *re=[NSString stringWithFormat:@"id is %d name is %@,id,name];
NSMutableString *re=[[NSMutableString alloc] init]; //stingbuffer.app();
[re appendString:string1];
[re appendString:string2];
分隔字串
NSSting *a=@"dog#cat";
NSArray *b=[a componentsSeparatedByString:@"#"];
獲取程式執行時目錄
安行讀取檔案
NSstring *temp;
NSArray *readline=[[NSString stringWithContentsOfFile:@"tests.txt"] componentsSeparatedByString:@"\"];
NSEnumerator *nse=[readline objectEnumerator];
while(temp=[nse nextObject])
{
NSLog(@"%@",temp);
}
NSString char*之間的轉換
NSString --> char*:
NSString *ban=@"test a string";
char *pre=[ban cStringUsingEncoding:NSASCIIStingEncoding];
char * -->NSString
char encode_buf[1024];
NSString *enstr=[[NSString alloc] initWithCString:(const char*) encode_buf:NSASCIIStringEncoding];
字串比較
1)判斷是否為空;
if([strobject length] >0)
{
//do something
}
if([strobject isEqualToString:@"some string"])
//do something
7 處理數值物件(NSNumber-->int;CGFloat,NSDecimalNuber])
NSNumber -->int
NSNumber *numobj=[NSNumber numberWithInt:2];
NSInteger myint=[numobj integerValue];
int a=[myint intValue];
8 處理日期時間NSDate
獲取當前時間:
NSDate * dateToDay=[NSDate date];
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:@yyyy-mm-dd hh:mm:ss"];
NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifiler:@"en_US"];
[df setLocale:locale];
從字串生成日期物件:
NSString *mydateString=@"2012-07-13 10:20:00";
NSDate *mydate=[df dateFromString:mydateString];
日期比較:
switch([dateToDay compare:mydate])
{
case NSOrderedSame:
NSLog(@"these dates are the same");
break;
case NSOrderedAscending:
NSLog(@"dateToDay is earlier than mydate");
break;
case NSOrderedDescending:
NSLog(@"mydate is earlier than dateToDay");
break;
default:
NSLog(@"bad time. invalid enum value returned.");
break;
demo:
void dateTimeTest(){
//獲取當前日期時間
NSDate *dateToDay = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[df setLocale:locale];
NSString *myDateString = @"2009-09-15 18:30:00";
//從字串生成日期物件
NSDate *myDate = [df dateFromString: myDateString];
//日期比較
switch ([dateToDay compare:myDate]) {
case NSOrderedSame:
NSLog(@"These dates are the same!");
break;
case NSOrderedAscending:
NSLog(@"dateToDay is earlier than myDate!");
break;
case NSOrderedDescending:
NSLog(@"myDate is earlier than dateToDay!");
break;
default:
NSLog(@"Bad times. Invalid enum value returned.");
break;
}
}
int
main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
dateTimeTest();
[pool release];
return 0;
}
9 NSData 處理
NSData --> NSString
NSMutableData *data; //NSData *data;
NSString *tmpdata=[[NSString alloc] init WithData:data encoding:NSACIIStringEncoding];
NSLog(@"[***] DATA:%@",tmpdata);
NSSting --> NSData
NSString * str=@"test string";
NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
//demo:
void NSDatatTest(){
//NSData 轉換為NSString
NSMutableData *data = nil;
NSString *tmpdata = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"[***] DATA:%@",tmpdata);
[tmpdata release];
//NSString 轉換為 NSData
NSString* str= @"teststring";
NSData* tdata=[str dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"[***] tdata:%@",tdata);
}
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDatatTest();
[pool release];
return 0;
}
10 集合操作:包含,比較,迴圈,獲取,刪除,複製,排序,過粒
NSArray,NSDictionary,NSSet及動態集合
將多個物件裝入陣列,陣列的最後一個元素必須是nil;
NSArray *arry=[NSArray arrayWithObjects:@"hennry",@"alex",nil];
NSMutableArray *arry=[[NSMutable alloc] arrayWithCapacity:3];
NSArray *newarray=[NSArray arrayWithArray:array];
陣列操作 :containsObject:(id)anObject
NSUintegerCount
lastObject
objectAtIndex:(NSUInteger)index
NSMutableArray *arry=[NSMutable arry];
[arry addObject:[NSColor blackColor]];
NSSet:不要求順次,要求唯一性,用集合NSSet;交集,重複