Hello I am currently following a Swift tutorial which uses the old version of Swift and I am not able to follow this as I believe the syntax has changed in the newer version of Swift.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var context:NSManagedObjectContext = appDel.managedObjectContext
var newUser = NSEntityDescription.insertNewObjectForEntityForName("users", inManagedObjectContext: context) as NSManagedObject
newUser.setValue("Joe", forKey: "username")
newUser.setValue("pass", forKey: "password")
do {
try context.save()
} catch let error {
print("Couldn't save user data")
print(error)
}
let request = NSFetchRequest(entityName: "Users")
request.returnsObjectsAsFaults = false
// var results = context.executeFetchRequest(request)
do {
var results =
try context.executeFetchRequest(request)
results = results as! [NSManagedObject]
} catch let error as NSError {
print(error)
}
for result: AnyObject in results {
print(results)
}
}
The error I am receiving is to do with the results on the line for result: AnyObject in results and the error is unresolved identifier 'results' which is giving me the impression that this should be declared somewhere as a variable as it is currently unresolved but I cannot figure out how to fix this, any help would be greatly appreciated, thanks!
do {
var results = try context.executeFetchRequest(request)
results = results as! [NSManagedObject]
} catch let error as NSError {
print(error)
}
for result: AnyObject in results {
print(results)
}
results only has scope inside the do block there. You need to move the processing of the array inside the do:
do {
var results = try context.executeFetchRequest(request)
results = results as! [NSManagedObject]
for result in results {
print(results)
}
} catch let error as NSError {
print(error)
}
Also there's no need to lose information by casting as AnyObject.
With the new API's against CoreData (in Swift 3.0) you should do:
let request = NSFetchRequest<Users>(entityName: "Users")
request.returnsObjectsAsFaults = false
do {
var results =
try context.fetch(request)
// `results` will here be [Users]
} catch let error as NSError {
print(error)
}
The new signature of fetch is fetch<T : NSFetchRequestResult>(_ request: NSFetchRequest<T>) throws -> [T] where T is the same as provided in <HERE> (ex. NSFetchRequest<Users>)
Related
I want my swift code to print out the strings attributes. Right now when calling the function I am getting a runtime error at context. I just want to print out all of each string entry. I have added the function in question below.
Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
let appDelegate = UIApplication.shared.delegate as! AppDelegate //Singlton instance
var context:NSManagedObjectContext!
#objc func pressRight(){
let fetchRequest = NSFetchRequest<Place>(entityName: "Name")
do {
let result = try context.fetch(fetchRequest)
let nameArray = result.map{$0.name}
print(nameArray)
} catch {
print("Could not fetch \(error) ")
}
}
pic
select manual in code gen
then create custom class of place add to your project
You are using the wrong entity name "Name" instead of "Place"
import Foundation
import CoreData
class CoreDataManager {
static let shared = CoreDataManager()
private init() {}
lazy var coreDataStack = CoreDataStack(modelName: "Place")
func allNames() -> [String]? {
let request: NSFetchRequest<Place> = Place.fetchRequest()
do {
// Peform Fetch Request
let places = try coreDataStack.managedContext.fetch(request)
return places.map({$0.name})
} catch {
print("Unable to Fetch Workouts, (\(error))")
}
return nil
}
func allPlaces() -> [Place]? {
let request: NSFetchRequest<Place> = Place.fetchRequest()
do {
// Peform Fetch Request
let places = try coreDataStack.managedContext.fetch(request)
return places
} catch {
print("Unable to Fetch Workouts, (\(error))")
}
return nil
}
}
if you still getting error then before this initialize your context
managedObjectContext/context you force unwrapping it
add this stack class
import Foundation
import CoreData
class CoreDataStack {
private let modelName: String
lazy var managedContext: NSManagedObjectContext = {
return self.storeContainer.viewContext
}()
init(modelName: String) {
self.modelName = modelName
}
private lazy var storeContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: self.modelName)
container.loadPersistentStores { storeDescription, error in
if let error = error as NSError? {
print("Unresolved error \(error), \(error.userInfo)")
}
}
return container
}()
func saveContext() {
guard managedContext.hasChanges else {return}
do{
try managedContext.save()
} catch let error as NSError {
print("Unresolved error \(error), \(error.userInfo)")
}
}
func updateContext() {
do {
try managedContext.save()
} catch let error as NSError {
print("Unresolved error \(error), \(error.userInfo)")
}
}
func clearChange() {
managedContext.rollback()
}
}
then how to use it
in your view controller viewDidLoad() function or any other button tap action you can get your place names like this
override func viewDidLoad() {
super.viewDidLoad()
// here you get all names
let names = CoreDataManager.shared.allNames()
print(names)
let places = CoreDataManager.shared.allPlaces()
print(places)
let namesAgain = places.map({$0.name})
print(namesAgain)
}
So I have this code which works fine, but I want a much better one.
func deleteCoreDataObjects() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
//where groupData is an Array of an Entity
for i in 0..<self.groupData.count {
context.delete(groupData[i])
}
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
Currently I'm deleting the objects one by one via for loop.
You can try this:
func deleteAllData(entity: String)
{
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: entity)
fetchRequest.returnsObjectsAsFaults = false
do
{
let results = try managedContext.executeFetchRequest(fetchRequest)
for managedObject in results
{
let managedObjectData:NSManagedObject = managedObject as! NSManagedObject
managedContext.deleteObject(managedObjectData)
}
} catch let error as NSError {
print("Detele all data in \(entity) error : \(error) \(error.userInfo)")
}
}
Usage:
self.deleteAllData("your_entityName")
Already seen in: https://stackoverflow.com/a/33931528/2894160
Best is delete the persistence storage and then add new one instead of looping each entity (if you want to delete all entities from coredata).
func deletePersistentStoreCoordinator () {
do {
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("YourDatabaseName.sqlite")
try self.persistentStoreCoordinator.destroyPersistentStoreAtURL(url, withType: NSSQLiteStoreType, options: nil)
try self.persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
}
catch{
}
}
Here is the code for deleting records from Core Data :
//Delete user info from local db
func deleteUserInfo() {
let context = appdelegate.managedObjectContext
let coord = appdelegate.persistentStoreCoordinator
let fetchRequest = NSFetchRequest(entityName: "User")
if #available(iOS 9.0, *) {
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
//let predicate = NSPredicate(format: "id == %#", key)
//fetchRequest.predicate = predicate
do {
try coord.executeRequest(deleteRequest, withContext: context)
}
catch let error as NSError {
//Error handling
}
catch {}
} else {
// Fallback on earlier versions
do {
let users: NSArray = try appdelegate.managedObjectContext.executeFetchRequest(fetchRequest)
for user in users {
appdelegate.managedObjectContext.delete(user)
}
try appdelegate.managedObjectContext.save()
} catch let error as NSError {
//Error handling
}
catch {}
}
}
I'm trying to load data and save it into the database but I get a warning and an error in func preloadData():
Cast from 'NSManagedObject' to unrelated type 'LeadItem' always fails
And in func removData():
cannot convert value of type 'LeadItem' to expected argument type 'NSManagedObject'
Similar solutions for an older version of Xcode aren't working for me.
func preloadData ()
{
if let contentsOfURL = NSBundle.mainBundle().URLForResource("leads_Data", withExtension: "csv")
{
removeData()
var error:NSError?
if let leads = parseCSV(contentsOfURL, encoding: NSUTF8StringEncoding, error: &error)
{
for lead in leads
{
let leadItem = NSEntityDescription.insertNewObjectForEntityForName("LeadItem", inManagedObjectContext: self.managedObjectContext) as! LeadItem
}
}
}
}
func removeData ()
{
let fetchRequest = NSFetchRequest(entityName: "LeadItem")
do
{
let leadItems = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [LeadItem]
for leadItem in leadItems
{
self.managedObjectContext.deleteObject(leadItem)
}
}
catch let error as NSError
{
print("Failed to retrieve record: \(error.localizedDescription) \n")
}
}
}
I'm trying to convert my Swift 1 code into Swift 2.1.1 code.
So I am trying to add a fetchRequest.
In Swift 1 I did this:
if let results = context.executeFetchRequest(fetchRequest, error:&error),
let managedObject = results.first as? NSManagedObject {
context.deleteObject(managedObject)
}
let saveError: NSError?
context.save(nil)
and
var error: NSError?
let fetchedResults = managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]?
if let results = fetchedResults {
people = results
}
else {
print("Could not fetch \(error), \(error!.userInfo)")
}
Swift 2.1 (2nd request -> doesn't work):
do {
let fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
// success if it gets here
if let results = fetchedResults {
people = results
}
} catch let error as NSError {
// failed so print error
print("Error: \(error.localizedDescription)")
}
Errors in line (if let results... & let fetchedResults...):
Initializer for conditional binding must have Optional type, not '[AnyObject]'
Call can throw, but is not marked with 'try' and the error is not handled
Errors in line (let fetchedResults...):
Call can throw, but is not marked with 'try' and the error is not handled
Cannot downcast from '[AnyObject]' to a more optional type '[NSManagedObject]?'
Could you please help me to translate this into Swift 2.1.1?
Thanks for your help!
You can wrap it in a do catch block. It will print an error if the let results line fails.
do {
let results = try context.executeFetchRequest(fetchRequest)
// success if it gets here
if let managedObject = results.first as? NSManagedObject {
context.deleteObject(managedObject)
}
} catch let error as NSError {
// failed so print error
print("Error: \(error.localizedDescription)")
}
EDIT
Second request:
do {
let fetchedResults = try managedContext.executeFetchRequest(fetchRequest)
// success if it gets here
if let results = fetchedResults as? [NSManagedObject]{
people = results
}
} catch let error as NSError {
// failed so print error
print("Error: \(error.localizedDescription)")
}
I got some issue with the code that I can't figure out. After I installed Xcode 7 beta and convert my swift code to Swift 2
Code:
override func viewDidAppear(animated: Bool) {
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = AppDel.managedObjectContext
let request = NSFetchRequest(entityName: "PlayerList")
list = Context.executeFetchRequest(request)
tableView.reloadData()
}
ScreenShot:
As of Swift 2, Cocoa methods that produce errors are translated to Swift functions that throw an error.
Instead of an optional return value and an error parameter as in Swift 1.x:
var error : NSError?
if let result = context.executeFetchRequest(request, error: &error) {
// success ...
list = result
} else {
// failure
println("Fetch failed: \(error!.localizedDescription)")
}
in Swift 2 the method now returns a non-optional and throws an error
in the error case, which must be handled with try-catch:
do {
list = try context.executeFetchRequest(request)
// success ...
} catch let error as NSError {
// failure
print("Fetch failed: \(error.localizedDescription)")
}
For more information, see "Error Handling" in "Adopting Cocoa Design Patterns"
in the "Using Swift with Cocoa and Objective-C" documentation.
You could try this code:
let result = (try! self.manageContext.executeFetchRequest(FetchRequest)) as! [NSManageObjectClass]
Swift 3.0
In this example PlayerList is NSManagedObject entity/class name (auto created by Xcode)
let request: NSFetchRequest<PlayerList> = PlayerList.fetchRequest()
var result:[PlayerList]?
do{
//Succes
result = try context.fetch(request)
}catch let error as NSError {
//Error
print("Error \(error)")
}
print("result: \(result)")
var results = [YourEntity]?
results = try! self.managedObjectContext!.executeFetchRequest(fetchRequest) as! [YourEntity]
Try the code below
override func viewWillAppear(animated: Bool) {
let appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context = appDel.managedObjectContext
let request = NSFetchRequest(entityName:"Users")
do {
let results = try context.executeFetchRequest(request)
itemList = results as! [NSManagedObject]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}