How to append a character to a string in Swift?

后端 未结 8 1839
长情又很酷
长情又很酷 2021-01-01 10:15

This used to work in Xcode 6: Beta 5. Now I\'m getting a compilation error in Beta 6.

for aCharacter: Character in aString {
    var str: String = \"\"
    v         


        
8条回答
  •  误落风尘
    2021-01-01 10:29

    I had to get initials from first and last names, and join them together. Using bits and pieces of the above answers, this worked for me:

      var initial: String = ""
    
                if !givenName.isEmpty {
                    let char = (givenName as NSString).substring(with: NSMakeRange(0, 1))
                   let str = String(char)
                    initial.append(str)
                }
    
                if !familyName.isEmpty {
                     let char = (familyName as NSString).substring(with: NSMakeRange(0, 1))
                    let str = String(char)
                    initial.append(str)
                }
    

提交回复
热议问题