2015-12-31 17 views
35

Voglio convertire l'indice di una lettera contenuta all'interno di una stringa in un valore intero. Tentativo di leggere i file di intestazione ma non riesco a trovare il tipo per Index, anche se sembra conforme al protocollo ForwardIndexType con metodi (ad esempio distanceTo).Come convertire "Index" in "Int" in Swift?

var letters = "abcdefg" 
let index = letters.characters.indexOf("c")! 

// ERROR: Cannot invoke initializer for type 'Int' with an argument list of type '(String.CharacterView.Index)' 
let intValue = Int(index) // I want the integer value of the index (e.g. 2) 

Qualsiasi aiuto è apprezzato.

+0

do u hanno Xcode 7.2 e veloce 2.x? – aaisataev

+0

In realtà, sto scaricando Xcode 7.2 proprio in questo momento. – Christopher

+0

Per la stringa di caratteri univoci: 'let index = String (letters.characters.reverse()). Characters.indexOf (" c ") !. distanceTo (letters.endIndex)' – dfri

risposta

30

è necessario utilizzare il metodo distanceTo (indice) rispetto alla stringa originale inizio index:

let intValue = letters.startIndex.distanceTo(index) 

è anche possibile estendere String con un metodo per restituire la prima occorrenza di un carattere in una stringa seguire:

extension String { 
    func indexDistanceOfFirst(character character: Character) -> Int? { 
     guard let index = characters.indexOf(character) else { return nil } 
     return startIndex.distanceTo(index) 
    } 
} 

let letters = "abcdefg" 
let char: Character = "c" 
if let index = letters.indexDistanceOfFirst(character: char) { 
    print("character \(char) was found at position #\(index)") // "character c was found at position #2\n" 
} else { 
    print("character \(char) was not found") 
} 

Xcode 8 • Swift 3

extension String { 
    func indexDistance(of character: Character) -> Int? { 
     guard let index = characters.index(of: character) else { return nil } 
     return distance(from: startIndex, to: index) 
    } 
} 

let letters = "abcdefg" 
let char: Character = "c" 
if let index = letters.indexDistance(of: char) { 
    print("character \(char) was found at position #\(index)") // "character c was found at position #2\n" 
} else { 
    print("character \(char) was not found") 
} 

Xcode 9 • Swift 4

extension String { 
    func indexDistance(of character: Character) -> Int? { 
     guard let index = index(of: character) else { return nil } 
     return distance(from: startIndex, to: index) 
    } 
} 

Un altro possibile approccio a Swift 4 è quello di restituire l'indice encodedOffset:

extension String { 
    func encodedOffset(of character: Character) -> Int? { 
     return index(of: character)?.encodedOffset 
    } 
    func encodedOffset(of string: String) -> Int? { 
     return range(of: string)?.lowerBound.encodedOffset 
    } 
} 

let letters = "abcdefg" 
let char: Character = "c" 
if let index = letters.encodedOffset(of: char) { 
    print("character \(char) was found at position #\(index)") // "character c was found at position #2\n" 
} else { 
    print("character \(char) was not found") 
} 
+6

Sono così confuso perché non decidono solo di creare una funzione che restituisce l'indice di valore intero dell'elemento di un array. . smh – MarksCode

+3

Non tutti i caratteri possono essere memorizzati in un singolo byte. Dovresti richiedere un po 'di tempo e leggere la documentazione di Swift String –

+1

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html –