Reputation: 10206
I have a bunch of activities that are specific to age groups and genders (not trying to be agist or sexist here, just some groups are more likely to take part in these activities so I am trying to suggest them using tags)
I basically need an array that would store this information.
I want a multidimensional array that essentially looks like this:
["Playing Video Games", 10, 24, "male"],
["Putting on Makeup", 14, 50, "female"],
["Sleeping", 0, 100, "both"]
["Activity name string", age lower limit, age upper limit, "genders allowed"];
How can I create this in iOS?
I'll be storing it in the userDefaults.
Upvotes: 0
Views: 48
Reputation: 11
Robert is right and u should use CoreData. There is an nice [Core Data Tutorial][1] which explains the basics.
WHile using UserDefaults you can store your Data in an Array
[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"myArray"];
Upvotes: 0
Reputation: 1472
Objective-C
is an object-oriented programming language. So, you need to create a custom class called, maybe 'Person' and then add the required properties
.
For example, the custom class's .h
file you require will be something like :
@interface Person : NSObject
@property (nonatomic, assign) NSString *activity;
@property (nonatomic, assign) NSString *sex;
@property int ageLowerLimit;
@property int ageUpperLimit;
@end
Then you can manage such objects by first,
Importing this custom class in your code :
#import "Person.h"
And then creating a new Person
type object :
Person *firstPerson = [[Person alloc] init];
[firstPerson setActivity : @"Sleeping"];
[firstPerson setSex : @"Male"];
[firstPerson setAgeLowerLimit : 0];
[firstPerson setAgeUpperLimit : 100];
To store them you could use CoreData
or just the good old NSUserDefaults
.
Here's how to go about the NSUserDefaults
approach :
You first store the various Person
objects in an NSMutableArray
, then you synchronise the defaults :
NSMutableArray *people = [[NSMutableArray alloc] init];
[people addObject : firstPerson];
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
[defaults setObject:people forKey:@"PEOPLE"];
[defaults synchronise];
To use these objects later, do this :
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
NSMutableArray *savedPeople = [defaults objectForKey:@"PEOPLE"];
Person *person1 = [savedPeople objectAtIndex:0];
Upvotes: 3