Swift 4: 'substring(to:)' is deprecated [duplicate] - ios

This question already has answers here:
How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator
(21 answers)
Closed 5 years ago.
I'm having trouble converting my Swift 3 code to Swift 4. I've managed to translate everything else in the app successfully, but am having trouble with a single line of code:
cleanURL = cleanURL.substring(to: cleanURL.index(before: cleanURL.endIndex))
The error I'm getting is this:
ViewController.swift:62:33: 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator.

Well, do what the error says, use the String slicing subscript (subscript(_:)) with a 'partial range upto' operator (..<, docs, where it's called a 'half open range', oddly enough):
let actuallyCleanURL = kindaCleanURL[..<kindaCleanURL.endIndex]
Note that this returns a Substring. If you need to do more slicing operations, do them on this substring. Once you're done, promote your to a String by running it through the String initializer (String(mySubString)), causing a copy of the memory to be made.

Related

Swift assign a float variable [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I found a issue that seems strange, or something I don't understand. The following is a very simple variable assignment:
let f: Float = 310.15
In debugger, I see it is 310.149994
I need to send the value as 310.15 to server, but 310.149994 is received instead.
Is this a Swift compiler or runtime bug? Or any way I can ensure 310.15 float is sent? (I cannot change the server API signature)
Try this one
let f : Float = 310.149954
print(round(1000*f)/1000)
result : 310.15

Swift compiler error: missing argument labels [duplicate]

This question already has answers here:
Swift 3 first parameter names
(5 answers)
Closed 5 years ago.
I run Swift on Xcode 8.3.3. Here I get an error regarding closures.
It seems that this part is all correct [var intro = introToFriends("Jim", "Pam")
intro] yet isn't clicking.
What's the issue?
Later Swift versions require explicit argument labels when calling a function. In your case it needs to read:
var intro = introToFriends(friendOne: "Jim", friendTwo: "Pam")
Alternatively, you can allow for omitting the use of argument labels if you add underscores to the parameter labels in the function declaration such as:
func introToFriends(_ friendOne: String, _ friendTwoString) {
...
}
For more details check the official Apple documentation on function argument labels.

Swift 3: Cannot automatically unwrap optional before setting it to Label [duplicate]

This question already has an answer here:
Swift 3 incorrect string interpolation with implicitly unwrapped Optionals
(1 answer)
Closed 6 years ago.
I am new to iOS development. I am doing this project by watching video tutorial, where they are using the earlier version of Swift, but I am using Swift.
I came across to this problem. Two optional integers are unwrapped when being used for calculation. But it is not unwrapped when the text is given to the label.
I tried to unwrap them again when the text is given to the label and it worked. Why it is behaving strange?
var leftNumber: Int!
var rightNumber: Int!
func generateProblem() {
leftNumber = generateRandomNumber()
rightNumber = generateRandomNumber()
// The problem is here
problemLabel.text = "\(leftNumber) x \(rightNumber)"
}
func generateRandomNumber() -> Int {
return Int(arc4random_uniform(UInt32(9))) + 1
}
The screenshot of simulator:
In Swift 3, an implicitly-unwrapped optional is the same as an optional, except that it will implicitly unwrap in contexts that require it. For instance, if you had a func foo(i: Int), you could write foo(i: leftNumber) and the compiler would perform the unwrap operation for you.
String interpolation is not a context where unwrapping is required. As you can use an optional there, Swift prefers the optional case and does not unwrap for you.
Tutorials that you find online may either not have been updated for Swift 3, or there may have been a misunderstanding.

StringByAppendingPathComponent() and WriteToFile()

I'm trying to learn Swift 3 with translating a code from Swift 2. In swift 2, I see the code like this:
return fullPath.stringByAppendingPathComponent(name)
But, when I try in Swift 3, I've got similiar code, but like this:
return fullPath.strings(byAppendingPaths: [name])
The issue is, return type in the 1st code is String (and that's the output I need from the lesson I learn), but return type in 2nd code should be [String].
The other issue is, in Swift 2 the code should be:
imgData?.WriteToFile(fullPath, atomicaly:Bool)
But in Swift 3, I only can input code like this:
imgData.Write(to: URL , option: WritingOption) throws
But in some examples, there's .Write(toFile: , atomically:) but I can't find it in Xcode.
Am I translating incorrectly or using both Swift 2 and Swift 3 incorrectly?
Regarding the first part of your question, as dan stated in the comments you should be using fullPath.appendingPathComponent(name) instead.
Regarding your second question:
The main difference between writeToFile and write(to: is the fact that the first is for Strings and the seconds is for NSData.
Somewhat related:
According to the NSData Class Reference
In iOS 2.0+ you have:
write(to:atomically:)
and
write(toFile:atomically:)
Given:
Since at present only file:// URLs are supported, there is no
difference between this method and writeToFile:atomically:, except for
the type of the first argument.
None of this has changed in Swift 3 according to the Swift Changelog.

swift UITest: Missing argument for parameter #1 in call [duplicate]

This question already has answers here:
Swift UITesting error: Invalid escape sequence in literal. \U201c
(2 answers)
Closed 7 years ago.
I am running the UITest for my swift project, after i click a UITextField it generated this code:
let secureTextField = app.secureTextFields["\U5bc6\U7801"]
secureTextField.tap()
The error is
/XXXUITests.swift:46:60: Invalid escape sequence in literal,
then i searched and changed ["\U5bc6\U7801"] to ["\u{5bc6}\u{7801}"], i got the error
Missing argument for parameter #1 in call
Any help?
Just not sure why i re-generated the code the error disappeared.

Resources