0

Hey I am having a problem trying to convert this old code into swift 3:

func textViewDidChange(textView: UITextView) {

           let components = self.txtBody.text.componentsSeparatedByString("    \n") 
           self.navigationItem.title = ""

           for item in components {

             if countElements(item.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines())) > 0 {


               self.navigationItem.title = item  

              break
     }
   }
 }

The problem is the countElements is not correct as it is not recognised in Swift. I am unsure how to convert it to swift 3

Any help or guidance would be great thanks

Harraj

  • Convert countElements to count –  Nov 17 '16 at 14:11
  • so simply change countElements to count?? – Harraj Singh Mann Nov 17 '16 at 14:14
  • @HarrajSinghMann Yes like this `item.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines()).count` – Nirav D Nov 17 '16 at 14:14
  • @HarrajSinghMann, no, `countElements` is function, `count` is property. And I thought it changed in Swift 2. Your code looks very 1.0ish – user28434'mstep Nov 17 '16 at 14:15
  • @user28434 the code is from Swift 1.0 I am following a tutorial trying to convert it to swift 3 you see... I've managed to do all of it but I am stuck on this bit.. – Harraj Singh Mann Nov 17 '16 at 14:16
  • @HarrajSinghMann user28434 is right: count has been for some time a global function, then was definetely converted to a property –  Nov 17 '16 at 14:18
  • http://stackoverflow.com/questions/24037711/get-the-length-of-a-string –  Nov 17 '16 at 14:22
  • Thanks for the help guys! Managed to fix it and run with no errors.. only problem is that It is not doing what it is intended to do in the tutorial :( It won't edit my title for the notes.. – Harraj Singh Mann Nov 17 '16 at 14:31
  • @HarrajSinghMann Post another question –  Nov 18 '16 at 15:08

0 Answers0