Error. Unrecognized selector to instance. (Swift) - ios

I've been looking at similar questions, but I still don't understand where I'm going wrong in my situation. Getting this error:
MyAwesomeApp[3481:1330310] -[Session setClient:]: unrecognized selector sent to instance 0x16e05de0
2015-03-15 07:30:29.990 MyAwesomeApp[3481:1330310] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Session setClient:]: unrecognized selector sent to instance 0x16e05de0'
*** First throw call stack:
(0x28e1045f 0x36ca6c8b 0x28e15879 0x28e13797 0x28d45008 0x646c4 0x628b4 0x62ba4 0x2c35de7f 0x2c4c1b35 0x2c35de7f 0x2c35de21 0x2c348c43 0x2c35d88d 0x2c35d567 0x2c356e31 0x2c32d759 0x2c5a12f9 0x2c32c1a9 0x28dd6fbf 0x28dd63cf 0x28dd4a35 0x28d223b1 0x28d221c3 0x3034f201 0x2c38c43d 0xa7a24 0xa7a60 0x37232aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
When I execute this code:
#IBAction func saveButton(sender: AnyObject) {
if session != nil {
editSession()
} else {
createSession()
}
navigationController?.popViewControllerAnimated(true)
}
func editSession() {
session?.sessionTitle = sessionTitleTxt.text
managedObjectContext?.save(nil)
}
func createSession() {
let entityDescription = NSEntityDescription.entityForName("Session", inManagedObjectContext: managedObjectContext!)
let newSession = Session(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext)
if sessionTitleTxt.text == "" {
newSession.sessionTitle = "Untitled Session"
} else {
newSession.sessionTitle = sessionTitleTxt.text
newSession.client = self.client!
}
managedObjectContext?.save(nil)
SessionsTVC?.client = newSession.client
}
Session class is:
import Foundation
import CoreData
#objc(Session)
class Session: NSManagedObject {
#NSManaged var sessionDate: String
#NSManaged var sessionTitle: String
#NSManaged var sessionNotes: String
#NSManaged var sessionExercises: NSSet
#NSManaged var client: Client
}
Can anyone help me out? I was getting a nil for the client variable before because I have several segues that I had confused, but now I'm getting this selector error and not sure how to move forward.
Thanks for any suggestions!

The error you are getting,
MyAwesomeApp[3481:1330310] -[Session setClient:]: unrecognized selector sent to instance 0x16e05de0
2015-03-15 07:30:29.990 MyAwesomeApp[3481:1330310] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Session setClient:]: unrecognized selector sent to instance 0x16e05de0'
happens when a method that is to be executed cannot be found. This can be thrown either because a method does not exist, or it is being called on the wrong object. In your specific case, the method setClientis being called on the Session object, which is being stored at the memory address of 0x16e05de0
Make sure that the setClient method exists and is supposed to be called on that Session object.
Here is a good link to help you with debugging:
http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1
Hope this helps!!

Related

Swift: nskeyedunarchiver fails and terminates app with 'NSInvalidArgumentException', reason: '-[__NSCFData count]

NSKeyedUnarchiver.UnarchivedArrayOfObjects() terminates app with 'NSInvalidArgumentException', reason: '-[__NSCFData count]
track conforms to NSManagedObject
However decodedTrackLocation is declared #Published public var decodedTrackLocation: [CLLocation] = [], as it is not persistent, but supposed to be in memory.
The below code works on certain 'saves', but sometimes it throws an exception and I can't figure out what to do about it.
It crashes at 'if let final =' and throws this exception:
2021-06-01 17:42:28.052628+0200 GeoTrack[12300:1124356] -[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50
2021-06-01 17:42:28.061055+0200 GeoTrack[12300:1124356] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50'
*** First throw call stack:
(0x1a9e27298 0x1bdb81480 0x1a9d362a8 0x1a9e298f4 0x1a9e2b89c 0x1ad67168c 0x1ad707e24 0x1026e5988 0x102704310 0x1b0345f94 0x1b0345fb0 0x1b0345f94 0x1b0737d74 0x1b0733e68 0x1b02225f4 0x1b088b190 0x1031f7ae8 0x102ee7ae8 0x102ee932c 0x102ef776c 0x1a9da62e0 0x1a9da0740 0x1a9d9f818 0x1c04a5570 0x1ac6cb0e8 0x1ac6d0664 0x1025696dc 0x1a9a7e140)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50'
terminating with uncaught exception of type NSException
The reason the NSKeyedUnarchiver fails is because I did something stupid, I stored the wrong Data. What I can't grasp is why the do-try-catch block this code lives in, doesn't work/catch.
Any help would be appreciated.
The code in Question:
do{
let out = try Data(referencing: NSData(data: track.trackLocation).decompressed(using: .lzma))
let archiver = try NSKeyedUnarchiver(forReadingFrom: out)
archiver.requiresSecureCoding = true
archiver.decodingFailurePolicy = .setErrorAndReturn
if let final = archiver.decodeArrayOfObjects(ofClass: CLLocation.self, forKey: NSKeyedArchiveRootObjectKey){
track.decodedTrackLocation = final
}
} catch {
print(error)
}
do{
let out = try Data(referencing: NSData(data: track.trackLocation).decompressed(using: .lzma))
if let final = try NSKeyedUnarchiver.unarchivedArrayOfObjects(ofClass: CLLocation.self, from: out){
track.decodedTrackLocation = final
}
} catch {
print(error)
}
Because it's not an Error that is thrown (throws), it's a NSInvalidArgumentException, a NSException, on Objective-C one. You can look for it :) - Larme
I learned something new!

import AlgoliaSearch with Swift 2 causing error

Adding this line to my App import AlgoliaSearch returns this error:
2017-05-08 22:35:07.814593 XXXXXX[6255:2627487] -[__NSDictionaryI
length]: unrecognized selector sent to instance 0x1702f0980 2017-05-08
22:35:07.816565 XXXXXX[6255:2627487] * Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSDictionaryI length]: unrecognized selector sent to instance
0x1702f0980'
* First throw call stack: (0x192f351b8 0x19196c55c 0x192f3c268 0x192f39270 0x192e3280c 0x1015c8608 0x10158d72c 0x10194a198
0x1001f6c24 0x1001f8c9c 0x100091fd4 0x1003973d4 0x100397690
0x1003be2c0 0x101a5d258 0x101a5d218 0x101a62280 0x192ee2810
0x192ee03fc 0x192e0e2b8 0x1948c2198 0x198e557fc 0x198e50534
0x10013c734 0x191df15b8) libc++abi.dylib: terminating with uncaught
exception of type NSException (lldb)
The line of code it is referring to is as below:
self.ref.child("users").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
for user in snapshot.children{
var numVids = NSDictionary()
if ((user.hasChild("videos")) == true){
--> numVids = user.value["videos"] as! NSDictionary
}
}
As soon as I remove the import Algolia module the error is removed, I have tried changing the version of the module and even switching to use the objc version but the error was the same.
Anyone seen something like this before?
https://github.com/algolia/algoliasearch-client-swift
https://github.com/algolia/algoliasearch-client-objc

Swift 3 timed event

I have the following:
let processURLS = processingViewController()
Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(processURLS.getURLsToSend),
userInfo: nil,
repeats: true)
When this event is triggered, I received the following error and I'm not sure why this doesn't work
2016-11-09 14:47:00.504932 AcumenJLR[3414:905978] * Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'-[AcumenJLR.homeViewController getURLsToSend]: unrecognized selector
sent to instance 0x100d25c60'
* First throw call stack: (0x1816721c0 0x1800ac55c 0x181679278 0x181676278 0x18157059c 0x18215c8f8 0x1816208f4 0x181620608
0x18161fec4 0x18161dac0 0x18154c048 0x182fd2198 0x1875372fc
0x187532034 0x100114620 0x1805305b8) libc++abi.dylib: terminating with
uncaught exception of type NSException
Here is the getURLsToSend method
func getURLsToSend () {
//create a fetch request, telling it about the entity
let fetchRequest: NSFetchRequest<URLsToSend> = URLsToSend.fetchRequest()
let context = getContext()
do {
//Get results
let searchResults = try getContext().fetch(fetchRequest)
print ("num of results = \(searchResults.count)")
//You need to convert to NSManagedObject to use 'for' loops
for urls in searchResults as [NSManagedObject] {
//get the Key Value pairs (although there may be a better way to do that...
//print("\(urls.value(forKey: "url"))")
let currentURL = urls.value(forKey: "url")!
//print(urls.value(forKey: "url")!)
completeLoadAction(urlString: currentURL as! String) { code in
if (code == 200){
context.delete(urls)
}
}
}
} catch {
print("Error with request: \(error)")
}
According to the target / action pattern the method specified in selector must be declared in the class specified in target in this case the current class self.
Either change the target or implement the method in self.
May be you have some paramter in method but you are not sending any value from there

CKModifyBadgeOperation crashes for no reason

I have created a subscription for insert & it sends push notification as follows
let subscriptionNotification = CKNotificationInfo()
subscriptionNotification.alertBody = "New Book has been created by Author"
subscriptionNotification.shouldBadge = true
subscription.notificationInfo = subscriptionNotification
When I receive the notification I open the app & I am simply trying to reset badge to zero
let resetBadge = CKModifyBadgeOperation(badgeValue: 0)
resetBadge.modifyBadgeCompletionBlock = {(error) in
if error != nil {
print(error)
}
else {
application.applicationIconBadgeNumber = 0
}
}
CKContainer.defaultContainer().addOperation(resetBadge)
I run this code on appdelegate did finish launching with options method.
I get following error. Keep in mind the name of the project is ckreference demo.
2015-12-15 21:41:58.417 ckreference demo[2486:1478837] -[CKNotification queryNotificationReason]: unrecognized selector sent to instance 0x15cd8a960
2015-12-15 21:41:58.427 ckreference demo[2486:1478837] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CKNotification queryNotificationReason]: unrecognized selector sent to instance 0x15cd8a960'
* First throw call stack:
(0x183521900 0x182b8ff80 0x18352861c 0x1835255b8 0x18342968c 0x1000b8b9c 0x1000b9250 0x1884cbae4 0x1884b9de0 0x184ad7790 0x184ad7b10 0x1834d8efc 0x1834d8990 0x1834d6690 0x183405680 0x184914088 0x18827cd90 0x1000b94d0 0x182fa68b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
Any suggestions on how to stop app from crashing?
The crash happens here

Updating an object on realm

I want to update an object with realm. But my app is crashing.
My code:
var findConversations = ShufflePerson.objectsWhere("receiver='\(from)'")
var shuf = findConversations[0] as! ShufflePerson
shuf.unreadMessage=shuf.unreadMessage++
self.realm.beginWriteTransaction()
self.realm.commitWriteTransaction()
Crash Log:
Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'
How can I fix ?
Try the following:
var findConversations = ShufflePerson.objectsWhere("receiver='\(from)'")
var shuf = findConversations[0] as! ShufflePerson
self.realm.beginWriteTransaction()
shuf.unreadMessage++
self.realm.commitWriteTransaction()
The issue, as the exception message said, was that you were modifying the object outside of the write transaction. Doing that mutation inside the transaction should do the trick!

Resources