解决swift错误:无法专门化非泛型定义

时间:2017-05-16 18:58:09

标签: ios swift swift3

我最近将一个xcode项目从xcode 7转换为xcode 8,并且这样做,从swift 2.3转移到3.该app依赖于pod和各种框架。目前,其中5个错误相同,错误是

  

无法专门化非通用定义   在解析这个'<'时作为类型参数括号

我觉得一旦这些错误中的一个得到解决,我将能够解决其余的问题。所以这里是当前发生错误的代码片段

func getWordIndex(_ charIndex:Int) -> (start: Int, stop: Int) {

        let plainString =  self.fillerAttribString?.string

        var charAtIndex = plainString?.substring(with: Range<String.Index>( plainString!.characters.index(plainString!.startIndex, offsetBy: charIndex) ..<  (plainString?.characters.index((plainString?.startIndex)!, offsetBy: charIndex+1))!))


        var tCharIndex = charIndex
        var leftCharIndex = 0
        var rightCharIndex = (self.fillerAttribString?.length)! - 1
        for i in tCharIndex..<self.fillerAttribString!.length {
            charAtIndex = plainString?.substring(with: Range<String.Index>( plainString!.characters.index(plainString!.startIndex, offsetBy: i) ..<  (plainString?.characters.index((plainString?.startIndex)!, offsetBy: i+1))!))
            if (charAtIndex == " ")
            {
                rightCharIndex = i
                break
            }
        }

        tCharIndex = charIndex
        for i in (0..<tCharIndex).reversed(){


            charAtIndex = plainString?.substring(with: Range<String.Index>( plainString!.characters.index(plainString!.startIndex, offsetBy: i) ..<  (plainString?.characters.index((plainString?.startIndex)!, offsetBy: i+1))!))
            if (charAtIndex == " ")
            {
                leftCharIndex = i + 1
                break
            }
        }

        return (leftCharIndex, rightCharIndex);
    }

目前在这些行上发生错误

    var charAtIndex = plainString?.substring(with: Range<String.Index>( plainString!.characters.index(plainString!.startIndex, offsetBy: charIndex) ..<  (plainString?.characters.index((plainString?.startIndex)!, offsetBy: charIndex+1))!))

charAtIndex = plainString?.substring(with: Range<String.Index>( plainString!.characters.index(plainString!.startIndex, offsetBy: i) ..<  (plainString?.characters.index((plainString?.startIndex)!, offsetBy: i+1))!))

charAtIndex = plainString?.substring(with: Range<String.Index>( plainString!.characters.index(plainString!.startIndex, offsetBy: i) ..<  (plainString?.characters.index((plainString?.startIndex)!, offsetBy: i+1))!))

这是此函数中发生此错误的代码的3部分。 先感谢您。我在这里环顾四周,似乎无法弄明白。

2 个答案:

答案 0 :(得分:0)

尝试删除Range<String.Index>初始化程序调用,因为看起来后面的内容已经是字符串范围。

尽管如此,你的风格也应该有效。例如,所有3种形式都可以互换:

let str = "foo"
let r0 = str.startIndex..<str.endIndex
let r1 = Range(str.startIndex..<str.endIndex)
let r2 = Range<String.Index>(str.startIndex..<str.endIndex)

r0 == r1 // true
r0 == r2 // true

答案 1 :(得分:0)

func getWordIndex(_ charIndex:Int) -> (start: Int, stop: Int) {

    let plainString =  self.fillerAttribString?.string

    var charAtIndex = plainString?.substring(with: plainString!.characters.index(plainString!.startIndex, offsetBy: charIndex) ..<  (plainString?.characters.index((plainString?.startIndex)!, offsetBy: charIndex+1))!)


    var tCharIndex = charIndex
    var leftCharIndex = 0
    var rightCharIndex = (self.fillerAttribString?.length)! - 1
    for i in tCharIndex..<self.fillerAttribString!.length {
        charAtIndex = plainString?.substring(with: plainString!.characters.index(plainString!.startIndex, offsetBy: i) ..<  (plainString?.characters.index((plainString?.startIndex)!, offsetBy: i+1))!)
        if (charAtIndex == " ")
        {
            rightCharIndex = i
            break
        }
    }

    tCharIndex = charIndex
    for i in (0..<tCharIndex).reversed(){


        charAtIndex = plainString?.substring(with: plainString!.characters.index(plainString!.startIndex, offsetBy: i) ..<  (plainString?.characters.index((plainString?.startIndex)!, offsetBy: i+1))!)
        if (charAtIndex == " ")
        {
            leftCharIndex = i + 1
            break
        }
    }

    return (leftCharIndex, rightCharIndex);
}