Hans Ben
Hans Ben

Reputation: 91

NSObject setValuesForKeys coding-complaint error in Swift 4.0.2

I usually would create an NSObject like the example below make a firebase observe single event call to get data, then I would create an instance of the NSObject and simply call the setValueForKeys method on the NSObject and the values will be passed in then I could easily use an if let statement to get the specific data I required. this has stopped working since I upgraded to swift 4.0.2 from swift 3.1 see code snippet below. I believe I am doing this wrong way, since the new update. As the key value it requires does exist as can be seen in the NSObject declaration and also the print out. Any suggestion will be appreciated.

class BarberMarker: NSObject {
var companyID: String?
var companyName: String?
var companyLogoImageUrl: String?
var companyLogoImage: UIImage?
var companyFormattedAddress: String?
var companyAddressLongitude: String?
var companyAddressLatitude: String?
var distanceFromCurrentUser: Double?
var dateCreated: String?
var timezone: String?
var calendar: String?
var local: String?
var ratingValue: CGFloat?
}

firebase call to get data from database

    func getAllMarkers(){
    firebaseRef.child("barberMarkers").observeSingleEvent(of: .value, with: { (snapshotssshhh) in
        if let dictionary = snapshotssshhh.value as? [String: AnyObject] {

            for marker in dictionary {
                if let locMarker = marker.value as? [String: AnyObject] {
                    var markerB = BarberMarker()
                    print(locMarker)
                    markerB.setValuesForKeys(locMarker)
                    self.barbermarkers.append(markerB)
                    guard let lon = markerB.companyAddressLongitude, let lat = markerB.companyAddressLatitude else {
                        return
                    }
                    let latitude = (lat as NSString).doubleValue
                    let longitude = (lon as NSString).doubleValue

                    let locValue:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
                    DispatchQueue.main.async{
                        let desiredMarker = GMSMarker(position: locValue)
                        desiredMarker.icon = GMSMarker.markerImage(with: UIColor(r: 118, g: 187, b: 220))
                        desiredMarker.map = self.mapView
                        self.bMarkers.append(desiredMarker)
                    }
                }
            }
        }
    }, withCancel: nil)
}

error message I get

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<maizewars.BarberMarker 0x7fb62ff5f5b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key companyAddressLongitude.'

Upvotes: 1

Views: 319

Answers (1)

AshvinGudaliya
AshvinGudaliya

Reputation: 3314

try this bro.

@objcMembers class BarberMarker: NSObject {
...
}

reason @objcMembers Use in Swift 4

When a Swift class introduces many new methods or properties that require behavior from the Objective-C runtime, use the @objcMembers attribute in the declaration of that class.

Applying the @objcMembers attribute to a class implicitly adds the @objc attribute to all of its Objective-C compatible members.

Because applying the @objc attribute can increase the compiled size of an app and adversely affect performance, only apply the @objcMembers attribute on declarations when each member needs to have the @objc attribute applied.

Upvotes: 3

Related Questions