Karan Bhatia
Karan Bhatia

Reputation: 897

How do I compare the value of password stored in CoreData with user input?

This is the code for login page where I want to compare input with stored data. import UIKit

import CoreData

class ViewController: UIViewController {

@IBOutlet weak var userName: UITextField!

@IBOutlet weak var passwordText: UITextField!

@IBAction func login(sender: AnyObject) {

var appdel : AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)

var context : NSManagedObjectContext = appdel.managedObjectContext!

var request1 = NSFetchRequest(entityName: "User")

NSEntityDescription newUser = [NSEntityDescription entityForName: @"User" inManagedObjectContext:context];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username == %@ AND password == %@",self.UsernameTextField.Text, self.PasswordTextField.Text];

var result : NSArray = context.executeFetchRequest(request1, error: nil)!

if (result.count > 0 ){

 println("true")

}

context.save(nil)

}

Upvotes: 1

Views: 889

Answers (2)

Lorenzo B
Lorenzo B

Reputation: 33428

Your predicate is correct. But you should set into your request.

Four considerations:

  • you don't need to save the context
  • you should not store credentials in Core Data. Maybe the Keychain will be suitable for this.
  • variable should be named with camelNotation. e.g usernameTextField and not UsernameTextField
  • why Objective-C and Swift code in the same file?

Upvotes: 1

Zell B.
Zell B.

Reputation: 10296

Your almost correct, and only one more step needed to make magic happen. Thats to set predicate to request like following

request1.predicate = predicate

Upvotes: 0

Related Questions