Re-instantiate Class variable in Dart / Flutter - flutter

At the beginning of my app I have a global variable gameData which is declared and instantiated as:
GameData gameData = GameData();
Later I want to clear the gameData variable and re-instantiate/reset the variable with a clean instance of GameData. I do this by calling a function:
void ResetGameData() {
gameData = new GameData();
}
But that's not clearing the gameData variable. All the old values are remaining. Is there a better way of doing this?

It seems like your approach should work.
If you're building your UI based off of GameData, you will need to call setState() or notifyListeners() to rebuild everything.

The issue was I was instantiating a class within a class, and that syntax was incorrect so the sub-class was retaining its previous data. The rest of the variable was re-instantiated correctly.

Related

datatype of The shared defaults object returned by NSUserDefaults

while going through the class reference file for NSUserDefaults, I notice that the return value for [NSUserDefaults standardUserDefaults]is
The shared defaults object
what is this object, what is its type? which methods we can call on this object?
Thanks,
It's an instance of NSUserDefaults. You can call any of the instance methods that are defined for that class. (e.g. stringForKey:, dictionaryForKey:....)
+ (NSUserDefaults *)standardUserDefaults
It is just class method for returning singleton object of type NSUserDefaults. So, you can invoke all methods for this class.
From NSUserDefaults Class Reference
The defaults are initialized for the current user. Subsequent modifications to the standard search list remain in effect even when this method is invoked again—the search list is guaranteed to be standard only the first time this method is invoked. The shared instance is provided as a convenience—you can create custom instances using alloc along with initWithUser: or init.

simple method returns in objective c

I am currently new to objective c and have came across a problem while making a game I have a custom made object called battleEngine which is an instance variable in my helloWorld scene in cocos2d. That object has an object as an instance variable called plyController which is a PlayerController object. I want battleEngine to have a getter method that returns the plyController object and this code doesn't work:
-(PlayerController*)getPlayerController
{
return plyController;
}
Is there any reason you haven't just declared your player controller object as a property? You could just use the synthesised getter in that case to get the player controller.
Have a look at the documentation on properties.
Also, and I'm afraid I have to say this or they will take my Cocoa-programmer badge away from me, getPlayerController is not a good method name. Methods with get in them are conventionally used to return values in the parameters passed in by reference. The Cocoa Coding Guidelines tells us this, and much more.

Why do variables initialized in viewDidLoad not retain value in Objective-C?

I have a class I wrote called Location that just holds some strings. I'm using two instances of that class in a view controller, and when I initialize the two variables in viewDidLoad, they work fine for that method, but then when I try to use them later they are null. I have them set as retained properties. I have tested them and know that they are initialized for viewDidLoad (I use their fields in the view). Do I have to do something special in the Location class to make sure they don't get released? When I re-initialize them in a different method, everything runs smoothly.
Instance variables properly initialized in viewDidLoad should retain values normally just like they would in any other method. A coding error may cause the issue you are describing, e.g. if you have local variables in viewDidLoad hiding identically named instance variables.
you (or the event loop) is probably releasing the objects you are initializing after viewDidLoad is complete since they are autoreleased or something. To prevent that, make the variables in question properties on the class with the "retain" attribute and set them to nil on dealloc.

Best way to use my singleton

I started to develop my singleton class but I have a problem.
What I want to do is have a search objects containing the values of the search form that I could use in several views.
I want to have the ability to get the singleton in any view in order to perform the search or build the search form.
So I have a set of values with a boolean for each to know if the variable has been initialized by the user or not, cause not all the search fields needs to be filled in.
For example :
NSString name= Bob;
BOOL nameFilled =True;
NSString adress= nil;
BOOL adressFilled=false;
NSNumber numberOfChilds = 0;
BOOL numberOfChildsFilled = false;
So my problem is that I can't retain the boolean in my header file because it's not a class.
How can I do, is there a better solution than what I presented above?
Hope I have been clear
You dont need to have this BOOLean value to see if it is filled, why not just use the object itself to see if it has been initialized so something like
if(name==nil)
//this means i t hasnt been initialized
else
//this means it has
Instead of using int, use NSNumber. Then, for objects that haven't been specified, use 'nil', which is distinct from an NSNumber with 0 as a value.
You don't need to #retain BOOL or other primitive types in Objective-C - you only need use that for object types.
Seriously, don't implement a singleton. It isn't necessary for this application. You should have a model class to handle this.
Try using dependancy injection and/or plist files to save the information. You'll have a much better time debugging and extending functionality.

What's the easiest way to determine inside a method if it has been called the first time since app start?

I have a method where I do some startup animations. The method gets called many times during usage of the app, but on it's first call it needs to do some special things in addition.
Are Singletons the way to go? Maybe there is also an better way, instead of measuring how many times this method was called, and storing that in an ivar.
- (void)someMethod {
static BOOL hasBeenCalledBefore = NO;
if (!hasBeenCalledBefore) {
// perform setup
hasBeenCalledBefore = YES;
}
// do other stuff
}
Extra work may be required if you're using threads, but that's the basic idea.
Why isn't that initialization code in the constructor? Maybe you need to factor that method out into it's own class which uses the constructor to handle the init block you mention.
An amendment to chuck's answer (pretty much correct)
His works and answers your question, but another option you could use (assuming it didn't need access to any of the variables being passed to that method) would be to take the code out of your method and put it in a static initializer. It will only be executed when the class is first loaded and will isolate what is essentially completely different pieces of code.
If you want it called for every new class, use Chuck's answer but with a member variable, or use a class initializer.