Ho convertito un NSIndexSet in un array [Int] utilizzando la risposta allo https://stackoverflow.com/a/28964059/6481734 Ho bisogno di fare essenzialmente l'opposto, trasformando lo stesso tipo di array in un NSIndexSet.Creare NSIndexSet dall'array intero in Swift
risposta
Swift 3
IndexSet
possono essere creati direttamente da un array letterale utilizzando init(arrayLiteral:)
, in questo modo:
let indices: IndexSet = [1, 2, 3]
originale risposta (Swift 2.2)
Simile a pbasdf's answer, ma usa forEach(_:)
let array = [1,2,3,4,5,7,8,10]
let indexSet = NSMutableIndexSet()
array.forEach(indexSet.add) //Swift 3
//Swift 2.2: array.forEach{indexSet.addIndex($0)}
print(indexSet)
è possibile utilizzare un NSMutableIndexSet
e il suo addIndex
metodo:
let array : [Int] = [1,2,3,4,5,7,8,10]
print(array)
let indexSet = NSMutableIndexSet()
for index in array {
indexSet.addIndex(index)
}
print(indexSet)
Questo sarà molto più facile a Swift 3:
let array = [1,2,3,4,5,7,8,10]
let indexSet = IndexSet(array)
Wow!
Informazioni sull'uso Set<Int>
dal IndexSet
è in sola lettura (non è possibile aggiungere nuovi articoli)?
var set: Set<Int> = [0]
set.insert(1)
set.insert(2)
return set
IndexSet - non è di sola lettura (immutabile), dipende dalla dichiarazione come let o var. Non confondere con NSIndexSet. – vsilux
Swift 3
let fromRange = IndexSet(0...10)
let fromArray = IndexSet([1, 2, 3, 5, 8])
Aggiunto questa risposta perché l'opzione fromRange
non era ancora menzionato.
- 1. NSIndexSet "-indexAtIndex:"?
- 2. Get NSIndexSet da NSArray
- 3. Conversione NSData in numero intero a Swift
- 4. Come creare un array intero in Python?
- 5. Swift & SpriteKit: Immagine a schermo intero
- 6. Come ottenere indici da NSIndexset in un NSArray nel cacao?
- 7. Ottieni le dimensioni dello schermo in Swift come numero intero
- 8. Come si imposta il carattere intero con htonl in Swift?
- 9. Qual è la differenza tra NSIndexSet e NSSet?
- 10. Come creare un pulsante circolare in Swift?
- 11. Come creare una matrice multidimensionale in swift?
- 12. Come creare un NSMutableDictionary in Swift?
- 13. Come creare oggetti Singleton in swift
- 14. Come creare un CGSize in Swift?
- 15. Come creare un array vuoto in Swift?
- 16. Il check in di un intero è divisibile per un altro intero (Swift)
- 17. Creare un NSAlert con Swift
- 18. zoom intero UICollectionView
- 19. iPhone come creare un'app a schermo intero?
- 20. Creare un oggetto Swift da un dizionario
- 21. Impossibile creare una gamma di Swift 3
- 22. Creare un setter Solo a Swift
- 23. Swift. Impossibile creare il modulo Objective-C
- 24. Come creare managedObjectContext utilizzando Swift 3 in Xcode 8?
- 25. è possibile creare una chiusura generica in Swift?
- 26. Come creare e aggiungere valori al dizionario in swift
- 27. Creare una matrice in Swift da un oggetto NSData
- 28. Come creare un UITextView verticale sottoclassandolo da UIScrollView in Swift?
- 29. Creare a livello di programmazione NSWindow in Swift
- 30. Creare una variabile in swift con nome dinamico
Grazie, ma se utilizzo la funzione reloadTableSection, funziona su una sezione? – Bruno
@BrunoFernandes Uh ... Cosa? – Alexander