Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
matt taylor
7,476 PointsUsing Core Data in Swift 2
Just building a small weather app. In the app i have the user setting the location they want in a text field. I'm trying to use core data to save and fetch this information(not sure if core data is the best way to go) Here is my code to save the data
@IBAction func SetLocation() {
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context: NSManagedObjectContext = appDel.managedObjectContext
let setLocation = NSEntityDescription.insertNewObjectForEntityForName("Location", inManagedObjectContext: context)
setLocation.setValue("New York, New York", forKey: "setLocation")
do {
try context.save()
print(setLocation)
} catch {
print("Could not save location")
}
performSegueWithIdentifier("setLocation", sender: nil)
}
I'm not getting any print statement at all when i click the setLocation button, So i'm not sure what is happening.
In my second view controller i'm trying to fetch the data with this function in viewdidload()
func fetchLocation() {
let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let context: NSManagedObjectContext = appDel.managedObjectContext
var request = NSFetchRequest(entityName: location)
do {
let results =
try context.executeFetchRequest(request)
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
But i'm stuck on where to go from here to load the result into my buttons label.
Thanks for any help.
2 Answers
Jeroen de Vrind
29,772 PointsResults is an array of NSManagedObjects. So with the following code you could iterate over the results and get the items and attributes out of it:
do {
let results =
try context.executeFetchRequest(request) as! [NSManagedObject]
for result in results {
print (result.valueForKey("setLocation"))
}
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
But overall i think CoreData is, for something that you want to achieve, a bit of an overkill. You could for example just store the location of the user on disk or in the NSUserDefault settings, if it's a string:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObjectForKey("UserLocation")
defaults.synchronize()
You can retrieve the information via:
defaults.objectForKey("UserLocation")
Jeroen de Vrind
29,772 PointsHave you created an entity in your .xcdatamodeld file called Location and added an attribute setLocation of type String? Then Editor > Create NSManagedObject subclass? In the assignment of the request variable of your fetchLocation method you should replace location with a string "Location". If you print your results you should see something in your debugger. Or try to open your sql file in sqlitebrowser for example (third party program).
matt taylor
7,476 PointsI looked up my sql in a browser and stuff is being saved... no errors so that is fixed. thanks, in the second part of my question, i'm setting results to the fetch request, what is the fetch returning and how can i get the results returned out of the function so that i can let say set a button label with the result. Thanks