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
Rohit Das
4,356 PointsCoreData and Swift - User Login
Hi there,
So I am creating an iOS application and want to allow users to log in and out/register. I am doing this using core Data and currently my program allows users to register, but the data isn't saved so when they try and log in, it says incorrect username/password, in other words, the program isn't recognizing the fact that the user already input their information when creating/registering their account and as a result cannot load the information they input and won't allow the user to log in. This is the code I have for when a user clicks the register button. Please help:
import UIKit
import CoreData
class RegisterPageViewController: UIViewController {
@IBOutlet weak var userEmailTextField: UITextField!
@IBOutlet weak var userPasswordTextField: UITextField!
@IBOutlet weak var confirmPasswordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func registerButtonTapped(sender: AnyObject) {
let userEmail = userEmailTextField.text
let userPassword = userPasswordTextField.text
let userConfirmPassword = confirmPasswordTextField.text
if (userEmail.isEmpty || userPassword.isEmpty || userConfirmPassword.isEmpty) {
displayMyAlertMessage("You haven't filled out all the fields.")
return;
}
if (userPassword != userConfirmPassword) {
displayMyAlertMessage("Passwords do not match.")
return;
}
var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context) as NSManagedObject
context.save(nil)
var successAlert = UIAlertController(title: "Alert", message: "Successfully registered.", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { action in
self.dismissViewControllerAnimated(true, completion: nil)
}
successAlert.addAction(okAction)
self.presentViewController(successAlert, animated: true, completion: nil)
}
What do you think is going wrong? Pasan Premaratne Amit Bijlani
I would really appreciate if I got some help for this! Thanks!
1 Answer
Stephen Whitfield
16,771 Points^They're probably saved in your .xcdatamodeld but not in your subclassed NSManagedObject files. You need to update those files with the newly added attributes.
Rohit Das
4,356 PointsRohit Das
4,356 PointsThank you for the response! I have not created an NSManagedObject file, what do you suggest I do?
Stephen Whitfield
16,771 PointsStephen Whitfield
16,771 PointsAh! You probably should as it's a great way to keep track of your models. Click on the Entity in the xcdatamodeld, click on Editor in the toolbar, click "Create NSManagedObject Subclass", click next, make sure you select the entity you're wanting to subclass, and you should be good to go.
Rohit Das
4,356 PointsRohit Das
4,356 PointsOk great! I've created my NSManagedObject file and this is what that file says as of right now:
import Foundation import CoreData
class Users: NSManagedObject {
}
The same error persists. Also, I was not using CoreData earlier and just recently decided to use CoreData for my app, so my login file looks like this:
@IBAction func loginButtonTapped(sender: AnyObject) {
Do you know of what I should change so that it accesses the new objects I'm saving using CoreData? I apologize, but it is rather difficult to find information on Swift as it is a relatively new language.
Thank you very much again.