1. 程式人生 > >NSString/NSArray/NSDictionary/NSSet方法的總結

NSString/NSArray/NSDictionary/NSSet方法的總結

NSString

  1. 建立
    NSString *[email protected]”test”;
    NSString *str1=[[NSString alloc]initWithString:str];
    NSString *str2=[[NSString alloc]initWithUTF8String:”test”];
    NSString *str3=[[NSString alloc]initWithFormat:@”str1”];
  2. 比較 isEqualToString
    BOOL result= [str isEqualToString;str1];
    NSLog(@”結果是%d”,result);
  3. 查詢字串 rangeOfString:
    NSString *[email protected]”u a the A o “;
    NSString *[email protected]”the”;
    NSRange range=[str7 rangeOfString:str8];
    NSLog(@”%lu %lu”,range.location,range.length);//——>結果是4 3
  4. 擷取
    NSSting *[email protected]”0123456789”;
    //從頭開始擷取到指定的位置
    NSString *str1=[str substringToIndex:6];//——>結果是012345
    //從指定的位置(不包括指定的位置)開始擷取到最後一位
    NSString *str2=[str substringFromIndex:6];//——>結果是6789
    //從第幾位開始(不包括指定位置)擷取幾位
    NSString *str3=[str substringWithRange:NSMakeRange(3,4)];//——>結果是3456
  5. 大小寫轉化
    NSString *[email protected]”I Love You”;
    NSString *strUpperCase=[string uppercaseString];//——>結果是I LOVE YOU
    NSString *strLowerCase=[string lowercaseString];//——>結果是i love you

NSMutableString
-(void)createNSMutableString
{
NSString *[email protected]”test”;
NSMutableString *mutableStr=[NSMutableString stringWithString:str];
//修改
[mutableStr setString:@”update”];
NSLog(@”%@”,mutableStr); //——>結果是update
//追加
[mutableStr appendString:@”str”];
NSLog(@”%@”,mutableStr); //——>結果是updatestr
[mutableStr appendFormat:@”str%@”,str];
NSLog(@”%@”,mutableStr); //——>結果是updatestrstrtest
//插入
[mutableStr insertString:@”insert” atIndex:4];
NSLog(@”%@”,mutableStr); //——>結果是updainserttestrstrtes
//修改
[mutableStr replaceCharactersInRange:NSMakeRange(1, 1) withString:@”K”];
NSLog(@”%@”,mutableStr); //——>結果是uKdainserttestrstrtes
//刪除
[mutableStr deleteCharactersInRange:NSMakeRange(1, 1)];
NSLog(@”%@”,mutableStr); //——>結果是udainserttestrstrtes
}

NSArray

-(void)createArray
{
//建立陣列
NSArray *array=[[NSArray alloc]initWithObjects:@”one”,@”two”,@”three”, nil];
NSArray *array1=[array arrayWithObjects:@”one”,@”two”,@”three”, nil];
NSArray *[email protected][@”one”,@”two”,@”three”];
NSLog(@”%@ %@ %@”,array,array1,array2);

//遍歷陣列
//for-in
id obj;
for(obj in array)
{
NSLog(@”obj====%@”,obj);
}
//用列舉器遍歷
NSEnumerator *enumerator=[array objectEnumerator];
while(obj=[enumerator nextObject])
{
NSLog(@”obj====%@”,obj);
}
//用列舉器逆序遍歷
NSEnumerator *enumerator1=[array reverseObjectEnumerator];
while(obj=[enumerator nextObject])
{
NSLog(@”obj====%@”,obj);
}
}

NSMutableArray

-(void)createMutable
{
NSMutableArray *array=[[NSMutableArray alloc]init];
//新增元素
[array addObject:@”one”];
[array addObject:@”two”];
[ array addObject:@”three”];

//NSString 與 NSArray 互轉
NSString *[email protected]"1,2,3,4,5";
NSArray *arr=[str componentsSeparatedByString:@","];
NSLog(@"%@",arr);

NSString *str1=[arr componentsJoinedByString:@"!"];
NSLog(@"str1==%@",str1);

}
NSDictionary
-(void)createNSDictionary
{
//建立字典
NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:@”one”,@”1”,@”two”,@”2”,@”three”,@”3” ,nil];
NSDictionary *dict1=[NSDictionary dictionaryWithObjectsAndKeys:@”one”,@”1”,@”two”,@”2”,@”three”,@”3” ,nil];
NSDictionary *[email protected]{@”one”:@”1”,@”two”:@”2”,@”three”:@”3”}; //注意這是新特性,字典是@{},陣列的是@[];
NSLog(@”dict–%@ %@ %@”,dict,dict1,dict2);

id obj;
for (obj in dict) {
    //key
    NSLog(@"obj===%@",obj);
    //value
    NSLog(@"value====%@",[dict objectForKey:obj]);
}
//value
NSEnumerator *enumerator=[dict objectEnumerator];
while (obj=[enumerator nextObject]) {
    NSLog(@"obj enum======%@",obj);
}
//key
NSEnumerator *enumerator1=[dict keyEnumerator];
while (obj=[enumerator1 nextObject]) {
    NSLog(@"obj eunm1======%@",obj);
}

}

NSMutableDictionary
-(void)createMutableDictionary
{
//建立
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
//新增
[dict setValue:@”value1” forKey:@”key1”];
[dict setValue:@”value2” forKey:@”key2”];
[dict setValue:@”value3” forKey:@”key3”];
[dict setValue:@”value4” forKey:@”key4”];
NSLog(@”dict===%@”,dict);

//刪除
[dict removeObjectForKey:@"key3"];
 NSLog(@"dict===%@",dict);

//2種都可以,但是valueForKey在[email protected]"@key"情況下,系統會把@符號去掉,這樣會crash,建議用objectForKey
NSLog(@"value====%@",[dict objectForKey:@"key2"]);
NSLog(@"value====%@",[dict valueForKey:@"key2"]);

NSLog(@"all key==%@",[dict allKeys]);
NSLog(@"all value==%@",[dict allValues]);  

}
NSSet
-(void)createNSSet
{
//建立
NSSet *set=[[NSSet alloc]initWithObjects:@”one”,@”two”,@”three”, nil];
NSLog(@”set====%@”,set);

//判斷是否包含某個物件
BOOL judgeSet=[set containsObject:@"three"];
BOOL judgeSet1=[set containsObject:@"Three"];
NSLog(@"bool ===%d  %d",judgeSet,judgeSet1);

//以陣列的形式存放資料
NSArray *[email protected][@"1",@"2",@"3"];
NSSet *set1=[NSSet setWithArray:array];
NSLog(@"%@   %@",array,set1);


NSSet *stu=[[NSSet alloc]initWithObjects:@"25",@"male",@"student" ,nil];
NSSet *stu1=[[NSSet alloc]initWithObjects:@"25",@"male",@"student",@"cool", nil];
NSLog(@"stu count ===%lu",(unsigned long)[stu count]);
//判斷是否包含
if ([stu containsObject:@"cool"]) {
    NSLog(@"include cool");
}
//判斷是否相等
if ([stu isEqualToSet:stu1]) {
    NSLog(@"!=");
}
//判斷是不是其子串
if ([stu isSubsetOfSet:stu1]) {
    NSLog(@"is sub");
}
//遍歷的方法
//1.for-in
id obj;
for (obj in stu1) {
    NSLog(@"%@",obj);
}
//2.列舉器
NSEnumerator *enumerator=[stu1 objectEnumerator];
while (obj=[enumerator nextObject]) {
    NSLog(@"%@",obj);
}

NSArray *stuArray=[stu allObjects];
NSLog(@"set===%@,array===%@",stu1,stuArray);

}
NSMutableSet
-(void)createNSMutableSet
{
//建立
NSMutableSet *set=[[NSMutableSet alloc]init];
[set addObject:@”1”];
[set addObject:@”2”];
[set addObject:@”3”];
[set addObject:@”4”];
[set addObject:@”8”];
NSLog(@”%@”,set);
//移除某個物件
[set removeObject:@”1”];
NSLog(@”%@”,set);
//移除所有物件
[set removeAllObjects];
//NSLog(@”%@”,set);

NSMutableSet *set1=[[NSMutableSet alloc]init];
[set1 addObject:@"1"];
[set1 addObject:@"2"];
[set1 addObject:@"3"];
[set1 addObject:@"4"];
//合併

[set unionSet:set1];
NSLog(@”set==%@ set1==%@”,set,set1);
// 去除
[set minusSet:set1];
NSLog(@”set==%@ set1==%@”,set,set1);
//交集
[set intersectSet:set1];
NSLog(@”set==%@ set1==%@”,set,set1);

}

相關推薦

NSString/NSArray/NSDictionary/NSSet方法總結

NSString 建立 NSString *[email protected]”test”; NSString *str1=[[NSString alloc]initWithString:str]; NSString *str2=[[NSSt

NSArray,NSDictionary,NSSet用法及區別

-NSArray1、初始化、取值等基礎操作          NSArray 是不可變陣列,一旦建立完成就不能夠對陣列進行,新增,刪除等操作  NSArray * array1 = [NSArrayarrayWithObjects:str1,str2,str3,str1,nil];      在建立一個N

OC字典(NSDictionary)和集合(NSSet)的總結

1.不可變字典 // 字典裡的元素是無序 1.1 建立不可變字典 (初始化方法,便利構造器) // 初始化方法(值在前,鍵在後) NSDictionary *dic1 = [[NSD

javascript字符串方法總結

大小寫 comm 其他 tolower 匹配 一行 ror 運算 轉換 一、單引號字符串內部可以使用雙引號,雙引號字符串內部也可以使用單引號 "hello ‘world‘" ‘welcome "to" js‘ 二、多行和轉義 如果要在單引號字符串的內部,使用單引號(或者

web測試中的測試點和測試方法總結

動態 小數 圖片尺寸 提示信息 方便 margin style 容錯性 字符型 測試是一種思維,包括情感思維和智力思維,情感思維主要體現在一句俗語:思想決定行動上(要懷疑一切),智力思維主要體現在測試用例的設計上。具有了這樣的思想,就會找出更多的bug。 一、輸入框

C# Winform 跨線程更新UI控件常用方法總結(轉)

sum tex ase adc 而是 this obj 出現 turn 出處:http://www.tuicool.com/articles/FNzURb 概述 C#Winform編程中,跨線程直接更新UI控件的做法是不正確的,會時常出現“線程間操作無效: 從不是創建控件的

ECMAScript面向對象(二)——之創建對象方法總結

擴展 console 動態 原型 struct 私有屬性 true asc 一份 創建對象的方法 工廠模式 缺點:無法識別對象的類型,因為根本沒有定義新的對象類型 // 工廠模式創建對象 //定義 function createPerson(name,age,

day2 字符串常用方法總結

mes 一個 並且 lun int() join() eba false 換行符 字符串在Python中是常用的功能,我們知道,字符串在Python中存儲的形式是以字符數組的形式存在,比如"alex"在內存中的存儲形式是:["a","l","e","x"],因為我們

zabbix   監控平臺搭建過程中的報錯與解決方法總結

監控 zabbix 運維自動化1.php option post_max_size 2.php option max_execution_time 3.php option max_input_time 4.php time zone 5.php bcm

C# 各種導出的方法總結

src view str inf object ret temp ksh 驅動程序 第一種:使用 Microsoft.Office.Interop.Excel.dll 首先需要安裝 office 的 excel,然後再找到 Microsoft.Office.Interop.

python os模塊功能和方法總結

isp 通用 工作 相同 使用 結束 所有 erro 大量 1 os.sep 可以取代操作系統特定的路徑分割符 2 os.linesep 字符串給出當前平臺使用的行終止符。例如,Windows使用‘\r\n‘,Linux使用‘\n‘ 而Mac使用‘\r‘。

display:inline-block; 去除間隙的方法 總結

remove col rdp 設備 狀態 zha targe style div 個人常用: 如: <ul> <li><a href="#" >實時數據</a></li> <li>&l

C#把datetime類型的日期轉化成年月日或其他格式方法總結

localtime ash diff time() com color 月份 大小 -s 日期格式:yyyyMMdd HH:mm:ss(註意此字符串的字母大小寫很嚴格) yyyy:代表年份 MM: 代表月份 dd: 代表天 HH: 代表小時(24小時制) mm:

shell 輸出100個+方法總結

shell 輸出100個+方法總結python -c "print(‘+‘*100)"head -c 100 /dev/zero |awk ‘gsub(/./,"+")‘perl -le "print(q(+)x100)"println("+"*100)輸出50個=的方法echo "" | sed ‘:a;

接口、抽象類、抽象方法、虛方法總結

blog 方法 實例 類名 class 訪問修飾符 檢查 spa code 一、接口   1、定義     1.1、訪問修飾符 interface 接口名{成員可以為屬性、方法、事件、索引器}     1.2、示例代碼     public delegate void D

C# 各種導入 Excel 文件的數據的方法總結

占用 guid 保存 null amp nbsp 字符串 count str 在導入之前都需要將上傳的文件保存到服務器,所以避免重復的寫這些代碼,先貼出上傳文件並保存到服務器指定路徑的代碼。 protected void btnImport_Click(object s

數組去重的方法總結

doctype 是否 bsp cti fun [0 logs war href <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> &

計算兩張圖片相似度的方法總結

title rac 相似度 無法 tween hive any 明顯 embed python工具包-pyssim 簡介 python工具包,用來計算圖像之間的結構相似性 (Structural Similarity Image Metric: SSIM)。結構相似性介紹

JQuery控制radio選中和不選中方法總結

use 獲取 class rop als trade val pro put 一、設置選中方法 代碼如下: $("input[name=‘名字‘]").get(0).checked=true; $("input[name=‘名字‘]").attr(‘checked‘,

C#將Word轉換成PDF方法總結(基於Office和WPS兩種方案)

path ebs htm soft off ros exc 標題 總結  有時候,我們需要在線上預覽word文檔,當然我們可以用NPOI抽出Word中的文字和表格,然後顯示到網頁上面,但是這樣會丟失掉Word中原有的格式和圖片。一個比較好的辦法就是將word轉換成pdf,然