Reputation: 15
I have created DB for my iOS mobile app using Realm, writing with Swift. I am trying to find way to look up for matching username and password in DB
This is what I have currently, Attempting filtering and get an object with matching username I am trying to address attribute/key called password from retrieved object
@IBAction func SignInCheck(sender: AnyObject) {
let realm = try! Realm()
var currentlogin = realm.objects(UserRecords).filter("name = LogUsernameTextField.text")
//this line causes SIGABRT to be thrown
if currentlogin.password == LogPasswordTextField.text { //This is the incorrectly addressed line
....
}
}
The issue maybe that I am not understanding how objects work in the right way, nor knowing the right syntax to address what I want.
I suspect it is in form of Result but I am still unable to find way to address desired information.
Here is the table structure for your information
class UserRecords: Object {
dynamic var username: String = ""
dynamic var password: String = ""
dynamic var latitude: Float = 0
dynamic var longtitude: Float = 0
}
I am open for suggestions better solution and ways of looking up/matching passwords in the table too, if any.
Thanks in advance
Upvotes: 1
Views: 306
Reputation: 27620
You are using a property called name
in your filter
string but in your UserRecords
class the property is called username
. Also you have to create the filter string differently:
var currentlogin = realm.objects(UserRecords).filter("username = '\(LogUsernameTextField.text!)'")
Also be aware that the filter method returns a list of UserRecord
objects that match the filter and not a single object. So calling if currentlogin.password == ...
will cause an error.
The list only has 1 item (because the username is unique) but it is still a list. So to access the UserRecord
object you can call first
:
var currentlogin = realm.objects(UserRecords).filter("name = LogUsernameTextField.text!").first
Also the text
property of UITextField
returns an Optional, so you have to unwrap it.
Upvotes: 1