Uffo
Uffo

Reputation: 10046

Use of unresolved identifier 'advance' swift3

enter image description here

As far as I understood things(advance()) have changed in swift 3, but I don't manage to get it to work, whats the proper way to do it in swift 3?

        // Set the note text as the default post message.
        if (self.full_description.text?.characters.count)! <= 140 {
            twitterComposeVC?.setInitialText("\(self.full_description.text)")
        }
        else {
            let index = advance(self.noteTextview.text.startIndex, 140)
            let subText = self.noteTextview.text.substringToIndex(index)
            twitterComposeVC.setInitialText("\(subText)")
        }

Upvotes: 0

Views: 1322

Answers (1)

vacawama
vacawama

Reputation: 154603

Updated for Swift 4.x:

You need to use the String method index(_:offsetBy:) to advance the index and replace the deprecated substring(to:) with a String slicing subscript with a 'partial range upto' operator:

let index = self.noteTextview.text.index(self.noteTextview.text.startIndex, offsetBy: 140)
let subText = String(self.noteTextview.text[..<index])

Another way to get the first 140 characters of your String would be:

let subText = String(self.noteTextview.text.prefix(140))

Upvotes: 3

Related Questions