Advancedby' in Swift Is Unavailable

advanced(by:)' is unavailable convert from swift 3 to swift 4

  1. In Swift 4 there is an API to make Range<String.Index> from NSRange and vice versa. The String extension can be reduced to

    internal func range(from nsRange: NSRange) -> Range<String.Index>? {
    return Range(nsRange, in: self)
    }
  2. In Swift 4 the type of string attributes has been changed from String to NSAttributedStringKey. For example NSForegroundColorAttributeName is replaced with NSAttributedStringKey.foregroundColor. You need to change the declaration of attrib to

    var attrib: [NSAttributedStringKey: Any] = [.font: font]

    and to change the line to add an attribute accordingly.

advanceby unaviable to advance an index by offer in swift 3

You should just do what the error says and use index(_:at:offsetBy:):

original.insert("-", at: original.index(original.startIndex, offsetBy: 14))

In Swift 3 string handling methods have been renamed a little. advanceBy turned into this offsetBy instead.

Got error of advancedBy on Swift3 conversion

You should Write this:

let filenameLast = imageFile.index(dotLocation!,offsetBy: -1)

advancedBy' API in Swift 2.0

You need to call advancedBy on endIndex.

let startIndex = cardNumber.endIndex.advancedBy(-4)
let lastFourDigitsOfPhoneNumber = phoneNumber.substringFromIndex(startIndex)

Can't create a range in Swift 3

In Swift 3, "Collections move their index", see
A New Model for Collections and Indices on Swift evolution.

Here is an example for String ranges and indices:

let string = "ABCDEFG"
if let range = string.range(of: "CDEF") {
let lo = string.index(range.lowerBound, offsetBy: 1)
let hi = string.index(range.lowerBound, offsetBy: 3)
let subRange = lo ..< hi
print(string[subRange]) // "DE"
}

The

public func index(_ i: Index, offsetBy n: IndexDistance) -> Index

method is called on the string to calculate the new indices from the
range (which has properties lower/upperBound now instead of
start/endIndex).

+ is unavailable: Please use explicit type conversions or Strideable methods for mixed-type arithmetics

Rather than using the + operator, you can simply use Strideable.advanced(by:).

func increment<T: Strideable>(number: T) -> T {
return number.advanced(by: 1)
}

increment(number: 2) //returns 3
increment(number: 2.5) //returns 3.5


Related Topics



Leave a reply



Submit