[ad_1]
I’m a novice with Swift and iOS growth and I’ve come throughout a bizarre state of affairs.
I’m making an attempt to make use of a do-catch assertion to forestall an error, nevertheless it looks like the do-catch is doing nothing in any respect.
Here is my code:
extension String {
mutating func insert(string:String,ind:Int) {
do{
attempt self.insert(contentsOf: string, at:self.index(self.startIndex, offsetBy: ind))
//This do-catch is to verify nothing occurs if there isn't a √ within the equation.
} catch {
}
}
}
The ‘attempt’ assertion doesn’t appear to be doing something both! I’ve searched and searched on-line boards and haven’t give you something helpful.
I do know Java properly, and you’ll all the time catch a selected error like:
attempt{
} catch NameOfException {
}
However I’ve discovered nothing the like in Swift. The error code that’s returned is that this:
Swift/StringCharacterView.swift:60: Deadly error: String index is out of bounds
2022-03-19 17:37:04.234797-0600 iOS-App[18937:2984774] Swift/StringCharacterView.swift:60: Deadly error: String index is out of bounds
(lldb)
I interpret this to imply that the String index is clearly out of bounds, however I can not discover a technique (like Java) to catch this sort of expression, or if I may even deal with errors like Java in any respect. However I need to catch it in order that my app doesn’t crash.
I am utilizing a operate to try to find all situations of ‘√’ inside a String; myString.insert(). ind is the ‘Index’ to insert the supplied ‘String’ worth.
[ad_2]
