object c基礎, 基本類型(NSString,char*NSDate,NSData),集合NSArray,NSMutableArray,NSDictionary,NSMutableDictionary,NSSet,NSMutableSet



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;交集,重復

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM