2015-12-31 10 views
35

Chcę przekonwertować indeks litery zawartej w ciągu na liczbę całkowitą. Próbowałem odczytać pliki nagłówkowe, ale nie mogę znaleźć typu dla Index, chociaż wydaje się, że jest zgodny z protokołem ForwardIndexType metodami (na przykład distanceTo).Jak przekonwertować "Indeks", aby wpisać "Int" w 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) 

Każda pomoc jest doceniana.

+0

Do U mają xcode 7,2 i szybką 2.x? – aaisataev

+0

Właściwie, w tej chwili pobieram Xcode 7.2. – Christopher

+0

Dla łańcucha znaków unikalnych: 'let index = String (letters.characters.reverse()). Characters.indexOf (" c ")! DistanceTo (letters.endIndex)' – dfri

Odpowiedz

30

trzeba użyć metody distanceTo (indeks) w stosunku do oryginalnego łańcucha rozpocząć index:

let intValue = letters.startIndex.distanceTo(index) 

Możesz również rozszerzyć String ze sposobu, aby powrócić do pierwszego wystąpienia znaku w ciągu znaków jako następująco:

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) 
    } 
} 

Innym możliwym rozwiązaniem Swift 4 jest powrót indeksu 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

Jestem bardzo zdezorientowany, dlaczego nie zdecydują się po prostu na funkcję, która zwraca indeks liczby całkowitej elementu tablicy. smh – MarksCode

+3

Nie wszystkie znaki mogą być przechowywane w jednym bajcie. Powinieneś poświęcić trochę czasu i przeczytać dokumentację Swift String: –

+1

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