0

How can i capitalized each first letter of the result of this

self.namE.text = currentUser.displayName

self.handle.text = snapshotValue?["handle"] as? String

instead of "Bruce willis" i would like to have "Bruce Willis", i created this extension to capitalized the first letter

extension String {
    func capitalizingFirstLetter() -> String {
        return prefix(1).uppercased() + dropFirst()
   }
}

but it obviously capitalize only the first word in a string, so how i have to modify this extension to get the right result? ( i looked in the doc but i didn't understand very well )

User672
  • 67
  • 1
  • 7

1 Answers1

0

String in swift4 has already a capitalized computed property on itself, so without implementing anything yourself, you can get the desired result using this:

self.namE.text = currentUser.displayName.capitalized

E.g.:

self.namE.text = "bruce willis".capitalized
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90