I've created an application in xcode that gets the location of a place from a user longclick on a map and inserte into a database. i keep getting this error
Travel Map Book[17935:3752754] *** Terminating app due to uncaught exception
'NSUnknownKeyException', reason: '[<Locations 0x60800009f630>
valueForUndefinedKey:]: the entity Locations is not key value coding-compliant
for the key "longtitude".'
*** First throw call stack:
(
0 CoreFoundation 0x00000001065d2b0b
__exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000102870141 objc_exception_throw + 48
2 CoreFoundation 0x00000001065d2a59 -[NSException raise] + 9
3 CoreData 0x0000000102de121b -[NSManagedObject valueForUndefinedKey:] + 299
4 Travel Map Book 0x00000001021e04d7 _TFC15Travel_Map_Book19FirstViewController9fetchDatafT_T_ + 7159
5 Travel Map Book 0x00000001021de18a _TFC15Travel_Map_Book19FirstViewController11viewDidLoadfT_T_ + 490
6 Travel Map Book 0x00000001021de202 _TToFC15Travel_Map_Book19FirstViewController11viewDidLoadfT_T_ + 34
7 UIKit 0x00000001036ab01a -[UIViewController loadViewIfRequired] + 1235
8 UIKit 0x00000001036e9e6c -[UINavigationController _layoutViewController:] + 56
9 UIKit 0x00000001036ea74a -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 466
10 UIKit 0x00000001036ea8bb -[UINavigationController _startTransition:fromViewController:toViewController:] + 127
11 UIKit 0x00000001036eba03 -[UINavigationController _startDeferredTransitionIfNeeded:] + 843
12 UIKit 0x00000001036ecb41 -[UINavigationController __viewWillLayoutSubviews] + 58
13 UIKit 0x00000001038de60c -[UILayoutContainerView layoutSubviews] + 231
14 UIKit 0x00000001035cb55b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1268
15 QuartzCore 0x000000010b7bd904 -[CALayer layoutSublayers] + 146
16 QuartzCore 0x000000010b7b1526 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 370
17 QuartzCore 0x000000010b7b13a0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
18 QuartzCore 0x000000010b740e92 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
19 QuartzCore 0x000000010b76d130 _ZN2CA11Transaction6commitEv + 468
20 QuartzCore 0x000000010b76db37 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 115
21 CoreFoundation 0x0000000106578717 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
22 CoreFoundation 0x0000000106578687 __CFRunLoopDoObservers + 391
23 CoreFoundation 0x000000010655d038 CFRunLoopRunSpecific + 440
24 UIKit 0x000000010350208f -[UIApplication _run] + 468
25 UIKit 0x0000000103508134 UIApplicationMain + 159
26 Travel Map Book 0x00000001021dd177 main + 55
27 libdyld.dylib 0x0000000106f9f65d start + 1
28 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
I have tried cross-checking every spelling and data-type, still no luck with it. this is my fetchdata function
func fetchData(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Locations")
request.returnsObjectsAsFaults = false
do {
let results = try context.fetch(request)
if results.count > 0{
self.titleArray.removeAll(keepingCapacity: false)
self.subtitleArray.removeAll(keepingCapacity: false)
self.longtitudeArray.removeAll(keepingCapacity: false)
self.latitudeArray.removeAll(keepingCapacity: false)
for result in results as! [NSManagedObject]{
if let title = result.value(forKey: "title") as? String{
self.titleArray.append(title)
}
if let subtitle = result.value(forKey: "subtitle") as? String{
self.subtitleArray.append(subtitle)
}
if let latitude = result.value(forKey: "latitude") as? Double{
self.latitudeArray.append(latitude)
}
if let longtitude = result.value(forKey: "longitude") as? Double{
self.longtitudeArray.append(longtitude)
}
self.tableView.reloadData()
}
}
} catch {
print("error")
}
}
Related
I have a problem with Place Picker in my simulator. it is crashing with
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'
*** First throw call stack:
(
0 CoreFoundation 0x000000010c15dd4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010bbbf21e objc_exception_throw + 48
2 CoreFoundation 0x000000010c07415f -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 351
3 CoreFoundation 0x000000010c073fcb +[NSDictionary dictionaryWithObjects:forKeys:count:] + 59
4 myApp 0x0000000106f64c58 -[GMSPlatformKeyInsertingPlacesServerClient setPlacesService:] + 376
5 myApp 0x0000000106f64ac6 -[GMSPlatformKeyInsertingPlacesServerClient placesService] + 104
6 myApp 0x0000000106f64163 -[GMSPlatformKeyInsertingPlacesServerClient executeQuery:completionHandler:] + 88
7 myApp 0x0000000106f639f3 -[GMSPlacesServerClient accountPlacePickerSessionWithRequestSource:callback:] + 343
8 myApp 0x0000000106f88d60 -[GMSPlacesClient accountPlacePickerSessionWithCallback:] + 147
9 myApp 0x0000000106f58564 -[GMSPlacePicker pickPlaceWithResultCallback:didDisappearCallback:] + 887
10 myApp 0x0000000106b12273 _TFC6Medico11NewAdressVC13chooseAddressfT_T_ + 259
11 myApp 0x0000000106b11449 _TFC6Medico11NewAdressVC11viewDidLoadfT_T_ + 985
12 myApp 0x0000000106b12152 _TToFC6Medico11NewAdressVC11viewDidLoadfT_T_ + 34
13 UIKit 0x0000000109c11a3d -[UIViewController loadViewIfRequired] + 1258
14 UIKit 0x0000000109c18062 -[UIViewController __viewWillAppear:] + 118
15 UIKit 0x0000000109c431d3 -[UINavigationController _startCustomTransition:] + 1290
16 UIKit 0x0000000109c53e48 -[UINavigationController _startDeferredTransitionIfNeeded:] + 697
17 UIKit 0x0000000109c54fdb -[UINavigationController __viewWillLayoutSubviews] + 58
18 UIKit 0x0000000109e4bdd7 -[UILayoutContainerView layoutSubviews] + 223
19 UIKit 0x0000000109b34ab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
20 QuartzCore 0x0000000109686bf8 -[CALayer layoutSublayers] + 146
21 QuartzCore 0x000000010967a440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
22 QuartzCore 0x000000010967a2be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
23 QuartzCore 0x0000000109608318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
24 QuartzCore 0x00000001096353ff _ZN2CA11Transaction6commitEv + 475
25 QuartzCore 0x0000000109635d6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
26 CoreFoundation 0x000000010c102267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
27 CoreFoundation 0x000000010c1021d7 __CFRunLoopDoObservers + 391
28 CoreFoundation 0x000000010c0e6f8e __CFRunLoopRun + 1198
29 CoreFoundation 0x000000010c0e6884 CFRunLoopRunSpecific + 420
30 GraphicsServices 0x000000010ec0ea6f GSEventRunModal + 161
31 UIKit 0x0000000109a6fc68 UIApplicationMain + 159
32 myApp 0x0000000106ba8c2f main + 111
33 libdyld.dylib 0x000000010d1a468d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I don't know why it is crashing, because it start crashing yesterday. But on device all ok
Here is my code, but even if I put this callback empty it still crashing. I don't know what to do
let config = GMSPlacePickerConfig(viewport: nil)
let placePicker = GMSPlacePicker(config: config)
placePicker.pickPlace(callback: { (place: GMSPlace?, error: Error?) -> Void in
if let error = error {
print("Pick Place error: \(error.localizedDescription)")
return
}
guard let place = place else {
print("No place selected")
return
}
let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 18)
self.mapView.camera = camera
self.marker.position = place.coordinate
self.marker.map = self.mapView
self.marker.appearAnimation = .pop
print("#Place address \(place.formattedAddress)")
if let addr = place.formattedAddress {
self.addressString = addr
}
self.updateAddressCell(place)
self.lastSelectedPlace = place
self.saveChangesButton.isHidden = false
})
I'm kind of new to Swift, and I was creating a program where I needed to use a segue through code. I made sure the identifier was correct, and I am in the right view controller. I tested to see if the button was the problem, and it wasn't. Here is the code and the error:
#IBAction func doneButtonPressed(_ sender: UIButton) {
firstName = firstNameField.text!
lastName = lastNameField.text!
email = emailField.text!
let contact = Contact(first: firstName, last: lastName, email: email)
storageContacts.append(contact)
self.performSegue(withIdentifier: "Done", sender: sender)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationvc = segue.destination
if let contactController = destinationvc as? ContactsTableViewController{
if segue.identifier != nil{
contactController.contacts = storageContacts
}
}
}
Error(preparing, changed, pressed are print statements):
preparing
2016-12-09 15:07:23.337754 Contacts[11194:961012] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/nick/Library/Developer/CoreSimulator/Devices/7A8982FC-6CA4-4DB1-AA80-C58E83CDF7E0/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2016-12-09 15:07:23.339582 Contacts[11194:961012] [MC] Reading from private effective user settings.
changed
changed
changed
pressed
preparing
2016-12-09 15:07:29.354 Contacts[11194:961012] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Contacts.ContactTableViewCell 0x7fdf11820600> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key fullName.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000111ef734b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010f20b21e objc_exception_throw + 48
2 CoreFoundation 0x0000000111ef7299 -[NSException raise] + 9
3 Foundation 0x000000010ed1b26f -[NSObject(NSKeyValueCoding) setValue:forKey:] + 291
4 UIKit 0x000000010f79e80d -[UIView(CALayerDelegate) setValue:forKey:] + 173
5 UIKit 0x000000010fae879e -[UIRuntimeOutletConnection connect] + 109
6 CoreFoundation 0x0000000111e9c590 -[NSArray makeObjectsPerformSelector:] + 256
7 UIKit 0x000000010fae7122 -[UINib instantiateWithOwner:options:] + 1867
8 UIKit 0x000000010f833df4 -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 399
9 UIKit 0x000000010f8342ec -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 71
10 Contacts 0x000000010ec1b546 _TFC8Contacts27ContactsTableViewController9tableViewfTCSo11UITableView12cellForRowAtV10Foundation9IndexPath_CSo15UITableViewCell + 150
11 Contacts 0x000000010ec1b7c7 _TToFC8Contacts27ContactsTableViewController9tableViewfTCSo11UITableView12cellForRowAtV10Foundation9IndexPath_CSo15UITableViewCell + 87
12 UIKit 0x000000010f8477b5 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 757
13 UIKit 0x000000010f847a13 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
14 UIKit 0x000000010f81b47d -[UITableView _updateVisibleCellsNow:isRecursive:] + 3295
15 UIKit 0x000000010f850d95 -[UITableView _performWithCachedTraitCollection:] + 110
16 UIKit 0x000000010f8375ef -[UITableView layoutSubviews] + 222
17 UIKit 0x000000010f79ef50 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
18 QuartzCore 0x0000000114cadcc4 -[CALayer layoutSublayers] + 146
19 QuartzCore 0x0000000114ca1788 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
20 QuartzCore 0x0000000114ca1606 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
21 QuartzCore 0x0000000114c2f680 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
22 QuartzCore 0x0000000114c5c767 _ZN2CA11Transaction6commitEv + 475
23 QuartzCore 0x0000000114c5d0d7 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
24 CoreFoundation 0x0000000111e9be17 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
25 CoreFoundation 0x0000000111e9bd87 __CFRunLoopDoObservers + 391
26 CoreFoundation 0x0000000111e80b9e __CFRunLoopRun + 1198
27 CoreFoundation 0x0000000111e80494 CFRunLoopRunSpecific + 420
28 GraphicsServices 0x0000000113daca6f GSEventRunModal + 161
29 UIKit 0x000000010f6da964 UIApplicationMain + 159
30 Contacts 0x000000010ec20cdf main + 111
31 libdyld.dylib 0x0000000112e2468d start + 1
32 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I have 3 main VC (MyAccount, Register, Login). Then I have a Swift file(Manager.swift) where I set up a class function where after the registering or logging in it would dismiss the VC.
Manager.swift
class Manager {
class func registerUser(vc: UIViewController?, user_email : String, user_password : String){
let myUrl = NSURL(string: hostURL + "userRegister.php")
let request = NSMutableURLRequest(URL:myUrl!)
request.HTTPMethod = "POST";
// Compose a query string
let postString = "user_email=\(user_email)&user_password=\(user_password)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil
{
print("error=\(error)")
return
}
// You can print out response object
print("response = \(response)")
// Print out response body
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
//Let’s convert response sent from a server side script to a NSDictionary object:
let myJSON = try!NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary
if let parseJSON = myJSON {
let status = parseJSON["status"] as? String
let msg = parseJSON["message"] as? String
print("Status: \(status)")
if(status != "Success")
{
UIAlert.displayAlert(vc, title: "Error", message: msg!, dismissTxt: "OK")
}
else{
defaults.setBool(true, forKey: "isLoggedIn")
//My Failed attempt to go back to Root :C
vc?.navigationController?.popToRootViewControllerAnimated(true)
}
}
}
task.resume()
}
}
RegisterVC
buttonPressed(sender: AnyObject) {
Manger.registerUser(self, user_email:username, user_password:password1234)
}
I used navigationController?.popToRootViewControllerAnimated in an attempt to back
It would work but sometimes the app would crash, spitting out this:
2015-10-02 14:51:01.410 Retail_Template[4551:261846] * Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3505.16/Keyboard/UIKeyboardTaskQueue.m:378
2015-10-02 14:51:01.415 Retail_Template[4551:261846] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
*** First throw call stack:
(
0 CoreFoundation 0x009f8a94 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x02b26e02 objc_exception_throw + 50
2 CoreFoundation 0x009f892a +[NSException raise:format:arguments:] + 138
3 Foundation 0x010bd3e6 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118
4 UIKit 0x021f9d2e -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 180
5 UIKit 0x019e14f2 -[UIKeyboardImpl setDelegate:force:] + 703
6 UIKit 0x019e122e -[UIKeyboardImpl setDelegate:] + 60
7 UIKit 0x01ddd1d6 -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 1208
8 UIKit 0x01de6bef -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:reset:] + 502
9 UIKit 0x01de6c85 -[UIPeripheralHost(UIKitInternal) _preserveInputViewsWithId:animated:] + 57
10 UIKit 0x0190dd0a -[UINavigationController navigationTransitionView:didStartTransition:] + 1029
11 UIKit 0x01903bf4 -[UINavigationController _startCustomTransition:] + 5104
12 UIKit 0x01913c0b -[UINavigationController _startDeferredTransitionIfNeeded:] + 801
13 UIKit 0x01914d05 -[UINavigationController __viewWillLayoutSubviews] + 68
14 UIKit 0x01aded9f -[UILayoutContainerView layoutSubviews] + 252
15 UIKit 0x017cb16b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 813
16 libobjc.A.dylib 0x02b3b059 -[NSObject performSelector:withObject:] + 70
17 QuartzCore 0x003ed60c -[CALayer layoutSublayers] + 144
18 QuartzCore 0x003e128e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
19 QuartzCore 0x003e10f2 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
20 QuartzCore 0x003d3c2b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317
21 QuartzCore 0x00407c23 _ZN2CA11Transaction6commitEv + 589
22 QuartzCore 0x00407fbd _ZN2CA11Transaction14release_threadEPv + 289
23 libsystem_pthread.dylib 0x04bc32f7 _pthread_tsd_cleanup + 93
24 libsystem_pthread.dylib 0x04bc3051 _pthread_exit + 108
25 libsystem_pthread.dylib 0x04bc3734 pthread_get_stackaddr_np + 0
26 libsystem_pthread.dylib 0x04bc0e0e start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Not sure what I'm doing wrong here.
Any solution will be appreciated. Thanks!
As your error says:
may only be called from the main thread.
Your operation should be called into main thread. As shown into below code:
dispatch_async(dispatch_get_main_queue()) {
//Perform your task here.
}
I'm using Core Data and I'm making a save button with this code:
self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: Selector("saveTapped")), animated: true)
The save function:
func saveTapped(){
if (cell!.textField.text.isEmpty) {
let alert = UIAlertView()
alert.title = "Nevyplnené údaje"
alert.message = "Musíš vyplniť všetky údaje o knihe."
alert.addButtonWithTitle("Ok")
alert.show()
}
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let obsah: NSManagedObjectContext = appDel.managedObjectContext!
let entity = NSEntityDescription.entityForName("List", inManagedObjectContext: obsah)
var pridat = Model(entity: entity! , insertIntoManagedObjectContext: obsah)
pridat.kniha = cell!.textField.text
pridat.autor = cell!.textField.text
pridat.rok = cell!.textField.text
pridat.vydavatelstvo = cell!.textField.text
pridat.strany = cell!.textField.text
obsah.save(nil)
}
This is the error I'm getting:
2015-04-29 19:12:42.762 iKnižnica[25716:11689183]
-[iKniz_nica.PridatViewController saveTapped]: unrecognized selector sent to instance 0x7ff5ab63ff50 2015-04-29 19:12:42.851
iKnižnica[25716:11689183] * Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason:
'-[iKniz_nica.PridatViewController saveTapped]: unrecognized selector
sent to instance 0x7ff5ab63ff50'
* First throw call stack: ( 0 CoreFoundation 0x0000000100bccc65 exceptionPreprocess + 165 1 libobjc.A.dylib
0x0000000102737bb7 objc_exception_throw + 45 2 CoreFoundation
0x0000000100bd40ad -[NSObject(NSObject) doesNotRecognizeSelector:] +
205 3 CoreFoundation 0x0000000100b2a13c
___forwarding_ + 988 4 CoreFoundation 0x0000000100b29cd8 _CF_forwarding_prep_0 + 120 5 UIKit
0x000000010146cda2 -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x000000010146cda2
-[UIApplication sendAction:to:from:forEvent:] + 75 7 UIKit 0x000000010157e54a -[UIControl _sendActionsForEvents:withEvent:] + 467
8 UIKit 0x000000010157d919
-[UIControl touchesEnded:withEvent:] + 522 9 UIKit 0x00000001014b9998 -[UIWindow _sendTouchesForEvent:] + 735 10 UIKit
0x00000001014ba2c2 -[UIWindow sendEvent:] + 682 11 UIKit
0x0000000101480581 -[UIApplication sendEvent:] + 246 12 UIKit
0x000000010148dd1c _UIApplicationHandleEventFromQueueEvent + 18265 13
UIKit 0x00000001014685dc
_UIApplicationHandleEventQueue + 2066 14 CoreFoundation 0x0000000100b00431
CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 15 CoreFoundation 0x0000000100af62fd
__CFRunLoopDoSources0 + 269 16 CoreFoundation 0x0000000100af5934 __CFRunLoopRun + 868 17 CoreFoundation
0x0000000100af5366 CFRunLoopRunSpecific + 470 18 GraphicsServices
0x0000000104bb3a3e GSEventRunModal + 161 19 UIKit
0x000000010146b900 UIApplicationMain + 1282 20 iKnizÃånica
0x00000001005c6377 main + 135 21 libdyld.dylib
0x0000000102e8f145 start + 1 ) libc++abi.dylib: terminating with
uncaught exception of type NSException (lldb)
I posted the source code here: http://pastebin.com/M9AMakGr
Problem is not in the button settings, but in function
Instead of:
func saveTapped(){
Should be:
func saveTapped(sender:UIButton){
Change your setRightBarButtonItem code to below code:
self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "saveTapped:"), animated: true)
In swift, for selector you just need to type the name of the function, that's it! Hope it helps
I am getting the error [__NSCFArray objectForKey:]: unrecognized selector sent to instance when trying to fetch some data from my Core Data.
I am relatively new to iOS programming and don't quite understand what this error is trying to tell me. I set an exception breakpoint to figure out which line is giving me the problem and it is this line of code:
if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [User] {
Why am I getting this error and how do I resolve it? Is this related to the way I save my Core Data? I just recently did a change where I receive a JSON from the server, then process in the background and then finally save changes to my ManagedObjectContext in the main thread.
func getUser(userId: String) -> User? {
let fetchRequest = NSFetchRequest(entityName: "User")
fetchRequest.predicate = NSPredicate(format: "userId == %#", userId)
fetchRequest.fetchLimit = 1
if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [User] {
if !fetchResults.isEmpty {
return fetchResults[0]
}
else {
println("UserId \(userId) not in database")
}
}
return nil
}
Error
2014-12-06 13:09:36.087 LFDate[3162:51570] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7fb9e95c4310
2014-12-06 13:09:36.090 LFDate[3162:51570] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7fb9e95c4310'
*** First throw call stack:
(
0 CoreFoundation 0x00000001025c2f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010465ebb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001025ca04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010252227c ___forwarding___ + 988
4 CoreFoundation 0x0000000102521e18 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x000000010248beaf CFDictionaryGetValue + 159
6 Foundation 0x0000000102befd78 -[NSKeyedUnarchiver initForReadingWithData:] + 1698
7 Foundation 0x0000000102c135ea +[NSKeyedUnarchiver unarchiveObjectWithData:] + 66
8 CoreData 0x000000010210b4e0 _prepareResultsFromResultSet + 3552
9 CoreData 0x0000000102108930 newFetchedRowsForFetchPlan_MT + 3296
10 CoreData 0x00000001020f53ac -[NSSQLCore objectsForFetchRequest:inContext:] + 524
11 CoreData 0x00000001020f4e2b -[NSSQLCore executeRequest:withContext:error:] + 299
12 CoreData 0x00000001021ca4f3 __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 3331
13 CoreData 0x00000001021d27ee gutsOfBlockToNSPersistentStoreCoordinatorPerform + 190
14 libdispatch.dylib 0x00000001052117f4 _dispatch_client_callout + 8
15 libdispatch.dylib 0x00000001051f8774 _dispatch_barrier_sync_f_invoke + 365
16 CoreData 0x00000001021c53d5 _perform + 197
17 CoreData 0x00000001020f4ac8 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 504
18 CoreData 0x00000001020f33e0 -[NSManagedObjectContext executeFetchRequest:error:] + 544
19 LFDate 0x0000000101b9c3b3 _TFC6LFDate14DatabaseHelper7getUserfS0_FSSGSqCS_4User_ + 1075
20 LFDate 0x0000000101c4ce01 _TFC6LFDate23MenuTableViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 4689
21 LFDate 0x0000000101c4e4ff _TToFC6LFDate23MenuTableViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79
22 UIKit 0x000000010349d4b3 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
23 UIKit 0x000000010347cfb1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
24 UIKit 0x0000000103492e3c -[UITableView layoutSubviews] + 213
25 UIKit 0x000000010341f973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
26 QuartzCore 0x000000010318ade8 -[CALayer layoutSublayers] + 150
27 QuartzCore 0x000000010317fa0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
28 QuartzCore 0x000000010317f87e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
29 QuartzCore 0x00000001030ed63e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
30 QuartzCore 0x00000001030ee74a _ZN2CA11Transaction6commitEv + 390
31 QuartzCore 0x00000001030eedb5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
32 CoreFoundation 0x00000001024f7dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
33 CoreFoundation 0x00000001024f7d20 __CFRunLoopDoObservers + 368
34 CoreFoundation 0x00000001024edb53 __CFRunLoopRun + 1123
35 CoreFoundation 0x00000001024ed486 CFRunLoopRunSpecific + 470
36 GraphicsServices 0x00000001072f09f0 GSEventRunModal + 161
37 UIKit 0x00000001033a6420 UIApplicationMain + 1282
38 LFDate 0x0000000101be33ae top_level_code + 78
39 LFDate 0x0000000101be33ea main + 42
40 libdyld.dylib 0x0000000105246145 start + 1
41 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException