2014-09-07 4 views

risposta

117

subscripting una matrice con un intervallo non restituisce un array, ma una fetta. Tuttavia, puoi creare una matrice da quella sezione.

var tags = ["this", "is", "cool"] 
tags[1..<3] 
var someTags: Slice<String> = tags[1..<3] 
var someTagsArray: [String] = Array(someTags) 
+2

Dove trovate 'Slice' documentato? Anche il messaggio di errore non è corretto e il 'Slice' si presenta come [String] nel campo da giuoco. Quindi sembra che ci siano due errori del compilatore e documentazione mancante. – zaph

+1

Qui è possibile vedere la definizione del metodo di iscrizione: https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/SwiftStandardLibraryReference/Array.html – connor

+2

Accetto che l'errore non sia corretto e che la documentazione possa usa un po 'di lavoro Se lo provi in ​​due passaggi: 'var someTags = tags [1 .. <3] var someTagsArray: [String] = someTags' ti dà l'errore' più utile: 'Slice ' non è convertibile in '[String ] " – connor

0

È anche possibile fare questo per ottenere un nuovo array della fetta:

var tags = ["this", "is", "cool"] 
var someTags = [String]() 
someTags += tags[1..<3] 
println(someTags[0]) //prints ["is", "cool"] 
+0

Va bene, ma è confuso. –

+0

Mi aspettavo che stampasse "è" – Yitzchak

9
var tags = ["this", "is", "cool"] 
var someTags: [String] = Array(tags[1..<3]) 
println("someTags: \(someTags)") // "someTags: [is, cool]" 
Problemi correlati