Cannot Invoke Initializer For Type 'Range≪String.Index≫' With an Argument List of Type '(Range≪String.Index≫)'

Cannot invoke initializer for type 'RangeString.Index' with an argument list of type '(RangeString.Index)'

Some background:

In Swift 3, additional range types were introduced, making a total
of four (see for example Ole Begemann: Ranges in Swift 3):

Range, ClosedRange, CountableRange, CountableClosedRange

With the implementation of SE-0143 Conditional conformances in Swift 4.2, the “countable” variants
are not separate types anymore, but (constrained) type aliases, for example

 public typealias CountableRange<Bound: Strideable> = Range<Bound>
where Bound.Stride : SignedInteger

and, as a consequence, various conversions between the different
range types have been removed, such as the

init(_ other: Range<Range.Bound>)

initializer of struct Range. All theses changes are part of the
[stdlib][WIP] Eliminate (Closed)CountableRange using conditional conformance (#13342) commit.

So that is the reason why

let range: Range<Index> = Range<Index>(start..<self.endIndex)

does not compile anymore.

How to fix

As you already figured out, this can be simply fixed as

let range: Range<Index> = start..<self.endIndex

or just

let range = start..<self.endIndex

without the type annotation.

Another option is to use a one sided range
(introduced in Swift 4 with SE-0172 One-sided Ranges):

extension String {
func index(of aString: String, startingFrom position: Int = 0) -> String.Index? {
let start = index(startIndex, offsetBy: position)
return self[start...].range(of: aString, options: .literal)?.lowerBound
}
}

This works because the substring self[start...] shares its indices
with the originating string self.

Cannot invoke initializer for type 'RangeString.Index' with an argument list of type '(start: String.Index, end: String.Index)'

Range.init(start:end:) constructor was removed in Swift 3.0 so you initialize a range like follows:

let range = hex.index(hex.startIndex, offsetBy: 2)..<hex.index(hex.startIndex, offsetBy: 4)

which returns a half-open range of type <String.Index>. Then, you can do the following with that:

hex.substring(with: range)

Error Cannot invoke initializer for type 'String' with an argument list of type '(Int64?)' with nil-coalescing operator

You can simply do,

let int64Value: Int64 = 123
let str = String(int64Value)

Edit:

Since detailItem?.count is optional, you need to unwrap it before passing it to the String initializer. You can use nil coalescing operator to unwrap and provide a default value, i.e.

cell.textLabel?.text = String(detailItem?.count ?? 1)


Related Topics



Leave a reply



Submit