关于NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary
? ? ? NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary 在 OC 中我們天天都要用,而我們要怎么學習它們呢?
? ? ? 我覺得學習這些經常使用的類,使用類比的方法即可了,僅僅要學會一個類,就能類比另外兩個類了..
? ? ? 比方,NSString和NSMutableString的可變與不可變,主要區分的標準是,它們本身是不是能夠變,可變的一般都能增,刪,改,而不可變的方法主要是創建,查看,得到元素個數或者長度等等;
? ? ? ? ??命名規則
NSString
第一,創建 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?initWithFormat或者stringWithFormat
第二,求字符串長度 ? ? ? ? ? ? ? ? ? ? ?length
第三,推斷字符串是否相等 ? ? ? ? ? isEqualToString
第四,字符串替換 ? ? ? ? ? ? ? ? ? ? ? ? stringByReplacingOccurrenceOfString:withString:
第五,字符串比較 ? ? ? ? ? ? ? ? ? ? ? ? compare ?返回值枚舉
第六,字符串拼接 ? ? ? ? ? ? ? ? ? ? ? ? stringByAppendingFormat
第七,求字符 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?substringFromlndex
第八,查詢字符串 ? ? ? ? ? ? ? ? ? ? ? ? rangeOfString
第九,前綴,后綴 ? ? ? ? ? ? ? ? ? ? ? ? ? ?hasPrefix ? ?hasSuffix
第十,字母大寫 ? ? ? ? ? ? ? ? ? ? ? ? ? ? uppercaseString
NSMutableString ? ? 可變字符串
第一,創建 ?使用父類繼承的方法或者使用自己的創建方法 ? ? ?initWithCapacity
第二, 增 ? ? ? ? insertString:atlndex: ? ? ? ?/
第三, 刪 ? ? ? ? deleteCharacterslnRange:
第四, 改 ? ? ? ? replaceCharacterslnRange:
NSArray ? ? ?容器 ?僅僅能存儲對象(繼承自NSObject的類) 不可變的數組 不能加入 不能刪除,不能改動,?
第一,創建 ? ? ? ? ? ? ?initWithObjects: ? ? arrayWithObjects:
第二,看元素 ? ? ? ? ?,objectAtIndex:
第三,數組個數, ? ? ?[array count]
第四,排序 ? ? ? ? ? ? ?[sortedArrayUsingSelector: ] 比較字符串直接用compare,假設比較person,就須要重寫比較方法
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[sortArrayUsingComparator: ]block作為參數排序,優先使用這個
NSMutableArray ? ?可變字符串
第一,增(加入), ? ? ? addObject
第二,刪 ? ? ? ? ? ? ? ? ?removeObjectAtIndex: ? ? ? ? ? removeObject ? ? ? removeAllObjects
第三,插入 ? ? ? ? ? ? ?insertObject: ?atIndex:
第四,替換 ? ? ? ? ? ? ?replaceObjectAtlndex: withObject:
第五,交換 ? ? ? ? ? ? ?exchangeObjectAtIndex:(目的串) ? ? withObjectAtIndex:(來源串)
NSDictionary ? 不可變字典
第一,創建 ? ? ? ? ? ? ? ? ? ??+?dictionaryWithObject:forKey: ?–?initWithObjectsAndKeys:
第二,看鍵值對個數 ? ? ? - count
第三,通過鍵獲取值 ? ? ? -objectForKey;
NSMutableDictionary ?可變字典
第一,創建 ? ? ? ? ? ? ? ? ? ? ?initWithCapacity
第二,加入(增) ? ? ? ? ? ? ? ?setObject: ?forKey:
第三,刪 ? ? ? ? ? ? ? ? ? ? ? ? ? removeObjectForKey:
第四,改 ? ? ? ? ? ? ? ? ? ? ? ? ?setObject: forKey;
collection ? 集合(容器)
包含 Array(有序容器) ? Dictionary(無序容器) ? set(集合) ?
NSValue , NSNumber 容器,轉換作用
NSValue ? ? 轉換結構體為對象
NSNumber 轉換基本數據類型為對象
NSSet集合 ? ? 無序并且不重 ? ? ?能夠用來排除反復 ?
??
另一個能夠用來計算反復多少次的子類
使用 —s復數的方法 ? 一般返回值是 ? 用新建的數組存起來
#pragma mark -----------創建NSCountedSet 能記錄元素的反復次數
? ? ? ? NSCountedSet * countSet = [NSCountedSet setWithObjects:@"1",@"2",@"3",@"1",@"1",nil];
? ? ? ? NSLog(@"countForObject = %ld",[countSet countForObject:@"1"]);
#pragma mark -----------高速枚舉(遍歷)
//? ? ? ? for (<#type *object#> in <#collection#>) {
//? ? ? ? ? ? <#statements#>
//? ? ? ? }
? ? ? ? //數組 上面object是元素 ? //字典 上面object是key
近期關于封裝,也有一些新的認識,詳細例如以下:就是把main函數里面的代碼封裝到還有一個新的類.然后在main函數里面調用方法即可了
就像C語言的函數一樣
假設有兩個或兩個以上初始化方法,就用指定初始化方法,指定初始化方法,以參數多的那個為指定方法.
轉載于:https://www.cnblogs.com/brucemengbm/p/6869419.html
總結
以上是生活随笔為你收集整理的关于NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: angularJS 全选反选批量删除
- 下一篇: 如何解决win8系统下卸载软件出现错误代