objective c 中字串與日期相互轉換

2022-05-05 15:18:11 字數 537 閱讀 7005

1、字串轉換為日期

nsdateformatter* dateformat = [[nsdateformatter alloc] init];//例項化乙個nsdateformatter物件

[dateformat setdateformat:@"yyyy-mm-dd hh:mm:ss"];//設定時間格式,這裡可以設定成自己需要的格式

nsdate *date =[dateformat datefromstring:@"1980-01-01 00:00:01"]; 

2、日期轉換為字串

nsdateformatter* dateformat = [[nsdateformatter alloc] init];//例項化乙個nsdateformatter物件

[dateformat setdateformat:@"yyyy-mm-dd hh:mm:ss"];//設定時間格式,這裡可以設定成自己需要的格式

nsstring *currentdatestr = [dateformatter stringfromdate:[nsdate date]];

objective c中字串長度計算

我們知道,在c語言中,使用sizeof 計算在記憶體中占用的位元組數,引用string.h後,使用strlen 計算字串的長度 不包含 0 而在object c中,length 這個方法,可以返回字串的長度。比如 nsstring str 12345 int a 0 a str lenght 此時a...

Objective C中數字 字串

1.在c語言中我們討論的所有數字資料型別,int型 float型和long型都是oc語言中的基本資料型別,但他們都不是物件。如果需要儲存基本資料型別 包括char型 可以使用nsnumber類,它會根據這些資料的型別建立物件。以下是數字物件相見的操作方法。import int main int ar...

Objective C字串處理

objective c字串處理 objective c獲得字串長度 objective c兩個字串連線 檢索字串 紀錄下些許關於objective c字串處理的函式 nslog 字串處理 獲得字串長度 nsstring str1 mac os pro nslog 長度是 d str1 length ...