0

I'm trying to delete values of a lot of variables inside an array:

var fbUserID = String()
var fbUserName = String()
var meNickname = String()
var userIDOneSignal = String()

var deleteStrings = [fbUserID, fbUserName, meNickname, userIDOneSignal]

Is it possible to do something in line of this:

for i in deleteStrings {
    i.removeAll()//remove all as in remove the values of each variable
}

I've also tried using deleteStrings[i].removeAll()

1
  • You should use a dictionary Commented Apr 22, 2017 at 13:58

4 Answers 4

3

Due to value semantics you cannot mutate variables (as a pointer) from an array.

Rather than an array use a struct

struct User {

    var fbUserID = "12"
    var fbUserName = "Foo"
    var meNickname = "Baz"
    var userIDOneSignal = "123"

    mutating func clear()
    {
        fbUserID = ""
        fbUserName = ""
        meNickname = ""
        userIDOneSignal = ""
    }
}

var user = User()
print(user.fbUserID) // "12"
user.clear()

user.fbUserID = ""
print(user.fbUserID) // ""
Sign up to request clarification or add additional context in comments.

2 Comments

Why not simply declare all properties with let and instead of "clear" just instantiate a new struct
@LeoDabus Yes, that's better but I wanted to keep the basic "workflow" of the question.
0

Simply delete the array elements

deleteStrings.removeAll()

Comments

0

If your class inherits from NSObject, you can use key value coding to access the fields by name:

class User: NSObject {
    var fbUserID = String()
    var fbUserName = String()
    var meNickname = String()
    var userIDOneSignal = String()

    var deleteStrings = ["fbUserID", "fbUserName", "meNickname", "userIDOneSignal"]

    func clearAll() {
        deleteStrings.forEach { setValue("", forKey: $0) }
    }
}

let user = User()
user.fbUserID = "12345"
user.fbUserName = "Joseph Doe"
user.meNickname = "Joe"

print(user.meNickname)  // "Joe"

user.clearAll()

print(user.meNickname)  // ""

Comments

-1

If you want to delete/remove variable value then use optional otherwise your variable value only gets removed or become non-existence when it goes out of scope.

For Example:

var a: Int? = 5  // it have default value 5
a = nil // Now a have nil which is kind of telling that it contains nothing which is what you want to achieve

Now coming to your question.

Is it possible to do something in line of this:

for i in deleteStrings {
    i.removeAll()   
}

You are iterating over an array of strings and then for each string, you are trying to remove all the characters. First of all you will get error

 error: cannot use mutating member on immutable value: 'i' is a 'let' constant

Even though you will correct is using var it will not achieve what you are trying to do i.e. I want to delete the variables value because still your fbUserID ... all other variables will have copies of the data you initialised with.

Now how to do it?

  • You can use optional to achieve it.

    var fbUserID: String? = String()
    var fbUserName: String? = String()
    var meNickname: String? = String()
    var userIDOneSignal: String? = String()
    
    // To delete you will need to assign them nil
    fbUserId = nil
    

Again, you can't do them over loop because var are of values type and when you add it to the list their copies get added.

4 Comments

The OP wants non-optional empty strings. Optionals are a bad suggestion in this case.
@vadian I thought, he asked ways to empty the string. Optional is one way to represent it. BTW thanks for the reply.
An empty string and an optional nil string are two completely different things. Face the huge benefit of non-optional values: The code will never crash at runtime and the compiler prevents assigning an optional value at compile time.
I agree. My concern was 'Delete Variable' != 'Empty String'. I thought 'Delete Variable' in terms of 'NULL/NIL'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.