純屬手打 自己摸索 ??如果有錯誤 歡迎指出 希望對你們有用
/**************** Mutable Array 這里是NSArray和Array通用的方法 ****************/
在數組的末位增加元素
open func add(_ anObject: Any)
在固定的地方給數組里增加元素
open func insert(_ anObject: Any, at index: Int)
刪除數組中最后1個元素
open func removeLastObject()
刪除數組中指定的元素
open func removeObject(at index: Int)
更改數組中指定位置元素的值
open func replaceObject(at index: Int, with anObject: Any)
正常初始化
public init()
創建一個預估數組長度的數組初始化
public init(capacity numItems: Int)
初始化 遵循NSCoding協議吧 具體我也沒研究出來 因為不常用就占時放棄了 等有空了再說吧
public init?(coder aDecoder: NSCoder)
/** 可變數組專屬方法 */
添加多個數組元素
open func addObjects(from otherArray: [Any])
將數組指定位置元素調換位置
open func exchangeObject(at idx1: Int, withObjectAt idx2: Int)
刪除所有元素
open func removeAllObjects()
刪除指定元素 從第數組的第幾位開始 往下尋找幾位 如果有就刪除 沒有就算
open func remove(_ anObject: Any, in range: NSRange)
刪除指定元素
open func remove(_ anObject: Any)
這里刪除指定元素 且在NSrange規定里查找 這里測試 字符串數組無用,字符串數組只能寫為Array[0] 直接刪除才可以 而int類型的數組可以
open func removeObject(identicalTo anObject: Any, in range: NSRange)
直接刪除指定元素 字符串數組同上不行 數字數組可以 摸索了半天 估計是個坑
open func removeObject(identicalTo anObject: Any)
刪除數組元素
open func removeObjects(in otherArray: [Any])
刪除指定位置的元素 具體參考NSRange 或者看我代碼
open func removeObjects(in range: NSRange)
/** 以下的方法我也不知道常用不常用 反正我是查了資料暫時沒找到準確的也沒試出來 等我有空了再補齊吧 ?? */
open func replaceObjects(in range: NSRange, withObjectsFrom otherArray: [Any], range otherRange: NSRange)
open func replaceObjects(in range: NSRange, withObjectsFrom otherArray: [Any])
open func setArray(_ otherArray: [Any])
open func sort(_ compare: @convention(c) (Any, Any, UnsafeMutableRawPointer?) -> Int, context: UnsafeMutableRawPointer?)
open func sort(using comparator: Selector)
open func insert(_ objects: [Any], at indexes: IndexSet)
open func removeObjects(at indexes: IndexSet)
open func replaceObjects(at indexes: IndexSet, with objects: [Any])
話不多說 下面直接上我寫的代碼 給大家借鑒下??
可變數組的初始化
var cccc = NSMutableArray.init(coder: )
創建了1個預估有2個元素組成的可變數組
var ssss = NSMutableArray.init(capacity: 2)
數組初始化 并且賦值
var aaaa = NSMutableArray.init(array: ["ssss","aaaa","cccc"])
可變數組初始化
var dddd = NSMutableArray.init()
/** 可變數組 - 不可變數組 --> 通用的方法 */
var namesArr = NSMutableArray.init()
namesArr = ["dddd","2222","aaass"]
//訪問第一個元素和最后一個元素
print("第一個元素:\(String(describing: namesArr.firstObject))")
print("最后一個元素:\(String(describing: namesArr.lastObject))")
//這里修改了數組第3位的元素值
namesArr[2] = "3333"
//在數組的末尾增加一個元素
namesArr.add("4444")
//在第二元素位置上增加1個元素
namesArr.insert("hahah", at: 1)
//刪除數組中最后1個元素
namesArr.removeLastObject();
//刪除數組中指定位置的元素
namesArr.removeObject(at: 1)
print(namesArr)
//更改第一位元素的值
namesArr.replaceObject(at: 0, with: "XXXXX")
print(namesArr)
//刪除所有的元素
namesArr.removeAllObjects()
/** 可變數組專屬方法 */
let StudentsArr = NSMutableArray.init(array: ["濤濤","耗子","飛飛","蕾蕾"])
//添加多個數組元素
StudentsArr.addObjects(from: ["洋洋","超超"])
print("數組里的屬于=\(StudentsArr)")
for value in StudentsArr {
print("1\(value)")
}
//這里講第1個位置的元素和第0個位置的元素進行了調換
StudentsArr.exchangeObject(at: 1, withObjectAt: 0)
for value in StudentsArr {
print("2\(value)")
}
//刪除指定元素 從第數組的第3位開始 往下尋找2位 如果有就刪除 沒有就算
StudentsArr.remove("飛飛", in: NSMakeRange(3, 2))
for value in StudentsArr {
print("```\(value)")
}
//刪除了111 從第數組的第0位開始 往下尋找2位 如果有就刪除 沒有就算
let NumArray = NSMutableArray.init(array: [111,222,333,444,555])
NumArray.removeObject(identicalTo:111, in: NSMakeRange(0, 1))
for value in NumArray {
print("~~\(value)")
}
//刪除指定元素 字符串數組不行
NumArray.removeObject(identicalTo:222)
for value in NumArray {
print("**\(value)")
}
//刪除數組元素
StudentsArr.removeObjects(in:["耗子","濤濤"])
for value in StudentsArr {
print("~~\(value)")
}
//這里刪除第一個元素開始往下的2個位置的元素
StudentsArr.removeObjects(in: NSMakeRange(0, 2))
for value in StudentsArr {
print("111\(value)")
}
/** 創建不可變的數組 */
let vowels = ["M","I","S","S"]
print(vowels)
}
/** 頁面將要顯示的時候做的數據處理 */
override func viewWillAppear(_ animated: Bool) {
//創建一個數組 調用下面的函數 并且打印出數組中的最小的值和最大的值
let bounds = minMax(array: [8, -6, 2, 109, 3, 71])
print("min is \(bounds.min) and max is \(bounds.max)")
}
/** 找出最大值和最小值 */
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
雙擊666