arrays - Swift sequences prefix(while:) return not all elements -


i have issue. testing usage of prefix(while:) , receive not expected result.count of elements after prefix(while:) missing 1 element. code possible see structures name has prefix oleg 5 after prefix(while:) returns 4.

example :

struct user {     let name: string }  let users = [user(name : "oleg 1"),user(name : "oleg 2"),user(name : "oleg 3"),user(name : "oleg 4"),user(name : "igor 1"),user(name : "oleg 5"),user(name : "max 1")]   print(users.prefix { $0.name.hasprefix("oleg") }.count) //4 

from documentation array prefix method:

returns subsequence containing initial elements until predicate returns false , skipping remaining elements.

when code gets "igor 1" instance, prefix stops , returns first subrange giving result of 4.

don't confuse array prefix method , string hasprefix method. used 2 different things.

perhaps want use filter.

print(users.filter { $0.name.hasprefix("oleg") }.count) // 5 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -