user12717187
user12717187

Reputation:

Cannot delete elements from array

I'm having in my textfield three email id's. These three emails are also stored in an array called feedbackEmailsArray. These email id's are also separated from each other by a comma.

But in case I don't want an email, then when I click on the backspace button and completely delete an email id, then I also want it to be deleted from my feedbackEmailsArray.

How can I achieve this..?

EDIT 1: This is how I'm deleting the email id:

    if let char = string.cString(using: String.Encoding.utf8) {
        let isBackSpace = strcmp(char, "\\b")
        if (isBackSpace == -92) {

           //Backspace was pressed
        }
    }

Upvotes: 2

Views: 119

Answers (1)

Zyfe3r
Zyfe3r

Reputation: 653

Try this.

First you split the string in your textfield into parts based on ' , '. Then we clear the feedbackEmailsArray and then add the emails back into the feedbackEmailsArray

validateEmail will check if the current string is a valid email or not, if its valid email, it will be added to the array. validateEmail could be made a global function or a static function if you want to use it anywhere in the program.

func validateEmail(email:String) -> Bool {

    let emailFormat = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
    let emailPredicate = NSPredicate(format:"SELF MATCHES %@", emailFormat)
    return emailPredicate.evaluate(with: email)

}

var emailIds = yourtextfield.text
var emailIdsArr = emailIds.split(separator: ",")
feedbackEmailsArray.removeAll()
for i in(0..<emailIdsArr.count) where emailIdsArr.indices.contains(i)
{

    if validateEmail(email:emailIdsArr[i]) == true
    { feedbackEmailsArray.append(emailIdsArr[i]) }

}

Explanation : @testname The validateEmail function checks if the stuff you typed are in a valid format or not, that is, '[email protected]'. Now, everytime you backspace in that email field, emailIds variable will get that entire string ([email protected],[email protected],asd@a). In this string, asd@a is not a valid email. Now emailIDs.split will split the string in emailIds variable based on the number on , character. These are stored in the emailIdsArr as emailIdArr[0],emailIdArr[1],emailIdArr[2]...

The for loop gets the total number of indexs in the emailIdArr by using emailIdArr.count. The where emailIdsArr.indices.contains(i) is a simple checking to check if the index is valid. Just to make it crash proof(was added by @iOSDev into my answer).

Now validateEmail is called and we give the argument as a string at index of emailIdsArr[i] (eg. i = 5 then index = 5 , so validateEmail(email:[email protected]) will return true as it's in an emailId format)

validateEmail is a boolean function that returns true if the string is in emailID format and return false if it isn't.

So if validateEmail returns true, that email gets appended to feedbackEmailsArray.

Since we are using append and the number of emailIds can be changed anytime, we have to clear the entire array every time this entire function gets called, hence the feedbackEmailsArray.removeAll() placed outside and above the for-loop.

Upvotes: 1

Related Questions