String length in Swift 1.2 and Swift 2.0 [duplicate]
Asked Answered
B

5

33

In the previous version of Swift, I had the following code.

func myfunc(mystr: String) {
    if mystr.utf16Count >= 3 {

With the latest release of Swift 1.2, I now get the following error.

'utf16Count' is unavailable: Take the count of a UTF-16 view instead, i.e. count(str.utf16)

So I change my code as follows:

func myfunc(mystr: String) {
    if count(mystr.utf16) >= 3 {

But that doesn't work. I now get the following error message instead.

'(String.UTF16View) -> _' is not identical to 'Int16'

What is the correct way to get the length of a string with Swift 1.2?

Breeks answered 11/4, 2015 at 7:10 Comment(2)
Right click on count, and "Jump to Definition".Wrongly
From Swift 4+ you can use, str.countRoadside
P
87

You can use extension for it like:

extension String {
     var length: Int { return count(self)         }  // Swift 1.2
}

and you can use it:

if mystr.length >= 3 {

}

Or you can directly count this way:

if count(mystr) >= 3{

}

And this is also working for me :

if count(mystr.utf16) >= 3 {

}

For Swift 2.0:

extension String {
    var length: Int {
        return characters.count
    }
}
let str = "Hello, World"
str.length  //12

Another extension:

extension String {
    var length: Int {
        return (self as NSString).length
    }
}
let str = "Hello, World"
str.length //12

If you want direct use:

let str: String = "Hello, World"
print(str.characters.count) // 12

let str1: String = "Hello, World"
print(str1.endIndex) // 12

let str2 = "Hello, World"
NSString(string: str2).length  //12
Pharynx answered 11/4, 2015 at 7:18 Comment(3)
Yeah I don't understand OP's error, its working for me too.Lymph
Thank you for the suggestion. Even with count(mystr) I see the error '(String) -> _' is not identical to 'Int16'.Breeks
Ah, I found it. The NSManagedObject based class had a field called "count" defined as a Int16 that was overriding the global count function. Thank you for the sanity check.Breeks
O
23

You have to use characters property that contains the property count :

yourString.characters.count

Optician answered 21/9, 2015 at 21:37 Comment(0)
F
7

Swift 2.0 UPDATE

extension String {
    var count: Int { return self.characters.count }
}

Use:

var str = "I love Swift 2.0!"
var n = str.count

Helpful Progamming Tips and Hacks

Fazeli answered 28/9, 2015 at 13:6 Comment(0)
R
6

Here is all in one -- copied from here

let str = "Hello"
let count = str.length    // returns 5 (Int)

extension String {
    var length: Int { return countElements(self) }  // Swift 1.1
}
extension String {
    var length: Int { return count(self)         }  // Swift 1.2
}
extension String {
    var length: Int { return characters.count    }  // Swift 2.0
}
Razo answered 16/10, 2015 at 8:21 Comment(0)
L
2

count(mystr) is the correct way, you do not need to convert the encoding.

This: if count(mystr.utf16) >= 3 is fine as long as you do Int16(3)

Edit: this is an old answer. OP updated his question to reflect Swift 2 and the above answer is correct.

Lymph answered 11/4, 2015 at 7:13 Comment(3)
Thank you for the suggestion, but that gives me the error '(String) -> _' is not identical to 'Int16'.Breeks
Not the recommended way for doing that in Swift 2, instead see the @Dharmesh Kheni's answer above.Bobbobb
@Bobbobb I know, I saw it half a year ago.Lymph

© 2022 - 2024 — McMap. All rights reserved.