Swift Core data Error when trying to populate - ios

I'm new with swift and got a problem with core data.
My app crashes when trying to populate. I just have 1 field as for testing purposes
import UIKit
import SwiftyJSON
import CoreData
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
seedRestaurantList()
fetch()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func fetch() {
let moc = DataController().managedObjectContext
let nameFetch = NSFetchRequest(entityName: "RestaurantList")
do {
let fetchedName = try moc.executeFetchRequest(nameFetch) as! [RestaurantList]
print(fetchedName.first!.name!)
} catch {
fatalError("merda again: \(error)")
}
}
func seedRestaurantList () {
let moc = DataController().managedObjectContext
let entity = NSEntityDescription.insertNewObjectForEntityForName("RestaurantList", inManagedObjectContext: moc) as! RestaurantList
entity.setValue("teste", forKey: "name")
do {
try moc.save()
} catch {
fatalError("deu merda: \(error)")
}
}
}
This is the error that I get
2015-11-02 17:38:04.880 DinDins[2993:1252158] CoreData: error: Illegal
attempt to save to a file that was never opened. "This
NSPersistentStoreCoordinator has no persistent stores (unknown). It
cannot perform a save operation.". No last error recorded. 2015-11-02
17:38:04.885 DinDins[2993:1252158] * Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'This
NSPersistentStoreCoordinator has no persistent stores (unknown). It
cannot perform a save operation.'
* First throw call stack: (0x236c585b 0x3503adff 0x2346da89 0x2346dc95 0x2346de2d 0x2347710f 0x1249d03 0x12534fb 0x23468ef1
0x233b31f3 0x233b3223 0x233d615b 0x9cff0 0x9c360 0x9c400 0x277f9f55
0x278b6c4f 0x278b6b45 0x278b5ef1 0x278b5b27 0x278b577d 0x278b56f7
0x277f5cc3 0x270bdb05 0x270b9201 0x270b9091 0x270b85b1 0x270b8263
0x270b1a1f 0x23688091 0x23686387 0x235d90f9 0x235d8ecd 0x27867607
0x278622dd 0x9fde0 0x35788873) libc++abi.dylib: terminating with
uncaught exception of type NSException (lldb)
Thanks in advance
---RestaurantList+CoreDataProperties.swift
import Foundation
import CoreData
extension RestaurantList {
#NSManaged var name: String?
}
---RestaurantList.swift
import Foundation
import CoreData
class RestaurantList: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
}

Ricardo, try reseting your simulator. Open the Simulator, go to menu Simulator and select Reset content and Settings

Related

Save an Object into User Defaults [duplicate]

This question already has answers here:
Attempt to insert non-property list object when trying to save a custom object in Swift 3
(6 answers)
Save custom objects into NSUserDefaults [duplicate]
(2 answers)
Closed 3 years ago.
I am not able to save the list of viewControllers in my user defaults, its getting crashed at this userDefault.set(vcList, forKey: "vcList").
The error log says:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object
import UIKit
class ViewController: UIViewController {
struct viewControllerList {
var vc1 = ViewController()
var vc2 = LoginViewController()
}
override func viewDidLoad() {
super.viewDidLoad()
let vcList = viewControllerList()
let userDefault = UserDefaults.standard
userDefault.set(vcList, forKey: "vcList")
}
}
Please help!
The UserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs.
A default object must be a property list—that is, an instance of (or for collections, a combination of instances of) NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.
You can try this
//MARK:- Save viewController in UserDefault
func saveInDefaults(_ viewController : UIViewController) -> Void{
do {
let encodeData = try NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: false)
UserDefaults.standard.set(encodeData, forKey: "vcList")
UserDefaults.standard.synchronize()
} catch {
print("error")
}
}
And Call function from viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
let vcList = viewControllerList()
self.saveInDefaults(vcList)
}

Having trouble identifying the error when using the navigationController stack

Description
So I decided to fire up my app once more, after doing a clean and reinstall, and found that I was getting a crash that would send me to a "Thread 1: signal SIGABRT" at the top of the AppDelegate.swift file. The error occurred when I was simply navigating through views, via buttons that show(push) on to the stack. I even tried commenting out the functions on the view that I was trying to reach, in order to insure that it wasn't a function throwing an error for some reason. Below, I will attach some code snippets connected with the view I am trying to reach and the view that I am currently on. Sorry if this seems vague. It is just a very vague issue I am running into.
Code
class back4: UIViewController, UITextFieldDelegate{
#IBOutlet weak var no: UIButton!
#IBOutlet weak var yes: UIButton!
#IBOutlet weak var distance: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
/*
#IBAction func save(_ sender: AnyObject) {
let appDel:AppDelegate = (UIApplication.shared().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext
let entity1 = NSEntityDescription.insertNewObject(forEntityName: "CrawlerOne", into:context) as NSManagedObject as! CrawlerOne
entity1.crawlerDistance = distance.text
}
#IBAction func yes(_ sender: AnyObject) {
let appDel:AppDelegate = (UIApplication.shared().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext
let entity1 = NSEntityDescription.insertNewObject(forEntityName: "CrawlerOne", into:context) as NSManagedObject as! CrawlerOne
entity1.crawlerAbrasion = "yes"
}
#IBAction func no(_ sender: AnyObject) {
let appDel:AppDelegate = (UIApplication.shared().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext
let entity1 = NSEntityDescription.insertNewObject(forEntityName: "CrawlerOne", into:context) as NSManagedObject as! CrawlerOne
entity1.crawlerAbrasion = "no"
}
*/
#IBAction func back(_ sender: AnyObject) {
if let navController = self.navigationController {
navController.popViewController(animated: true)
}
}
}
^ This is the code for the view that I am trying to reach, via a show segue connected to a button*
class back3: UIViewController, UITextFieldDelegate{
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func back(_ sender: AnyObject) {
if let navController = self.navigationController {
navController.popViewController(animated: true)
}
}
}
^ This is the code attached to the view I am currently at
Recap
Again, I don't get any error messages besides the "Thread 1: signal SIGABRT" at the top of AppDelegate.swift. I've also already checked for accidental multi-segue'd buttons, because I know I've accidentally done that in the past. I am using swift 3 and xcode 8.0
Debugger Console
2016-08-04 10:05:32.239 Prototype Inspection[9292:4560823] CUICatalog: Invalid asset name supplied
2016-08-04 10:05:32.241 Prototype Inspection[9292:4560823] Could not load the "" image referenced from a nib in the bundle with identifier "Wirtgen.Prototype-Inspection"
2016-08-04 10:05:33.732 Prototype Inspection[9292:4560823] CUICatalog: Invalid asset name supplied:
2016-08-04 10:05:33.733 Prototype Inspection[9292:4560823] Could not load the "" image referenced from a nib in the bundle with identifier "Wirtgen.Prototype-Inspection"
2016-08-04 10:05:34.609 Prototype Inspection[9292:4560823] CUICatalog: Invalid asset name supplied:
2016-08-04 10:05:34.610 Prototype Inspection[9292:4560823] Could not load the "" image referenced from a nib in the bundle with identifier "Wirtgen.Prototype-Inspection"
2016-08-04 10:05:35.981 Prototype Inspection[9292:4560823] Unknown class back4 in Interface Builder file.
2016-08-04 10:05:36.010 Prototype Inspection[9292:4560823] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key distance.'
*** First throw call stack:
(0x180a42db0 0x1800a7f80 0x180a42a70 0x18134f6e4 0x185f15de8 0x186078eb0 0x180966888 0x186077898 0x185f19230 0x185cde118 0x185ba08ec 0x185bb90d0 0x185d53e5c 0x185c5fe40 0x185c5fb1c 0x185c5fa84 0x185b9c1e4 0x18352e994 0x1835295d0 0x183529490 0x183528ac0 0x183528820 0x185b9eff4 0x1809f909c 0x1809f8b30 0x1809f6830 0x180920c50 0x182208088 0x185c0a088 0x1000ad4c8 0x1804be8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
The view controller that you're navigation to has an outlet for distance hooked up in the storyboard, but a distance property doesn't (or no longer) exists on the destination view controller's class. It says that no back4 class could be found. Make sure that in the view controller's identity inspector, it has the correct module set below the class name. Sometimes Xcode fails to infer the correct module, and you have to set it manually.

New Facebook SDK issues: "terminating with uncaught exception of type NSException"

So with the updated Facebook SDK to enable a user to login to my app, I seem to not be able to get it to work properly! I am using code based off of Firebase's guide since that is what I am trying to integrate it with.
I am getting frustrated with it and cannot get the below error to stop terminating my app. I have all of the proper .plist info entered, I have the cocoa pods installed, and all of the SDK folders that are required. PLEASE HELP!
My code displays no errors, but when I build and run and then click on the Facebook button I get: "libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) "
Here is my code:
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func fbButtonPressed(sender: UIButton!) {
let facebookLogin = FBSDKLoginManager()
facebookLogin.logInWithReadPermissions(["email"], fromViewController: self) { (facebookResult: FBSDKLoginManagerLoginResult!, facebookError: NSError!) -> Void in
if facebookError != nil{
print("Facebook login failed. Error\(facebookError)")
} else {
let accessToken = FBSDKAccessToken.currentAccessToken().tokenString
print("Succesfully logged in with Facebook. \(accessToken)")
}
}
}
}

SIGABRT error in swift 2

I recently updated my app to Swift 2, and had one error, which I solved thanks to you guys.
And now, I have a second error, which is at runtime, when I press the one button to play a sound. It is a signal SIGABRT error.
Here is the error message I get in the debug console:
2016-01-25 09:16:09.019 WarningShot1.0.0[291:19030] -[WarningShot1_0_0.ViewController playMySound:]: unrecognized selector sent to instance 0x135547d30
2016-01-25 09:16:09.021 WarningShot1.0.0[291:19030] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WarningShot1_0_0.ViewController playMySound:]: unrecognized selector sent to instance 0x135547d30'
*** First throw call stack:
(0x182835900 0x181ea3f80 0x18283c61c 0x1828395b8 0x18273d68c 0x18755fe50 0x18755fdcc 0x187547a88 0x18755f6e4 0x18755f314 0x187557e30 0x1875284cc 0x187526794 0x1827ecefc 0x1827ec990 0x1827ea690 0x182719680 0x183c28088 0x187590d90 0x10005e2e0 0x1822ba8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
Also, this is the part of the code where it throws this error, in the second line, where the class is declared:
import UIKit
#UIApplicationMain
-> class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
What is happening here? What am I missing / misnaming? What do I need to change in my code to get ot running again. This app is ridiculously simple, and worked for months under the last version of Swift. Why is it now giving me errors?
Thank you for your help.
Here is the code for my ViewController.swift file:
import UIKit
import AVFoundation
import CoreMotion
class ViewController: UIViewController {
var myPlayer = AVAudioPlayer()
var mySound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("RemSound_01", ofType: "wav")!)
func initYourSound() {
do {
try myPlayer = AVAudioPlayer(contentsOfURL: mySound, fileTypeHint: nil)
myPlayer.prepareToPlay()
// myPlayer.volume = 1.0 // < for setting initial volume, still not perfected.
} catch {
// handle error
}
var motionManager = CMMotionManager()
var currentMaxAccelY : Double = 0.0
func viewDidLoad() {
super.viewDidLoad()
initYourSound()
// Do any additional setup after loading the view, typically from a nib.
//set motion manager properties
motionManager.accelerometerUpdateInterval = 0.17
//start recording data
// motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler: {
// (accelerometerData: CMAccelerometerData!,error:NSError!) -> Void in
// self.outputAccelerationData(accelerometerData.acceleration)
// if(error != nil) {
// print("\(error)")
// }
// })
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: {
(accelerometerData,error) in outputAccelerationData(accelerometerData!.acceleration)
if(error != nil) {
print("\(error)", terminator: "")
}
})
}
//func outputAccelerationData(acceleration : CMAcceleration){
// accY?.text = "\(acceleration.y).2fg"
//if fabs(acceleration.y) > fabs(currentMaxAccelY)
//{
// currentMaxAccelY = acceleration.y
//}
// maxAccY?.text = "\(currentMaxAccelY) .2f"
//}
func outputAccelerationData(acceleration : CMAcceleration){
if fabs(acceleration.y) >= 1.25 {
myPlayer.play()
}
}
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func playMySound(sender: AnyObject) {
myPlayer.play()
}
// self.resetMaxValues()
// #IBOutlet var accY: UILabel!
// #IBOutlet var maxAccY: UILabel!
// #IBAction func resetMaxValues() {
// currentMaxAccelY = 0
// }
}
}
"unrecognized selector sent to instance" means that there is a mismatch between the action-name in "addTarget()" and the name of the function you want to call. Probably something with the parameters of the function.. It's hard to say without seeing any code.
action: Selector("playMySound:")
would expect to find a function:
func playMySound(sender: AnyObject?) {};
To easier track what's happening, you might add a symbolic breakpoint for all exceptions. You do that in Xcode on the "exceptions" tab (left part of the window) and when an exception is thrown, Xcode stops like usual and you might look up the stack trace. If the call is synchronous, you should easily find and see your mistake.
EDIT: Oh well, you seem to have done the user interface using a XIB file. The mistake could be, you wired the button's action in the XIB file to the view controller. If you later change the method's signature (parameter, name, etc.), UIKit can't find the method. Open the XIB and fix your error. ;-)

[Error]: Type info TC,N,D not yet supported

class ABC{
#NSManaged var liked: Bool
.....
}
in other class
class xyz{
func afunc(){
let item = ABC()
if item.liked == true{ // getting crash on this line
}}}
[Error]: Type info TC,N,D not yet supported
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[...]: unrecognized selector sent to instance 0x15d78310'
* First throw call stack:
I am simply accessing a value that I have created in a class and
its working fine on one device(ios version 8.1 and 8.3) and throwing error on another device(ios version 8.4).
I think you have created swift class. Then it should be like below :
class ABC {
var liked = Bool()
}
class XYZ {
func execute() {
let item = ABC()
if item.liked == true { // getting crash on this line
}
}
}
class ClassA: NSObject {
var liked:Bool
override init() {
liked = true
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
execute();
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func execute(){
let item = ClassA();
if item.liked==true{
print(item.liked)
}
}
}

Resources