Foundation架構下的常用類:NSNumber、NSDate、NSCalendar、NSDateFormatter、NSNull、NSKeyedArchiver,nsnumbernsdate

來源:互聯網
上載者:User

Foundation架構下的常用類:NSNumber、NSDate、NSCalendar、NSDateFormatter、NSNull、NSKeyedArchiver,nsnumbernsdate

==========================

Foundation架構下的常用類

==========================

 

一.【NSNumber】

【注】像int、float、char、double等這種都是基礎資料類型。

【注】繼承自C語言的基礎變數類型(int,float,char、double等)不能被添加到數組和字典等oc專有的資料結構中。使用不方便,也不能通過添加類別等oc專有文法進行管理。

【另】可以認為,NSNumber是基礎類型資料轉成物件類型資料的一個類。

【注】NSNumber 就是一個類,這個類就是為了把基礎資料類型轉成對象資料類型的一個類。

【注】可以先將基礎類型的資料存入到nsnumber對象中,再將nsnumber存入到數組或者字典中。

【注】NSNumber能乾的事情都可以用NSString來取代。所以,更常用NSString。

 

二.【NSDate】

【注】NSDate就是一個日期(時間)類。

【注】因為存在時區差異,擷取的時間差8小時

 

∆        1 // 目前時間建立NSDate

                NSDate *myDate = [NSDate date];

                NSLog(@"myDate = %@",myDate);

 ∆       2 // 從現在開始的24小時

                NSTimeInterval secondsPerDay = 24*60*60;

                NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

                NSLog(@"myDate = %@",tomorrow);

     3 // 根據已有日期建立日期

                 NSTimeInterval secondsPerDay1 = 24*60*60;

                NSDate *now = [NSDate date];

                NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1];

                NSLog(@"yesterDay = %@",yesterDay);

         

   ∆      4 // 比較日期

                BOOL sameDate = [now isEqualToDate:yesterDay];

                NSLog(@"sameDate = %lu",sameDate);

    ∆            4.1 // 擷取較早的日期

                NSDate *earlierDate = [yesterDay earlierDate:now];

                NSLog(@"earlierDate  = %@",earlierDate);

    ∆            4.2 // 較晚的日期

                NSDate *laterDate = [yesterDay laterDate:now];

                NSLog(@"laterDate  = %@",laterDate);

三. NSCalendar

// 通過NSCALENDAR類來建立日期

// 通過NSCalendar這個類可以自己建立一個指定的日期 

        NSDateComponents *comp = [[NSDateComponents alloc]init];

        [comp setMonth:06];

        [comp setDay:01];

        [comp setYear:2001];

        [comp setHour:24];

        NSCalendar *myCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

        NSDate *myDate1 = [myCal dateFromComponents:comp];

        

        NSLog(@"myDate1 = %@",myDate1);

 

// 從已有日期擷取日期

        NSCalendar *myCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

        unsigned units  = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;

        

        NSDateComponents *comp1 = [myCal components:units fromDate:[NSDate date]];

        NSInteger month = [comp1 month];

        NSInteger year = [comp1 year];

        NSInteger day = [comp1 day];

 

ΔΔ四【NSDateFormatter】格式化日期類

 

NSDateFormatter 的一些格式介紹 

// 執行個體化一個NSDateFormatter對象

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

// 設定時間格式,這裡可以設定成自己需要的格式

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

// 用[NSDate date]可以擷取系統目前時間

NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];

// 輸出格式為:2016-03-23 13:22:13

NSLog(@”%@”,currentDateStr);

 

自訂格式

   // 這裡要注意的是formatter的格式,如果是小寫"hh",那麼時間將會跟著系統設定變成12小時或者 24小時制。大寫的"HH",則強製為24小時制。 

     [dateFormatter setDateFormat:@"yyyy- MM-dd HH:mm:ss"]; 

     [dateFormatter setDateFormat:@"yyyy年MM月dd日#EEEE"];      // EEEE為星期幾,EEE為周幾 

     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

     [dateFormatter setDateFormat:@"yyyy年MMMMd日"];              // MMMM 為xx月,一個d可以省去01日前的0

 

輸出格式通setDateStyle和setTimeStyle設定,分別定義的日期和時間的格式可選一下的系統給出的方法

typedef enum {

    NSDateFormatterNoStyle     = kCFDateFormatterNoStyle,

    NSDateFormatterShortStyle  = kCFDateFormatterShortStyle,//“11/23/37” or “3:30pm”

    NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle,//\"Nov 23, 1937\"

    NSDateFormatterLongStyle   = kCFDateFormatterLongStyle,//\"November 23, 1937” or “3:30:32pm\"

    NSDateFormatterFullStyle   = kCFDateFormatterFullStyle//“Tuesday, April 12, 1952 AD” or “3:30:42pm PST”

} NSDateFormatterStyle;

 

五.日期比較

日期之間比較可用以下方法

    - (BOOL)isEqualToDate:(NSDate *)otherDate;

    與otherDate比較,相同返回YES

 

    - (NSDate *)earlierDate:(NSDate *)anotherDate;

    與anotherDate比較,返回較早的那個日期

 

    - (NSDate *)laterDate:(NSDate *)anotherDate;

    與anotherDate比較,返回較晚的那個日期

 

    - (NSComparisonResult)compare:(NSDate *)other;

    該方法用於排序時調用:

      . 當執行個體儲存的日期值與anotherDate相同時返回NSOrderedSame

      . 當執行個體儲存的日期值晚於anotherDate時返回NSOrderedDescending

      . 當執行個體儲存的日期值早於anotherDate時返回NSOrderedAscending

 

六.NSDate轉NSString互相轉化

         // NSDate 轉NSString 並截取月份

         NSString* time = [[NSString alloc]initWithFormat:@"%@",myDate1];

        NSLog(@"%@",time);

        NSRange range = {5,2};

        NSLog(@"month:%@",[time substringWithRange:range]);

 

Δ七.iOS-NSDate 相差 8 小時 

//方法一

- (void)tDate

{

    NSDate *date = [NSDatedate];

    NSTimeZone *zone = [NSTimeZonesystemTimeZone];

    NSInteger interval = [zone secondsFromGMTForDate: date];

    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];  

    NSLog(@"%@", localeDate);

}

//方法二

+ (NSString *)fixStringForDate:(NSDate *)date 

{

    NSDateFormatter* dateFormatter = [[NSDateFormatteralloc]init];

    [dateFormatter setDateStyle:kCFDateFormatterFullStyle];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *fixString = [dateFormatter stringFromDate:date]; 

    [dateFormatter release];

    return fixString;

}

 

ΔΔ【NSNull】

【注】表示空的事物有四個

【NULL】【nil】【Nil】【NSNull】

 

NULL:表示基礎類型指標為空白

int * p = NULL;

 

nil:表示對象指標為空白

id obj = nil;

 

Nil:表示Class變數為空白

Class class = Nil;

 

NSNull:用在數組字典等資料結構中佔位,作為空白元素

// 唯一方法

[NSNull null]; 建立表示空的對象

 

八.歸檔 NSKeyedArchiver

    //  建立了一個數組,初始化了一些資料

        NSArray* array = [[NSArray alloc]initWithObjects:@"zhangsan",@"lisi",@"wanger",@"xiaoming", nil];

        

        // 先指定要儲存的檔案名稱以及路徑

        // NSHomeDirectory()就是當前系統的home路徑

        // stringByAppendingPathComponent 添加一個檔案,檔案名稱是:file

        // 檔案類型可以不寫,檔案名稱和尾碼隨便

        NSString* filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"file.txt"];

        

        NSLog(@"歸檔檔案路徑:%@",filePath);

        

        // NSKeyedArchiver 這個類是用來歸檔的類

        // 傳回值代表歸檔是否成功

        BOOL isSuccess = [NSKeyedArchiver archiveRootObject:array toFile:filePath];

        if (isSuccess == YES) {    

            NSLog(@"檔案儲存成功");

        }

        //【注】使用這種方法歸檔的檔案都是經過簡單加密的,打不開,也是不允許開啟的。

        // 解歸檔

        NSArray* Arr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

        NSLog(@"取出的資料是:%@",Arr);

【閱讀官方文檔】

【注】建議閱讀Xcode官方,官方文檔非常標準的,沒有錯誤。

// 缺點:

1.可讀性比較差。(讀不懂)(官方文檔都比較言簡意賅)

2.例子比較少,很少有參照demo。

 

// 優點

1.知識嚴謹。

2.當遇到某些特殊(疑難雜症)官方文檔都可以找到答案(前提是花費時間去閱讀尋找)

【解決編程中問題的方法】

1.可以先百度。

2.請去Google。(Google出來答案99%英文文檔,配合有道詞典進行查閱)

3.請去請教同事。

4.請看官方文檔。

5.請配合有道詞典查看。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.