iOS開發(fā)這些最基礎的東西你確定你都知道嗎

一卿樱、字符串

1销部、不可變字符串的創(chuàng)建(NSSString)

//最簡單的創(chuàng)建方式
NSString * str1 = @"I love iOS";

//以str1創(chuàng)建str2
NSString * str2 = [[NSString alloc] initWithString:str1];

//以C的字符串創(chuàng)建OC字符串
char * cStr = "I love iOS ha";
NSString * str3 = [[NSString alloc] initWithCString:cStr encoding:NSUTF8StringEncoding];    
NSString * str4 = [[NSString alloc] initWithUTF8String:cStr];

//format格式創(chuàng)建字符串(拼接字符串)
int a = 1;
float b = 3.14;
NSString * str5 = @"I like iOS";
NSString * str6 = [[NSString alloc] initWithFormat:@"??%d??%f??%@",a,b,str6];

//從文件當中讀取字符串
NSString * str7 = [[NSString alloc] initWithContentsOfFile:PATH encoding:NSUTF8StringEncoding error:nil];

2、字符串的比較

//*****************C字符串的比較
char *p = "jos";
char *q = "ios";

int ret = strcmp(p, q);//ret = p - q
if (ret > 0) {
    NSLog(@"p > q");
} else if (ret < 0) {
    NSLog(@"p < q");
} else {
    NSLog(@"p == q");
}

//*****************比較相等與否
NSString * str1 = @"I love ios";
NSString * str2 = @"I love ios";
if ([str1 isEqualToString:str2]) {
    NSLog(@"相等");
}else{
    NSLog(@"不相等");
}

//*****************OC字符串比較大小
NSString * str3 = @"how do you do";
NSString * str4 = @"how do you do";
NSComparisonResult result = [str3 compare:str4];
if (result == NSOrderedSame) {
    NSLog(@"str3 == str4");
}else if (result == NSOrderedAscending){
    NSLog(@"str3 < str4");
}else if (result == NSOrderedDescending){
    NSLog(@"str3 > str4");
}

//*****************不區(qū)分大小寫的比較
NSString * str5 = @"how are you";
NSString * str6 = @"HOW ARE YOU";
NSComparisonResult result1 = [str5 caseInsensitiveCompare:str6];
if (result1 == NSOrderedSame) {
    NSLog(@"str5 == str6");
}else if (result1 == NSOrderedAscending){
    NSLog(@"str5 < str6");
}else if (result1 == NSOrderedDescending){
    NSLog(@"str5 > str6");
}

3奥帘、字符串的處理

NSString * str = @"I love iOS";

//小寫轉大寫
NSString * str1 = [str uppercaseString];

//大寫轉小寫
NSString * str2 = [str lowercaseString];

//首字母大寫
NSString * str3 = [str capitalizedString];

//字符串范圍
NSRange range = [str rangeOfString:@"love"];//空格也要算在內
//位置range.location == 2铜邮,長度range.length == 4

//提取字符串(第一個字符的下標是0)
NSString * str4 = [str substringFromIndex:3];//截取從第3個字符開始到結尾(即:ove iOS)
NSString * str5 = [str substringToIndex:5];//截取前5個字符(即:I lov)
NSString * str6 = [str substringWithRange:NSMakeRange(2, 4)];//截取從第2個字符起往后數(shù)4個字符(即:love)

//判斷字符串首尾
if ([str hasPrefix:@"I love"]) {
    NSLog(@"是以I love開頭");
}
if ([str hasSuffix:@"iOS"]) {
    NSLog(@"是以iOS結尾");
}

4、可變字符串的創(chuàng)建(NSMutableString)

//方式1
NSString * str = @"I love iOS";
NSMutableString * mStr1 = [[NSMutableString alloc] initWithString:str];
//方式2
int i = 3;
NSMutableString * mstr2 = [[NSMutableString alloc] initWithFormat:@"i = %d",i];
//方式3
NSMutableString * mstr3 = [[NSMutableString alloc] initWithUTF8String:"haha"];

5寨蹋、可變字符串的處理

//字符串的追加1(追加到尾部)
NSMutableString * mStr1 = [[NSMutableString alloc] initWithString:@"I love iOS"];
[mStr1 appendString:@" very much"];  
//結果:mStr1 = @"I love iOS very much"

//字符串的追加2(追加到尾部)
NSMutableString * mStr2 = [[NSMutableString alloc] initWithString:@"I love iOS"];
int i = 2;
[mStr2 appendFormat:@" ?%d", i];  
//結果:mStr2 = @"I love iOS ?2?"

//字符串的插入
NSMutableString * mStr3 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr3 insertString:@"yyy" atIndex:3];
//結果:mStr3 = @"abcyyydefg"

//字符串的刪除
NSMutableString * mStr4 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr4 deleteCharactersInRange:NSMakeRange(3, 2)];
//結果:mStr4 = @"abcfg"

//字符串的替換
NSMutableString * mStr5 = [[NSMutableString alloc] initWithString:@"abcdefg"];
[mStr5 replaceCharactersInRange:NSMakeRange(0, 2) withString:@"1234"]
//結果:mStr5 = @"1234cdefg"

二松蒜、數(shù)組

1、不可變數(shù)組

#創(chuàng)建數(shù)組#
//創(chuàng)建方式1
NSArray *array1 = [[NSArray alloc] initWithObjects:@"xixi",@"haha",@"hehe", nil];
//創(chuàng)建方式2
NSArray *array2 = [[NSArray alloc] initWithArray:array1];
//創(chuàng)建方式3
NSArray *array3 = @[@"xixi",@"haha",@"hehe"];
//創(chuàng)建方式4
NSArray *array4 = [NSArray arrayWithObjects:@"how",@"are",@"you", nil];
//創(chuàng)建方式5
NSArray *array5 = [NSArray arrayWithArray:array4];

#查找元素位置#
NSArray *array6 = @[@"how", @"are", @"you"];
NSUInteger index = [array6 indexOfObject:@"are"];
//結果:index = 1

#字符串分割到數(shù)組里#
NSString *str = @"You@are@the@best";
NSArray *array7 = [str componentsSeparatedByString:@"@"];
//結果:array7 = @[@"You",@"are",@"the",@"best"];

#數(shù)組連接成字符串#
NSArray *array8 = @[@"I", @"love", @"you"];
NSString *str = [array8 componentsJoinedByString:@"?"];
//結果:str = @"I?love?you"

2已旧、可變數(shù)組

#創(chuàng)建數(shù)組#
//創(chuàng)建方式1
NSMutableArray * mArray1 = [[NSMutableArray alloc] initWithObjects:@"xixi",@"haha",@"hehe", nil];
//創(chuàng)建方式2
NSMutableArray * mArray2 = [[NSMutableArray alloc] initWithArray:mar1];
//創(chuàng)建方式3
NSMutableArray * mArray3 = [NSMutableArray arrayWithObjects:@"how",@"are",@"you", nil];
//創(chuàng)建方式4
NSMutableArray * mArray4 = [NSMutableArray arrayWithArray:mar3];

#增刪替換#
NSArray * array = @[@"how",@"do",@"you"];
NSMutableArray * mArray5 = [[NSMutableArray alloc] initWithArray:array];

//添加
[mArray5 addObject:@"do"];
//結果:mArray5 = @[@"how", @"do", @"you", @"do"]

//插入
[mArray5 insertObject:@"GG" atIndex:0];
//結果:mArray5 = @[@"GG", @"how", @"do", @"you", @"do"]

//刪除
[mArray5 removeObjectAtIndex:0];
//結果:mArray5 = @[@"how", @"do", @"you", @"do"]

[mArray5 removeLastObject];
//結果:mArray5 = @[@"how", @"do", @"you"]

//替換
[mArray5 replaceObjectAtIndex:0 withObject:@"what"];
//結果:mArray5 = @[@"what", @"do", @"you"]

//交換位置
[mArray5 exchangeObjectAtIndex:0 withObjectAtIndex:1]
//結果:mArray5 = @[@"do", @"what", @"you"]

三秸苗、字典

1、不可變字典

#創(chuàng)建字典#
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"one", @"1", @"two", @"2", @"three", @"3",  nil];
#鍵值對的個數(shù)#
NSInteger count = [dict count];
#通過鍵查找值#
NSString *str = [dict objectForKey:@"two”];
NSString *str = dict[@"two”];
#遍歷#
for (NSString *str in dict) {
        NSLog(@"%@", str);      //遍歷的是字典dict中的鍵
        NSLog(@"%@", dict[str]);//遍歷的是字典dict中的值
}

2运褪、可變字典

#創(chuàng)建可變字典#
NSMutableDictionary * mdict = [[NSMutableDictionary alloc] initWithDictionary:dict];
#增加鍵值對#
[mdict setObject:@"four" forKey:@"4"];
#刪除鍵值對#
[mdict5 removeObjectForKey:@“4”];//刪除單個鍵
[mdict5 removeAllObjects];//全部刪除
#字典替換#
NSDictionary * dict = @{@“3”:@“xixi", @"2":@"gogo"};
[mdict setDictionary:dict];//把字典mdict全部改為字典dict
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末惊楼,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子秸讹,更是在濱河造成了極大的恐慌檀咙,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嗦枢,死亡現(xiàn)場離奇詭異攀芯,居然都是意外死亡,警方通過查閱死者的電腦和手機文虏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門侣诺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來殖演,“玉大人,你說我怎么就攤上這事年鸳∨烤茫” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵搔确,是天一觀的道長彼棍。 經常有香客問我,道長膳算,這世上最難降的妖魔是什么座硕? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮涕蜂,結果婚禮上华匾,老公的妹妹穿的比我還像新娘。我一直安慰自己机隙,他們只是感情好蜘拉,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著有鹿,像睡著了一般旭旭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上葱跋,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天持寄,我揣著相機與錄音,去河邊找鬼年局。 笑死际看,一個胖子當著我的面吹牛,可吹牛的內容都是我干的矢否。 我是一名探鬼主播仲闽,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼僵朗!你這毒婦竟也來了赖欣?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤验庙,失蹤者是張志新(化名)和其女友劉穎顶吮,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體粪薛,經...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡悴了,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片湃交。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡熟空,死狀恐怖,靈堂內的尸體忽然破棺而出搞莺,到底是詐尸還是另有隱情息罗,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布才沧,位于F島的核電站迈喉,受9級特大地震影響,放射性物質發(fā)生泄漏温圆。R本人自食惡果不足惜挨摸,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望捌木。 院中可真熱鬧油坝,春花似錦嫉戚、人聲如沸刨裆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽帆啃。三九已至,卻和暖如春窍帝,著一層夾襖步出監(jiān)牢的瞬間努潘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工坤学, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留疯坤,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓深浮,卻偏偏與公主長得像压怠,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子飞苇,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內容