views:

238

answers:

4

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

+2  A: 

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
Daniel
A: 

I use a boolean cause I also have numerical fields that the user can set to 0 so I can't test if(number==0)

Mathieu
Post these as comments...Use NSNumber instead of int
Daniel
You can definitely put if (number==0) in number is an int. Please clarify the original question. For a search form, you should simply have to check to see if fields are filled, if they are valid, and then group them into a search query to run a search.
TahoeWolverine
some fields can not be filled in, so by default they will be initialized at 0 so I can't make the difference between a field that haven't been filled and a field that have been set to 0 by the user
Mathieu
A: 

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.

AlBlue
oh yeah that's a smart idea
Mathieu
A: 

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.

Corey Floyd
What are you calling a "model class"? Can't say I'm familiar with the term.
Dinah
I don't really see what you mean
Mathieu
Just a class to encapsulate your model data. It can hold data within a dictionary or other instance variables.
Corey Floyd
Cocoa encourages the MVC pattern. The idea is that you are hiding dependancies with your use of a singleton. Ideally you want declare object dependancies in the interface file.The form data you want to store should probably be stored using a subclass of NSObject. Then you have to decide how to use this data between different views. You could pass the object around (dependancy injection) or have the object save its data to a plist. Then whenever you instantiate a new model object you can load the form data from the plist. This should hopefully lead to easier to maintain code.
Corey Floyd