Swift 3: Unrecognized Selector Sent to Instance Xcode 8

Swift 3: Unrecognized selector sent to instance Xcode 8

If you are using swift 3 your selector should be like this

#selector(self.handleTap(recognizer:))

Swift 3 Xcode 8 - SwiftValue encodeWithCoder - unrecognized selector sent to instance

The problem is that you are trying to archive an optional. Replace this line:

if (object != nil) {

with:

if let object = object {

Unrecognized selector sent to instance error when pressing UIButton in Xcode 8/Swift 3

It seems like you have rename your IBAction method name earlier it was different and it is connected to the previous name inside your storyboard. So just disconnect your action method and reconnect it appropriately.

Swift unrecognized selector sent to instance error

Two changes for Swift 3:

The selector should look like:

#selector(ClassName.followButtonClick(_:))

The function should have an underscore:

@IBAction func followButtonClick(_ sender: UIButton!) { ...

Notice that these two should be in the same class, otherwise, make sure you initialize the ClassName class.

If you want the selector method(followButtonClick(_:)) to be in the UITableViewCell class. Remove @IBAction(I don't think you need it there):

func followButtonClick(_ sender: UIButton!) { ... 

Unrecognized selector sent to instance 0x7fecc7011000 In Swift

The problem is this outlet:

IBOutlet weak var tableView: UITableView!

You have hooked it up in the storyboard to the wrong object. It is hooked to the cell, not the table view. It looks like you must have hooked it up and then changed the class of the thing it is hooked to.

How to fix 'unrecognized selector sent to instance' in Swift 4

A class which adopts NSCoding(not NSCoder) must be a subclass of NSObject.

class Items: NSObject, NSCoding { ...

But in Swift 4 it's highly recommended to drop the ObjC runtime and use lightweight Codable protocol to serialize custom structs or classes to Property List or JSON.


The class can be reduced to a struct

struct Item : Codable {
var title : String
var completed : Bool
}

Load data :

guard let data = UserDefaults.standard.data(forKey: "list") else {
self.list = []
return
}
do {
self.list = try JSONDecoder().decode([Item].self, from: data)
} catch {
print(error)
self.list = []
}

Save data :

do {
let itemData = try JSONEncoder().encode(list)
UserDefaults.standard.set(itemData, forKey: "list")
} catch {
print(error)
}

A few notes:

  • Never declare properties as implicit unwrapped optional which are initialized in init methods. Either use regular optional ? or non-optional.
  • This is Swift: No trailing semicolons.
  • Structs and classes are supposed to be named in singular form (Item).


Related Topics



Leave a reply



Submit