daily pastebin goal
5%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let array = Array(someSet)
  2.    
  3. let mySet = Set(["a", "b", "a"])  // Set<String>
  4. let myArray = Array(mySet)        // Array<String>
  5. print(myArray) // [b, a]
  6.    
  7. init<S>(_ s: S) where S : Sequence, Element == S.Iterator.Element
  8.    
  9. let stringSet = Set(arrayLiteral: "car", "boat", "car", "bike", "toy")    
  10. let stringArray = Array(stringSet)
  11.  
  12. print(stringArray)
  13. // may print ["toy", "car", "bike", "boat"]
  14.    
  15. let stringSet = Set(["car", "boat", "bike", "toy"])
  16. let stringArray = stringSet.sorted()
  17.  
  18. print(stringArray)
  19. // will print ["bike", "boat", "car", "toy"]
  20.    
  21. let stringSet = Set(arrayLiteral: "car", "boat", "car", "bike", "toy")
  22. let stringArray = stringSet.filter { $0.characters.first != "b" }
  23.  
  24. print(stringArray)
  25. // may print ["car", "toy"]
  26.    
  27. let intSet = Set([1, 3, 5, 2])
  28. let stringArray = intSet.flatMap { String($0) }
  29.  
  30. print(stringArray)
  31. // may print ["5", "2", "3", "1"]
  32.    
  33. let intSet = Set([1, 3, 5, 2])
  34. // alternative to `let intArray = Array(intSet)`
  35. let intArray = intSet.map { $0 }
  36.  
  37. print(intArray)
  38. // may print [5, 2, 3, 1]
  39.    
  40. var example:Set = ["a","b","c"]
  41. let makeExampleArray = [example.sorted()]
  42. makeExampleArray
  43.    
  44. ["a","b","c"] or ["b","c","a",] or ["c","a","b"] or ["a","c","b"] or ["b","a","c"] or ["c","b","a"]
  45.    
  46. for item in myItemSet {
  47.    ...
  48. }
  49.    
  50. let itemsArray = myItemSet.sorted()
  51.    
  52. let itemsArray = myItemSet.map { $0 }
  53.    
  54. var itemsArray : [MyObjectType] = []
  55. if let typedSet = myItemSet as? Set<MyObjectType> {
  56.  itemsArray = typedSet.map { $0 }
  57. }
  58.    
  59. extension Set {
  60.     var array: [Element] {
  61.         return Array(self)
  62.     }
  63. }
  64.    
  65. let array = set.array
  66.    
  67. func getArrayFromSet(set:NSSet)-> NSArray {
  68.  
  69. return set.map ({ String($0) })
  70. }
  71.    
  72. var letters:Set = Set<String>(arrayLiteral: "test","test") // your set
  73. print(self.getArrayFromSet(letters))
RAW Paste Data
Top