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?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you mean by the term “defer”?

482


Do swift classes inherit from nsobject?

453


Explain the common execution states for a swift ios app (ios application lifecycle).

449


What is swift? How is it different from objective-c?

488


What is the latest version of swfit programming?

459






What language is similar to swift?

451


What is “defer”?

514


What is final class in swift?

540


What is a type in swift?

502


What is the floating point numbers and what are the types of floating number in swift?

483


Is swift pass by reference?

436


Which banks use swift?

430


Does swift have abstract classes?

457


What is swift nsarray?

460


What are the collection types available in swift?

440