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

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!)

Related

Can we use get instead of function in swift? [duplicate]

This question already has answers here:
Computed read-only property vs function in Swift
(11 answers)
Closed 1 year ago.
I have created getName1 & getName2() as below
var myName = "Jhon"
var salt = " Salt"
var getName1:String {
get {
return myName + salt
}
}
func getName2() ->String {
return myName + salt
}
print("getName1\(getName1)")
print("getName2\(getName2())")
So instead of getName2() can I use getName1 directly does that make any difference ?
There is no difference but getName1 is a computed property while getName2 is a function , in this case you better use getName1 as a function is expected to do some block code or IMO not just a one line

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

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
}
}

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"

What to use in place of stringByAppendingPathComponent in Swift 2 [duplicate]

This question already has answers here:
stringByAppendingPathComponent is unavailable
(11 answers)
Closed 7 years ago.
I have just installed Xcode 7 with the new Swift 2, and I now have 50+ errors saying that "stringByAppendingPathComponent" is unavailable, and that I should use "URLByAppendingPathComponent" instead. I have been setting all of my texture properties like so:
let dropTexture = SKTexture(image: UIImage(
contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent(
"P04_rainDrop1.png"))!)
I have been doing this so they do not stay in memory when the SKScene is changed and it has been working perfectly. However directly replacing "URLByAppendingPathComponent" does not fix the errors.
How can I change this to fix the error and get the same SKTexture?
All you have to do is cast to NSString to recover stringByAppendingPathComponent, like this:
let dropTexture = SKTexture(image: UIImage(
contentsOfFile:(NSBundle.mainBundle().resourcePath! as NSString).stringByAppendingPathComponent(
"P04_rainDrop1.png"))!)
As Leo Dabus rightly says, you can save yourself from all that casting by adding an extension to String. However, you should not, as he suggests, call NSString(string:), which generates an extra string. Just cast:
extension String {
func stringByAppendingPathComponent(pathComponent: String) -> String {
return (self as NSString).stringByAppendingPathComponent(pathComponent)
}
}

How to create an Array UIImage in Swift [duplicate]

This question already has an answer here:
Expected Declaration error creating array in ViewController, can't work out why
(1 answer)
Closed 7 years ago.
This my code
var changePhoto = [UIImage]()
let photoPng = UIImage(named: "1.png")
let photoJpg = UIImage(named: "1.jpg")
changePhoto += photoPng
changePhoto += photoJpg
but error "expected declaration"
Please help me! What's wrong?
You need to use the append method to add new objects to your array:
changePhoto.append(photoPng)
changePhoto.append(photoJpg)
Also, you only can have functionality inside of methods. So try to call it inside a method. for example if you use a Viewcontroller in your viewDidLoad method.