日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OC-Foundation框架

發布時間:2025/3/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OC-Foundation框架 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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);

?

自定義格式

?????[dateFormatter?setDateFormat:@"yyyy- MM-dd?HH:mm:ss"];?//這里要注意的是formatter的格式,如果是小寫的"hh",那么時間將會跟著系統設置變成12小時或者 24小時制。大寫的"HH",則強制為24小時制。?

?????[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);

?

?

NSDate的常用用法==============================================
1. 創建或初始化可用以下方法 ----------------------------------------------------

用于創建NSDate實例的類方法有 + (id)date;
返回當前時間?

+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; 返回以當前時間為基準,然后過了secs秒的時間?

? ? +

(id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)

secs;

返回以2001/01/01 GMT為基準,然后過了secs秒的時間?

? ? +

(id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;

返回以1970/01/01 GMT為基準,然后過了secs秒的時間?

? ? + (id)distantFuture;

返回很多年以后的未來的某?一天。 比如你需要?一個比現在(Now)晚(大)很長時間的時間值,則可以調用該方法。?

? ? + (id)distantPast;

? 返回很多年以前的某?一天。

比如你需要?一個比現在(Now)早(小)大很長時間的時間值,則可以調用該方 法。?

用于創建NSDate實例的實例方法有
- (id)addTimeInterval:(NSTimeInterval)secs; 返回以目前的實例中保存的時間為基準,然后過了secs秒的時間?

用于初始化NSDate實例的實例方法有 - (id)init; 初始化為當前時間。類似date方法?

? ? -

(id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)

secs;

初始化為以2001/01/01 GMT為基準,然后過了secs秒的時間。類似 dateWithTimeIntervalSinceReferenceDate:方法?

? ? - (id)initWithTimeInterval:(NSTimeInterval)secs

sinceDate:(NSDate *)refDate;

初始化為以refDate為基準,然后過了secs秒的時間?

- (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs; 初始化為以當前時間為基準,然后過了secs秒的時間?

3. 取回時間間隔可用以下方法 ------------------------------------------------------------------

? ? - (NSTimeInterval)timeIntervalSinceDate:(NSDate

*)refDate;

以refDate為基準時間,返回實例保存的時間與refDate的時間間隔?

? ? - (NSTimeInterval)timeIntervalSinceNow;

以當前時間(Now)為基準時間,返回實例保存的時間與當前時間(Now)的時 間間隔?

? ? - (NSTimeInterval)timeIntervalSince1970;

以1970/01/01 GMT為基準時間,返回實例保存的時間與1970/01/01 GMT 的時間間隔?

? ? - (NSTimeInterval)timeIntervalSinceReferenceDate;

以2001/01/01 GMT為基準時間,返回實例保存的時間與2001/01/01 GMT 的時間間隔?

? ? + (NSTimeInterval)timeIntervalSinceReferenceDate;

以2001/01/01 GMT為基準時間,返回當前時間(Now)與2001/01/01 GMT 的時間間隔?

*/?

// 時區類
// 獲得系統時區?

NSTimeZone *tz = [NSTimeZone systemTimeZone];
// 獲得當前時間距離GMT時間相差的秒數!
NSInteger seconds = [tz secondsFromGMTForDate:[NSDate?

date]];
// 以[NSDate date]時間為基準,間隔seconds秒后的時間! NSDate *localDate = [NSDate?

dateWithTimeInterval:seconds sinceDate:[NSDate date]]; // 北 京時間?

?? ? ? ? ? NSLog(@"localDate = %@",localDate);

// 日期和字符串相互轉換?

// 日期格式的類!
NSDateFormatter *df = [[NSDateFormatter alloc] init]; // 2013-11-27 11:00:57
// HH 24小時進制 hh 12小時進制
df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *dateStr = [df stringFromDate:[NSDate date]]; NSLog(@"dateStr = %@", dateStr);?

?? ? ? NSDate *dateTest = [df dateFromString:@"2013-11-27

11:00:57"];

NSLog(@"dateTest = %@", dateTest);//默認是格林威治時間需 要轉化為北京時間?

NSTimeZone *tz = [NSTimeZone systemTimeZone]; // 獲得dateTest距離GMT時間相差的秒數! NSInteger seconds = [tz?

secondsFromGMTForDate:dateTest];

NSDate *localDate = [NSDate dateWithTimeInterval:seconds sinceDate: dateTest]; // 北京時間?

?? ? ? NSLog(@"localDate = %@",localDate);

?

【閱讀官方文檔】

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

//缺點:

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

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

?

//優點

1.知識嚴謹。

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

?

轉載于:https://www.cnblogs.com/GJ-ios/p/5313304.html

總結

以上是生活随笔為你收集整理的OC-Foundation框架的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。