'characters' is unavailable: Please use String directly [duplicate] - swift

This question already has an answer here:
'characters is unavailable' please use string directly
(1 answer)
Closed 3 years ago.
Пожалуйста помогите
'characters' is unavailable: Please use String directly

Swift strings are different now. There's no characters property anymore. This should do the trick:
extension URL {
func makeStandardizedLastPathComponent() -> String {
return lastPathComponent.trimmingCharacters(in: .whitespaces)
}
func makeStandardizedFirstCharacterOfLastPathComponent() -> Character? {
return makeStandardizedLastPathComponent().localizedUppercase.first
}
}

Related

How to fix 'characters' is unavailable: Please use String directly in swift 5 [duplicate]

This question already has answers here:
How to filter characters from a string in Swift 4
(2 answers)
Closed 3 years ago.
i've some code to filtering number inside variable.
Here's the code:
var numbers = String(anotherNumbers.characters.filter { "01234567890.".characters.contains($0) })
In the swift 3, this code working correctly. But in the Swift 5, i get an error 'characters' is unavailable: Please use String directly
How to fix this error?
Thankyou.
You can remove characters to use String directly.
For example
var anotherNumbers = "0123456789"
var numbers = String(anotherNumbers.filter { "01234567890.".contains($0) })
returns "0123456789"

How can I get the string value in Optional() in swift, which is followed by nil? [duplicate]

This question already has answers here:
What is an optional value in Swift?
(15 answers)
Closed 5 years ago.
How can I get the string value in Optional() in swift, which is followed by nil?any other proper way to use this evaluateJavaScript() and get the callback value?
webView.evaluateJavaScript("(function() { return 'this'; })();",
completionHandler: {
result in
//Handle your variable
print(result)//(Optional(this), nil)
})
Use if let to parse the optional value:
if let value = result {
print(value)
}

What does 'using' do in Swift methods? [duplicate]

This question already has an answer here:
What are the new "for", "at", "in" keywords in Swift3 function declarations?
(1 answer)
Closed 5 years ago.
I noticed that in I was getting an error unless I did
animateTransition(using transitionContext: UIViewControllerContextTransitioning)
However some tutorial present this method as,
animateTransition(transitionContext: UIViewControllerContextTransitioning) without the using.
It only seems to build if I include using but I'm curious as to it's role and when the change occurred.
check apple documentation
Specifying Argument Labels
You write an argument label before the parameter name, separated by a
space:
func someFunction(argumentLabel parameterName: Int) {
// In the function body, parameterName refers to the argument value
// for that parameter.
}
Here’s a variation of the greet(person:) function that takes a
person’s name and hometown and returns a greeting:
func greet(person: String, from hometown: String) -> String {
return "Hello \(person)! Glad you could visit from \(hometown)."
}
print(greet(person: "Bill", from: "Cupertino"))
// Prints "Hello Bill! Glad you could visit from Cupertino."
The use of argument labels can allow a function to be called in an
expressive, sentence-like manner, while still providing a function
body that is readable and clear in intent.

Why can't I use .toInt() on a String in swift? [duplicate]

This question already has answers here:
.toInt() removed in Swift 2?
(5 answers)
Closed 6 years ago.
I'm making a mock remote control Iphone app using swift and Xcode is not letting me use .toInt() on the text of a UILabel. I'm trying to convert the text from a label into an integer and I'm not sure how to do this. Can anybody help me out? Thanks.
Here's my code:
#IBAction func channelInc(_ sender: UIButton) {
var chnl = channel.text!.toInt()
if (chnl!+1 > 99) {
} else {
let newChnl = chnl!+1
channel.text = "\(String(newChnl))"
}
}
There is not method on the String class called toInt(). The way to do it in swift is to use the initializer for the Int class.
let number = Int("5")
// returns a Int? (optional)
Try
let chnl:Int? = Int(channel.text!)

Error: value of type 'String' has no member 'Generator' in Swift [duplicate]

This question already has answers here:
Iterate through a String Swift 2.0
(4 answers)
Closed 7 years ago.
I have this piece of code in Swift:
var password = "Meet me in St. Louis"
for character in password {
if character == "e" {
print("found an e!")
} else {
}
}
which throws the following error: value of type 'String' has no member 'Generator' in Swift in line: for character in password
I have tried to find online the possible error, but I can't (plus I am new to Swift and trying to navigate myself through the idiosyncrasies of the language).
Any help would be much appreciated (plus a brief explanation of what I am missing if possible)
In order for your code to work you need to do this :
var password = "Meet me in St. Louis"
for character in password.characters {
if character == "e" {
print("found an e!")
} else {
}
}
The problem with your code not working was the fact that it was not iterating over your array looking for a particular Character. Changing it to password.characters forces i to "search" each Character of the array password and voila.This behaviour happens in swift 2.0 because String no longer conforms to SequenceType protocol while String.CharacterView does!