iOS - Swift Set 集合


前言

  • Set:集合

    	public struct Set<Element : Hashable> : Hashable, CollectionType, ArrayLiteralConvertible
    	public class NSSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration
    
    • 集 NSSet 是一個無序對象的集合。集也有動態和靜態之分,NSSet 是靜態類型,成員不能被修改。NSMutableSet 是動態類型,成員可以被修改。

    • 集和數組不一樣的是,它存儲的每一個元素都是唯一的,沒有完全相同的兩個元素。所以數組中如果有兩個相同的值,轉換為集后只會有一個值。

  • NSCountedSet:計數集合

    	public class NSCountedSet : NSMutableSet
    
    • NSCountedSet 這種集合中同一對象可以出現多次,然而並非在集合中存放多次這個對象,而是維護一個次數計數。當第一次將對象添加到集合中時,對象的 count 值被置為 1,然后每次將這個對象添加到集合中,count 值就會增加 1,每次從集合中刪除對象,count 值就會減 1.當對象的 count 值為零時,實際上對象本身就被刪除了。
  • NSIndexSet:索引集合

    	public class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding
    	public class NSMutableIndexSet : NSIndexSet
    
    • NSIndexSet 這種集合用於存儲有序的索引到某種數據結構,比如數組。

1、集合的 創建

	// Set 型集合
		    
		// 不賦初值,指定數據類型為 String 型
		let set1 = Set<String>()
		    
		// 不指定數據類型,自動推斷
		let set2:Set = [1, 3, 5, 7]
		    
		// 指定數據類型為 Int 型
		let set3:Set<Int> = [1, 3, 5, 7]
			
	// NSSet 型集合
		    
		let swiftArr:Array = ["a", "b", "c"]
		let swiftSet:Set = [1, 2, 3, 4]
		    
		// 創建一個空集合
		let nsSet1:NSSet = NSSet()
		    
		// 指定多個元素值
		let nsSet2:NSSet = [2, 4, 6, 8]
		    
		// 由 Array 型數組創建
		let nsSet3:NSSet = NSSet(array:swiftArr)
		    
		// 由 Set 型集合創建
		let nsSet4:NSSet = NSSet(set: swiftSet)
		    
		// 由 NSSet 型集合創建
		let nsSet5:NSSet = NSSet(set: nsSet3)
		    
		// 指定一個元素值
		let nsSet6:NSSet = NSSet(object: 7)
		    
		// 指定多個元素值
		let nsSet7:NSSet = NSSet(objects: 2, 4, 6, 8)

2、集合成員 個數的計算

	// Set 或 NSSet 型集合
		
		let set:NSSet = [1, 3, 5, 7]
	    
		let num:Int = set.count

3、集合元素的獲取

	// NSSet 型集合
		    
		let nsSet:NSSet = [2, 4, 6, 8]
		    
		// 獲取集合中指定的元素,不存在時返回 nil
		let object1:AnyObject? = nsSet.member(8)
		  	
		// 獲取集合中隨機的一個值,集合為空時返回 nil
		let objects1:AnyObject? = nsSet.anyObject()
		  	
		// 獲取集合中所有的元素
		let objects2:AnyObject = nsSet.allObjects

4、集合的比較

	// Set 型集合
		    
		let swiftSet1:Set = [1, 3, 5, 7]
		let swiftSet2:Set = [5, 7, 9, 11]
		    
		// 判斷集合是否為空
		let bl1:Bool = swiftSet1.isEmpty
		    
		// 判斷集合中是否包含某個元素
		let bl2:Bool = swiftSet1.contains(7)
		    
		// 相等,判斷兩個集合是否相等
		let bl3:Bool = swiftSet1 == swiftSet2
		    
		// 子集,判斷當前集合是否是指定集合的子集
		let bl4:Bool = swiftSet1.isSubsetOf(swiftSet2)
		    
		// 交集,返回當前集合與指定集合的交集,沒有交集時返回一個空集合
		let interSet:Set = swiftSet1.intersect(swiftSet2)
		    
		// 並集,返回當前集合與指定集合的並集
		let unionSet:Set = swiftSet1.union(swiftSet2)
		  	
	// NSSet 型集合
		    
		let nsSet1:NSSet = [2, 4, 6, 8]
		let nsSet2:NSSet = [6, 8, 10, 12]
		let nsSet3:NSMutableSet = [6, 8, 10, 12]
		    
		// 判斷集合中是否包含某個元素
		let bl5:Bool = nsSet1.containsObject(8)
		    
		// 相等,判斷兩個集合是否相等
		let bl6:Bool = nsSet1 == nsSet2
		        
		// 相等,判斷兩個集合是否相等
		let bl7:Bool = nsSet1.isEqualToSet(nsSet2 as Set)
		    
		// 子集,判斷當前集合是否是指定集合的子集
		let bl8:Bool = nsSet1.isSubsetOfSet(nsSet2 as Set)
		    
		// 交集,判斷當前集合與指定集合是否有交集
		let bl9:Bool = nsSet1.intersectsSet(nsSet2 as Set)
		   	
		// 交集,返回當前集合與指定集合的交集,返回到當前集合中
		nsSet3.intersectSet(nsSet1 as Set)
		  	
		// 並集,返回當前集合與指定集合的並集,返回到當前集合中
		nsSet3.unionSet(nsSet1 as Set)
		   	
		// 補集,返回當前集合與指定集合的補集,返回到當前集合中,當前集合中刪除與指定集合的交集
		nsSet3.minusSet(nsSet1 as Set)

5、集合元素的添加

	// Set 型集合
		    
		var swiftSet:Set = [1, 3, 5, 7]
		    
		// 向集合中添加一個元素
		swiftSet.insert(8)                                                  
		  	
	// NSSet 型集合
		    
		let nsSet:NSMutableSet = [2, 4, 6, 8]
		    
		// 向集合中添加一個元素
		nsSet.addObject(10)                                                 
		  	
		// 向集合中添加一個數組
		nsSet.addObjectsFromArray([11, 12, 12])                             

6、集合元素的刪除

	// Set 型集合
		    
		var swiftSet:Set = [1, 3, 5, 7]
		    
		// 刪除第一個元素
		swiftSet.removeFirst()                                              
		  	
		// 刪除指定元素
		swiftSet.remove(1)                                                  
		  	
		// 刪除所有元素
		swiftSet.removeAll(keepCapacity: true)                             
		  	
	// NSSet 型集合
		    
		let nsSet:NSMutableSet = [2, 4, 6, 8]
		    
		// 刪除指定元素
		nsSet.removeObject(4)                                               
		  	
		// 刪除指定集合,當前集合中刪除與指定集合的交集
		nsSet.minusSet([2, 6])                                              
		  	
		// 刪除所有元素
		nsSet.removeAllObjects()                                            

7、集合元素的修改

	// NSSet 型集合
		    
		let nsSet:NSMutableSet = [2, 4, 6, 8]
		    
		nsSet.setSet([1, 2, 3])

8、集合的遍歷

	// NSSet 型集合
		    
		let nsSet:NSSet = [2, 4, 6, 8]
		    
		// 用閉包遍歷
			
			nsSet.enumerateObjectsUsingBlock { (obj:AnyObject, stop:UnsafeMutablePointer<ObjCBool>) in
			    
				print(obj)
			}
			    
		// 用迭代遍歷
			
			let enu:NSEnumerator = nsSet.objectEnumerator()
				    
				// AnyObject 后不要加 ?,否則會導致一直循環
				while let obj:AnyObject = enu.nextObject() {
				    
				print(obj)
			}
			    
		// 條件遍歷
			
			let set:Set = nsSet.objectsPassingTest { (obj:AnyObject, stop:UnsafeMutablePointer<ObjCBool>) -> Bool in
			    
				return (obj as! Int) > 5 ? true : false
			}
			
			print(set)

9、計數集合的創建

	let nsCountSet1:NSCountedSet = NSCountedSet()
	let nsCountSet2:NSCountedSet = NSCountedSet(capacity: 0)
	    
	let nsCountSet3:NSCountedSet = NSCountedSet(array: [1, 3, 5, 7, 9])
	    
	let nsCountSet4:NSCountedSet = NSCountedSet(set: [6, 9])
	    
	let nsCountSet5:NSCountedSet = NSCountedSet(object: 8)
	let nsCountSet6:NSCountedSet = NSCountedSet(objects: [2, 3, 6, 8], count: 4)

10、計數集合元素的添加

	let nsCountSet:NSCountedSet = [1, 3, 5, 7, 9]
	    
	// 向集合中多次添加同一個元素,該元素的計數值增加
	nsCountSet.addObject(6)

11、計數集合元素的刪除

	let nsCountSet:NSCountedSet = [1, 3, 5, 7, 9]
	    
	// 從集合中刪除元素,該元素的計數值減小
	nsCountSet.removeObject(5)                                          

12、計數集合計數值的計算

	let nsCountedSet:NSCountedSet = NSCountedSet(array: [1, 2, 3])
	    
	nsCountedSet.addObject(2)
	nsCountedSet.removeObject(1)
	    
	// 檢索集合中指定元素的計數值
	let num1:Int = nsCountedSet.countForObject(1)
	    
	// 檢索集合中指定元素的計數值
	let num2:Int = nsCountedSet.countForObject(2)

13、索引集合的創建

	let indexSet1:NSIndexSet = NSIndexSet()
	    
	let indexSet2:NSIndexSet = NSIndexSet(index: 8)
	    
	let indexSet3:NSIndexSet = NSIndexSet(indexSet: indexSet2)
	    
	let indexSet4:NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 8))

14、索引集合元素數量的計算

	let indexSet:NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 8))
	    
	// 計算索引集合所有索引的數量
	let num1:Int = indexSet.count
	
	// 計算索引集合中某一范圍內的索引的數量
	let num2:Int = indexSet.countOfIndexesInRange(NSMakeRange(3, 4))

15、索引集合元素的獲取

	let indexSet:NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 8))
	    
	// 獲取索引集合中的第一個索引,如果集合為空則返回 NSNotFound
	let index1:Int = indexSet.firstIndex
	
	// 獲取索引集合的最后一個索引,如果集合為空則返回 NSNotFound
	let index2:Int = indexSet.lastIndex
	
	// 獲取索引集合中小於指定的索引且最接近的索引,如果沒有小於的索引則返回 NSNotFound
	let index3:Int = indexSet.indexLessThanIndex(5)
	
	// 獲取索引集合中小於等於指定的索引且最接近的索引,如果沒有小於的索引則返回 NSNotFound
	let index4:Int = indexSet.indexLessThanOrEqualToIndex(5)
	
	// 獲取索引集合中大於指定的索引且最接近的索引,如果沒有大於的索引則返回 NSNotFound
	let index5:Int = indexSet.indexGreaterThanIndex(7)
	
	// 獲取索引集合中大於等於指定的索引且最接近的索引,如果沒有大於的索引則返回 NSNotFound
	let index6:Int = indexSet.indexGreaterThanOrEqualToIndex(7)

16、索引集合的判斷

	let indexSet:NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 8))
	    
	// 判斷索引集合中是否包含某個索引
	let bl1:Bool = indexSet.containsIndex(9)
	
	// 判斷一個索引集合中是否包含另一個索引集合
	let bl2:Bool = indexSet.containsIndexes(NSIndexSet(indexesInRange: NSMakeRange(2, 3)))
	
	// 判斷一個索引集合中是否包含某一個范圍內的所有值
	let bl3:Bool = indexSet.containsIndexesInRange(NSMakeRange(2, 3))
	
	// 判斷一個索引集合中是否包含某一個范圍內的值(是否有交集)
	let bl4:Bool = indexSet.intersectsIndexesInRange(NSMakeRange(7, 3))
	
	// 判斷兩個索引集合是否相等
	let bl5:Bool = indexSet.isEqualToIndexSet(NSIndexSet(indexesInRange: NSMakeRange(1, 7)))

17、索引集合的添加

	let mIndexSet:NSMutableIndexSet = NSMutableIndexSet(indexesInRange: NSMakeRange(1, 3))
	    
	// 添加一個索引元素,集合中已存在時會合並
	mIndexSet.addIndex(6)
	
	// 添加一個索引集合,兩個索引集合有交集時會合並
	mIndexSet.addIndexes(NSIndexSet(indexesInRange: NSMakeRange(8, 2)))
	
	// 添加一個范圍內的元素,有交集時會合並
	mIndexSet.addIndexesInRange(NSMakeRange(12, 3))

18、可變索引集合刪除

	let mIndexSet:NSMutableIndexSet = NSMutableIndexSet(indexesInRange: NSMakeRange(1, 10))
	    
	// 刪除一個索引集合中的索引元素
	mIndexSet.removeIndex(2)
	
	// 刪除指定集合中含有的索引元素
	mIndexSet.removeIndexes(NSIndexSet(indexesInRange: NSMakeRange(4, 2)))
	
	// 刪除指定范圍內含有的索引元素
	mIndexSet.removeIndexesInRange(NSMakeRange(8, 3))
	
	// 刪除全部索引元素
	mIndexSet.removeAllIndexes()

19、索引集合的遍歷

	let indexSet:NSIndexSet = NSIndexSet(indexesInRange: NSMakeRange(1, 5))
    
	// 用閉包遍歷
    
		// 1. enumerateIndexesUsingBlock
    
			// 遍歷獲取索引集合的所有索引值
			indexSet.enumerateIndexesUsingBlock { (idx:Int, stop:UnsafeMutablePointer<ObjCBool>) in             
         
				print(idx)
			}
        
		// 2. enumerateRangesUsingBlock
    
			// 遍歷獲取索引集合的起始值和索引元素數量
			indexSet.enumerateRangesUsingBlock { (range:NSRange, stop:UnsafeMutablePointer<ObjCBool>) in        
        
				print("\(range.location), \(range.length)")
			}
    
	// 條件遍歷
    
		// 1. indexesPassingTest
    
			// 遍歷索引集合,找出索引中所有滿足條件的索引
			let indexs:NSIndexSet = indexSet.indexesPassingTest { (idx, stop) -> Bool in                        
                
				return idx > 3 ? true : false
			}
            
			indexs.enumerateIndexesUsingBlock { (idx:Int, stop:UnsafeMutablePointer<ObjCBool>) in
   
				print(idx)
			}
        
		// 2. indexPassingTest
    
			// 遍歷索引集合,找出索引中第一個滿足條件的索引
			let index:Int = indexs.indexPassingTest { (idx, stop) -> Bool in                                    
                
				return idx > 3 ? true : false
			}
            
			print(index)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM