Init(Start:End:)' Is Deprecated: It Will Be Removed in Swift 3. Use the '..<' Operator

init(start:end:)' is deprecated: it will be removed in Swift 3. Use the '.. ' operator

You should simply write

var continousDigitsRange1:Range<Int> = 0..<0

or if you want to go even simpler

var continousDigitsRange = 0..<0

++' is deprecated: it will be removed in Swift 3

Since Swift 2.2, you should use += 1 or -= 1 instead.

And after looking up Swift's evolution, there are some reasons for removing these operators:

  1. These operators increase the burden to learn Swift as a first programming language - or any other case where you don't already know these operators from a different language.

  2. Their expressive advantage is minimal - x++ is not much shorter than x += 1.

  3. Swift already deviates from C in that the =, += and other assignment-like operations returns Void (for a number of reasons). These operators are inconsistent with that model.

  4. Swift has powerful features that eliminate many of the common reasons you'd use ++i in a C-style for loop in other languages, so these are relatively infrequently used in well-written Swift code. These features include the for-in loop, ranges, enumerate, map, etc.

  5. Code that actually uses the result value of these operators is often confusing and subtle to a reader/maintainer of code. They encourage "overly tricky" code which may be cute, but difficult to understand.

  6. While Swift has well defined order of evaluation, any code that depended on it (like foo(++a, a++)) would be undesirable even if it was well-defined.

  7. These operators are applicable to relatively few types: integer and floating point scalars, and iterator-like concepts. They do not apply to complex numbers, matrices, etc.

Finally, these fail the metric of "if we didn't already have these, would we add them to Swift 3?"

Please check out Swift evolution for more info.

init()' is deprecated: init() will be removed in Swift 3. Use `nil`

AudioUnit is a typealias of AudioComponentInstance which itself is a typealias of COpaquePointer. Initializing pointers will be removed with Swift 3.0, just set the variable to nil.

nil and AudioUnit() should do the same thing. If your program is crashing with nil, you probably have a bug somewhere else.

Xcode Playground

Creating closed Range in Swift 3 not working

Operators ... and ..< used to produce the same type, Range, in Swift 2.x. Now they produce different types (migration guide):

  • Range
  • CountableRange
  • ClosedRange
  • CountableClosedRange

Changing the type in the first assignment to ClosedRange should fix the problem. Better yet, let Swift infer the type for you:

let range = 0...2

NSRange to Range String.Index

The NSString version (as opposed to Swift String) of replacingCharacters(in: NSRange, with: NSString) accepts an NSRange, so one simple solution is to convert String to NSString first. The delegate and replacement method names are slightly different in Swift 3 and 2, so depending on which Swift you're using:

Swift 3.0

func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {

let nsString = textField.text as NSString?
let newString = nsString?.replacingCharacters(in: range, with: string)
}

Swift 2.x

func textField(textField: UITextField,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String) -> Bool {

let nsString = textField.text as NSString?
let newString = nsString?.stringByReplacingCharactersInRange(range, withString: string)
}

What are the advantages Swift deprecates C-style for statement?

For details, see Swift Evolution - Remove C style for-loops

To quote the reasoning:

  1. Both for-in and stride provide equivalent behavior using Swift-coherent approaches without being tied to legacy terminology.
  2. There is a distinct expressive disadvantage in using for-loops compared to for-in in succinctness
  3. for-loop implementations do not lend themselves to use with collections and other core Swift types.
  4. The for-loop encourages use of unary incrementors and decrementors, which will be soon removed from the language.
  5. The semi-colon delimited declaration offers a steep learning curve from users arriving from non C-like languages
  6. If the for-loop did not exist, I doubt it would be considered for inclusion in Swift 3.

In summary: there are better ways (more expressive) than a C-style for-loop to iterate in Swift.

Some examples:

for-in over a range:

for i in 0 ..< 10 {
//iterate over 0..9
print("Index: \(i)")
}

for i in (0 ..< 10).reverse() {
//iterate over 9..0
print("Index: \(i)")
}

For arrays (and other sequences) we have many options (the following is not a complete list):

let array = ["item1", "item2", "item3"]

array.forEach {
// iterate over items
print("Item: \($0)")
}

array.reverse().forEach {
// iterate over items in reverse order
print("Item: \($0)")
}

array.enumerate().forEach {
// iterate over items with indices
print("Item: \($1) at index \($0)")
}

array.enumerate().reverse().forEach {
// iterate over items with indices in reverse order
print("Item: \($1) at index \($0)")
}

for index in array.indices {
// iterate using a list of indices
let item = array[index]
print("Item \(item) at index: \(index)")
}

Also note that if you are converting an array to another array, almost always you want to use array.filter or array.map or a combination of them.

For all Strideable types we can use the stride method to generate indices, for example:

for index in 10.stride(to: 30, by: 5) {
// 10, 15, 20, 25 (not 30)
print("Index: \(index)")
}

for index in 10.stride(through: 30, by: 5) {
// 10, 15, 20, 25, 30
print("Index: \(index)")
}

With arrays we can do:

for index in 0.stride(to: array.count, by: 2) {
// prints only every second item
let item = array[index]
print("Item \(item) at index: \(index)")
}

How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator

You should leave one side empty, hence the name "partial range".

let newStr = str[..<index]

The same stands for partial range from operators, just leave the other side empty:

let newStr = str[index...]

Keep in mind that these range operators return a Substring. If you want to convert it to a string, use String's initialization function:

let newStr = String(str[..<index])

You can read more about the new substrings here.



Related Topics



Leave a reply



Submit