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.
Related
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
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.
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.
This question already has answers here:
Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence
(3 answers)
Closed 6 years ago.
From recent update of Xcode 7.3, I started seeing this message. I am using a sequence for in loop as below:
for (index, product) in EnumerateSequence(self.products) {
//Do something with the product
//Do something with the index
}
The note is on the EnumerateSequence.
If you're wondering why they added this warning and are going to remove EnumerateSequence.init, it's because EnumerateSequence is an implementation detail of the enumerate method. They want you to use enumerate and not rely on how it's implemented.
After some testing, this is the solution to use from Swift 2.2 onwards in case you want to use both the index and the object:
for (index, product) in self.products.enumerate() {
//Do something with the product
//Do something with the index
}
Remove EnumerateSequence and use your Array.enumerate() method
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.