HurkNburkS
HurkNburkS

Reputation: 5510

creating an object out of multiple variable types

I have an NSDictionary that I am passing to a NSObject Class where I pull all of the values out of the dictionary and pass them into their correct types.

For instance NSInteger, BOOL, NSString, char are the types of values I am pulling out of the NSDictionary and putting into their only variables.

My question what is the best way to turn these values into one big object that can then be putt into an array?

I have heard that I can use the class itself as an Object.. But I am not really sure how to do this.

or could I just put them back into a NSDictionary?... but if thats the case do NSDictionaries allow for multiple value types?

Upvotes: 0

Views: 89

Answers (3)

HelmiB
HelmiB

Reputation: 12333

Actually, you are in the right path. this is basically MVC architecture way. so you are questioning about M = Model.

Model in here example is class that defines all variables. cut to the point, here's you should do:

-> create a class that contain your variable, with @property & @synthesize name : ClassA.

then you could set object ClassA into dictionary.

ClassA *myClass = [[ClassA alloc] init];
myClass.myString = @"test String";
myClass.myBoolean = True;

[dictionary setObject:myClass forKey:@"myObject"];
[myClass release]; //we no longer need the object because already retain in dictionary.

then retrieve it by :

ClassA *myClass = (ClassA*)[dictionary objectForKey:@"myObject"];
NSLog(@"this is value of myString : %@ & boolean : %i",myClass.myString,myClass.myBoolean);

Upvotes: 3

Sanoj Kashyap
Sanoj Kashyap

Reputation: 5060

Yeah, You can create a class itself with these as different values as a properties.Once you pass this object to any class you can access those values there by obj.propertyName.Doing by this Lead to create Modal in MVC pattern. Please let me know if there is doubt.

 Test *object = [[Test alloc] init];
object.testBool=true;
object.testString=@"Test";
NSDictionary *passData = [[NSDictionary alloc] initWithObjectsAndKeys:object,@"testObject", nil];
Test *getObject = (Test*)[passData objectForKey:@"testObject"];
NSLog(@"%d",getObject.testBool);
NSLog(@"%@",getObject.testString);

You can customized the init method of Test Class.

Upvotes: 0

Kent
Kent

Reputation: 1689

You can put the collection of values in NSDictionary, NSArray, NSMutableArray, etc. Any of the collection types. Or if you have a fixed number of values/types, you can create a class that has a data member for each value/type and put it in that. The class solution would eliminate having to do a lot of casting, but it also only works if there are a fixed number of values/types.

Here is the AppleDoc on collections.

Upvotes: 0

Related Questions