ahmed ahmed - 5 months ago 38
Swift Question

Swift 3 updates that cause error

I updated to swift 3.0 and I get these two errors please help. Theres the code and images below

I get an error at self.blockfinish

var blockFinish: ((_ selectedTags: Array<Tag>, _ unSelectedTags: Array<Tag>) -> ())!

func finishTagController() {
var selected: Array<Tag> = Array()
var unSelected: Array<Tag> = Array()

for currentTag in tags {
if currentTag.isSelected {
else {
self.dismissViewControllerAnimated(true, completion: { () -> Void in
self.blockFinish(selectedTags: selected, unSelectedTags: unSelected)

Here I get an error at let ("addTagView.textEdit.text.stringByTrimmingCharactersInSet(spaceSet)")

func createNewTag() {
let spaceSet = NSCharacterSet.whitespaces
let contentTag = addTagView.textEdit.text.stringByTrimmingCharactersInSet(spaceSet)
if strlen(contentTag) > 0 {
let newTag = Tag(isSelected: false, isLocked: false, textContent: contentTag)
tags.insert(newTag, atIndex: tags.count)

Error image


The signature for the string trimming method has changed in Swift 3.0. Here is the new method:

func trimmingCharacters(in set: CharacterSet) -> String

So instead of:

let contentTag = addTagView.textEdit.text.stringByTrimmingCharactersInSet(spaceSet)

you would do this:

let contentTag = addTagView.textEdit.text.trimmingCharacters(in: spaceSet)

edit: As for the other error, you have to provide the declaration for blockFinish, I can't help otherwise.