I have the following class with private struct for strings which I would like to use them for formatted strings later on. However, the code crashes at run time.
Why is this? Is it because it is defined as static let?
Below is the stripped code:
class LGNotificationHandler {
private struct Strings {
static let SentImagesENG = "Sent %@ images to the event"
static let SentImagesTUR = "Etkinliğe %@ görsel gönderdi"
}
func buildNotificationString(imageCount: Int) -> String {
if imageCount == 1 {
.
.
.
} else {
// below line is giving error at run time
notificationENG = String(format: Strings.SentImagesENG, imageCount)
notificationTUR = String(format: Strings.SentImagesTUR, imageCount)
}
}
}
Swift String Interpolation 2) Means the string is created from a mix of constants, variables, literals or expressions. Example: let length:Float = 3.14 var breadth = 10 var myString = "Area of a rectangle is length*breadth" myString = "\(myString) i.e. = \(length)*\(breadth)"
You neglected to provide any details about the crash but one obvious problem is the use of the %@
format specifier with an Int
. You need to use %d
with Int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With