The String struct doesn’t provide a count or length property or method to count the number of characters it contains. Instead a global countElements<T>() function is available. When applied to strings, what’s the complexity of the countElements function:

O(1)

O(n)

and why?



The String struct doesn’t provide a count or length property or method to count the number of ..

Answer / iosraj

Swift strings support extended grapheme clusters. Each character stored in a string is a sequence of one or more unicode scalars that, when combined, produce a single human readable character. Since different characters can require different amounts of memory, and considering that an extreme grapheme cluster must be accessed sequentially in order to determine which character it represents, it’s not possible to know the number of characters contained in a string upfront, without traversing the entire string. For that reason, the complexity of the countElements function is O(n).

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Apple iOS Swift Interview Questions

What is the difference between swift and ‘objective-c’ language?

0 Answers  


Which banks use swift?

0 Answers  


what are functions different from methods?

0 Answers  


What are the advantages of swift?

0 Answers  


How do you make a bridging header in swift?

0 Answers  






Explain how multiple line comment can be written in swift?

1 Answers  


What are jsonencoder and jsondecoder?

0 Answers  


What are the control transfer statements in swift?

0 Answers  


What is the full meaning of swift?

0 Answers  


What are type methods in swift?

0 Answers  


What is init() in swift?

0 Answers  


How can you define a base class in swift?

0 Answers  


Categories