Search code examples
swiftdidsetproperty-observer

How can I set a string variable and make it always lowercase?


I want to set a string variable and want to keep it always lowercase.

This is my code :

var alwaysLowercaseString : String? {

    didSet{
        alwaysLowercaseString = alwaysLowerCaseString!.lowercaseString
    }
}

But when I use it, it goes into a infinite loop. How can I solve this problem?


Solution

  • I stand corrected, this is the correct approach. LeoDabus deserves the credit for this answer:

    var alwaysLowercaseString : String? {
        
        didSet{
       
        alwaysLowercaseString = alwaysLowercaseString?.lowercaseString
          print(alwaysLowercaseString)
        }
    }