How do I get the length of a String
in 'Swift3' on macOS?
This used to work in Swift2:
if purchaseDateString.length == 0 {
purchaseDateString = "n/a"
}
What is Swift3 equivalent for finding String's length.
UPDATE
There are answers to this question as Martin points out. The referred question has 30 answers. However, as far as I can tell, none of them relate specifically to Swift3. I could be wrong..
Swift – String Length/Count To get the length of a String in Swift, use count property of the string. count property is an integer value representing the number of characters in this string.
string-truncate.swift Truncates the string to the specified length number of characters and appends an optional trailing string if longer. - Parameter trailing: A 'String' that will be appended after the truncation. - Returns: 'String' object. let str = "I might be just a little bit too long".
In Swift, the first property is used to return the first character of a string.
Have you try characters.count
if purchaseDateString.characters.count == 0 {
purchaseDateString = "n/a"
}
Edit:- As of in Swift 3.2 and Swift 4 String is Collection type so you can directly use count
property on String.
if purchaseDateString.count == 0 {
purchaseDateString = "n/a"
}
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