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:
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.
Their expressive advantage is minimal - x++ is not much shorter than x += 1.
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.
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.
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.
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.
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.
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:
- Both
for-in
andstride
provide equivalent behavior using Swift-coherent approaches without being tied to legacy terminology.- There is a distinct expressive disadvantage in using for-loops compared to for-in in succinctness
for-loop
implementations do not lend themselves to use with collections and other core Swift types.- The
for-loop
encourages use of unary incrementors and decrementors, which will be soon removed from the language.- The semi-colon delimited declaration offers a steep learning curve from users arriving from non C-like languages
- 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
Swift Xcode Index Freezing or Slow
Ios11 Swift Silent Push (Background Fetch, Didreceiveremotenotification) Is Not Working Anymore
How to Unittest Combine Cancellables
In Swift 3, What Is a Way to Compare Two Closures
Swift 2/iOS 9 - Libz.Dylib Not Found
Swift - Unit Testing Private Variables and Methods
How to Get Rgb Components from Color in Swiftui
Swift: Testing Against Optional Value in Switch Case
How to "Strongify" Optional Self Using Guard in Swift 2.0
Self' Used Before All Stored Properties Are Initialized
Swiftui - Is There a Popviewcontroller Equivalent in Swiftui
Saving Webview to Pdf Returns Blank Image
Println Dictionary Has "Optional"
Convert a Swift Array of String to a to a C String Array Pointer
Pull Down to Refresh Data in Swiftui
How to Create Dictionary That Can Hold Anything in Key? or All the Possible Type It Capable to Hold
Swift Nspredicate Throwing Exc_Bad_Access(Code=1, Address=0X1) When Compounding Statements
Using Some Protocol as a Concrete Type Conforming to Another Protocol Is Not Supported