# Reseting Class Instances

**URL:** <https://forums.kodeco.com/t/reseting-class-instances/27545>\
**Category:** Swift\
**Created:** [March 26, 2017, 6:45am UTC](https://forums.kodeco.com/t/reseting-class-instances/27545 "2017-03-26T06:45:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elearner](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/elearner/32/109248_2.png) [@elearner](https://forums.kodeco.com/u/elearner)\
**Post date:** [March 26, 2017, 6:45am UTC](https://forums.kodeco.com/t/reseting-class-instances/27545/1 "2017-03-26T06:45:18Z")

</div>

I have an app that I’m working on and it has one class, with instance variables and functions, that I developed. My class instance variables are arrays and are initialized with literal values.

I want to be able to “reset” all my instances and or class instance without having to end the app and restart it. What’s the best practice to do this?

The only thing that I’ve come up with so far is setting up an instance of  
my class as an optional. I’m not sure if that’s good practice and I’m also  
not sure if I’ve coded everything correctly.

Here’s an example(this is not my actual code!): myApp  
//class file  
Class myNewClass {  
var aString = [“A”,“B”,C"]  
var aDigit = [1,2,3]  
}

//viewcontroller code

var myClassInstance: myNewClass? = myNewClass

//UIButton that resets my class instance  
@IBAction func resetClass(\_ sender: UIButton)  
{  
myClassInstance = nil  
var myClassInstance: myNewClass? = myNewClass  
}

So, will this code work and or should I even try to make a class instance  
into an optional?

If this code won’t work then what is a better way to “reset” my class and or it’s instance variables without ending the app?

Thanks for any and all help!

Gary

---

<div class="post-metadata">

**Author:** ![ofiron](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ofiron/32/102075_2.png) [@ofiron](https://forums.kodeco.com/u/ofiron)\
**Post date:** [March 26, 2017, 2:05pm UTC](https://forums.kodeco.com/t/reseting-class-instances/27545/2 "2017-03-26T14:05:42Z")

</div>

Hi Elearner,

Setting the variable **myClassInstance** as optional, is depend with the use you use the optional in your code.  
To make it sort if the only reason you used it for the reset function, I will do it in a different approach:

- remove the optional from the class definition
- replace the code in resetClass action to: _myClassInstance = myNewClass()_  
If you need to use the optional in your code, you can keep it and just replace the code in reset function.

I think it the simple solution to your problem by the code. But if you interesting we can start discussion on other approaches.

---

<div class="post-metadata">

**Author:** ![elearner](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/elearner/32/109248_2.png) [@elearner](https://forums.kodeco.com/u/elearner)\
**Post date:** [March 26, 2017, 4:49pm UTC](https://forums.kodeco.com/t/reseting-class-instances/27545/3 "2017-03-26T16:49:57Z")

</div>

Hi ofiron,

Thanks for your quick response.  
I’ll give your suggestion a try.

Thanks,  
Gary

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [March 29, 2017, 1:05pm UTC](https://forums.kodeco.com/t/reseting-class-instances/27545/4 "2017-03-29T13:05:14Z")

</div>

@ofiron suggestion should be followed if you don’t want to handle the unwrapping of your variable each time you need it, especially if it never needs to be nil.  
Also, do you want to reset a single instance or a variable number of instance of this object ?

---

<div class="post-metadata">

**Author:** ![ckeithray](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@ckeithray](https://forums.kodeco.com/u/ckeithray)\
**Post date:** [April 3, 2017, 6:48pm UTC](https://forums.kodeco.com/t/reseting-class-instances/27545/5 "2017-04-03T18:48:27Z")

</div>

The technique is good, but if you can use ‘struct’ instead of ‘class’ then you can safely put the reset function into the struct itself.

```
struct MyNewThing {
    var aString = ["A","B",C"]
    var aDigit = [1,2,3] 
    func reset() {
        self = MyNewThing()
    }
}

```

It’s not safe to do that inside a class.

–  
C. Keith Ray  
Senior Software Engineer / Trainer / Agile Coach

- [http://www.thirdfoundationsw.com/keith\_ray\_resume\_2014\_long.pdf](http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf)

---

<div class="post-metadata">

**Author:** ![elearner](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/elearner/32/109248_2.png) [@elearner](https://forums.kodeco.com/u/elearner)\
**Post date:** [April 4, 2017, 1:58am UTC](https://forums.kodeco.com/t/reseting-class-instances/27545/6 "2017-04-04T01:58:29Z")

</div>

Thanks for the suggestion, Keith!  
I’m beginning to believe, that in most cases, it’s better to start with  
a struct vs a class.

Gary
