-1

I am new to programming...

I know this probably is the best way to do an Offline login page but MYSQL users db (consists of usernames, passwords, id) is 'downloaded' from a php script to iOS device.That part works, and the users print to the console.

What I am struggling with is, checking the textfield where the user enters their username to the 'usersArray' to see if it exists at index[0]

Any help is appreciated.

NoobCoder
  • 1
  • 1
  • Possible duplicate of [Shorthand to test if an object exists in an array for Swift?](http://stackoverflow.com/questions/29679486/shorthand-to-test-if-an-object-exists-in-an-array-for-swift) – Asdrubal Aug 01 '16 at 15:44

1 Answers1

1

I'm not entirely sure what you are trying to do here but I think you are trying to take the value of a text field and see if that value appears in your usersArray. Provided this is the case try:

let exists = usersArray.contains(usernameTextField.text)

This will give you a Bool that indicates if the username exists in your array.

Side Note:

Hopefully you are only doing some general testing at the moment but in case you aren't… Please don't send passwords in plain text and absolutely don't download all usernames and passwords even in an encrypted format to a device! Aside from potentially taking up a large amount of space on the user's device you would be making it very easy for unscrupulous people to crack every user password in your database!

theMikeSwan
  • 4,739
  • 2
  • 31
  • 44
  • yes, I am trying to take the value of the textfield and see if the value appears in my users array. the above code throws an error cannot call value of non-function type 'NSMutableArray – NoobCoder Aug 01 '16 at 15:51
  • That is only part of the error message though it looks like You are using an NSMutableArray rather than an Array so change `contains` to `containsObject` and you should get the result you want. – theMikeSwan Aug 01 '16 at 16:09
  • continuing on your comment. `let exists = usersArray.containsObject(usernameTextField.text!)` I added in `print(exists)` and it prints false to console, even though the username exists. It seems my whole approach for logging into the device is incorrect. – NoobCoder Aug 01 '16 at 18:39
  • Is the `usersArray` an array of usernames or an array of dictionaries with usernames and passwords in it? In the second case you need to make an array that holds _only_ the usernames as Strings so the text from the field can be compared apples to apples rather than apples to oranges. The other option is to loop through the `usersArray` comparing the value of the username to the text field value, this is the expensive route… – theMikeSwan Aug 01 '16 at 19:13
  • the 'usersArray' has both usernames and passwords in it. I think I'm going to have to make it look through the array (which isn't a problem, should have at most 15 users when app is ready, and users won't increase) – NoobCoder Aug 01 '16 at 19:41