如何将“索引”转换为Swift的“Int”类型?

时间:2021-09-15 16:32:14

I want to convert the index of a letter contained within a string to an integer value. Attempted to read the header files but I cannot find the type for Index, although it appears to conform to protocol ForwardIndexType with methods (e.g. distanceTo).

我要将字符串中包含的字母的索引转换为整数值。试图读取头文件,但我找不到索引的类型,尽管它似乎与协议转发索引类型(例如distanceTo)一致。

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)

Any help is appreciated.

任何帮助都是感激。

1 个解决方案

#1


40  

You need to use the method distanceTo(index) in relation to the original string start index:

你需要使用方法distanceTo(index)相对于原始字符串start index:

let intValue = letters.startIndex.distanceTo(index)

You can also extend String with a method to return the first occurrence of a character in a string as follow:

您还可以使用方法扩展字符串,以返回字符串中第一个字符的出现,如下所示:

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

Xcode 8•Swift 3。

let letters = "abcdefg"
if let index = letters.characters.index(of: "c") {
    let distance = letters.distance(from: letters.startIndex, to: index)
    print("distance:", distance)
}

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

Xcode 9•Swift 4

let letters = "abcdefg"
if let index = letters.index(of: "c") {
    let distance = letters.distance(from: letters.startIndex, to: index)
    print("distance:", distance)
}

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

Another possible approach in Swift 4 is to return the index encodedOffset:

Swift 4的另一种可能方法是返回索引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")
}

#1


40  

You need to use the method distanceTo(index) in relation to the original string start index:

你需要使用方法distanceTo(index)相对于原始字符串start index:

let intValue = letters.startIndex.distanceTo(index)

You can also extend String with a method to return the first occurrence of a character in a string as follow:

您还可以使用方法扩展字符串,以返回字符串中第一个字符的出现,如下所示:

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

Xcode 8•Swift 3。

let letters = "abcdefg"
if let index = letters.characters.index(of: "c") {
    let distance = letters.distance(from: letters.startIndex, to: index)
    print("distance:", distance)
}

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

Xcode 9•Swift 4

let letters = "abcdefg"
if let index = letters.index(of: "c") {
    let distance = letters.distance(from: letters.startIndex, to: index)
    print("distance:", distance)
}

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

Another possible approach in Swift 4 is to return the index encodedOffset:

Swift 4的另一种可能方法是返回索引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")
}