Generic parameter 'Self' could not be inferred.

.If working on my substring conversions for Swift 4 I get the error: Generic parameter 'Self' could not be inferred.


let myStr = "TEXT("

let pos = tmp.range(of:"(")

let substr = myStr[..<pos] ----- Generic parameter 'Self' could not be inferred.


I get it in my code and I created a playgounnd to work on my slicing and get it there as well.

I'm just trying to remove char(s) from the end of the string.


How can I fix is one?


Thanks in advance

Answered by Claude31 in 275353022

Error message is a bit cryptic!


You have 2 ways to do it, at least:


let myStr = "TEXT("
if let pos = myStr.range(of:"(") {    // Need to unwrap ; myStr and not tmp
     let substr = myStr[..<pos.lowerBound]     // pos is a range, you need an index as the limit for ..<
}

or


let myStr = "TEXT("
if let pos = myStr.index(of: "(") {
     let substr2 = myStr[..<pos]     // pos is an index, it works
}



The first will work with a string :

let pos = myStr.range(of:"XT(")!


not the second which expects a character only

Accepted Answer

Error message is a bit cryptic!


You have 2 ways to do it, at least:


let myStr = "TEXT("
if let pos = myStr.range(of:"(") {    // Need to unwrap ; myStr and not tmp
     let substr = myStr[..<pos.lowerBound]     // pos is a range, you need an index as the limit for ..<
}

or


let myStr = "TEXT("
if let pos = myStr.index(of: "(") {
     let substr2 = myStr[..<pos]     // pos is an index, it works
}



The first will work with a string :

let pos = myStr.range(of:"XT(")!


not the second which expects a character only

Generic parameter 'Self' could not be inferred.
 
 
Q