iOS store Array of Objects

孤街浪徒 提交于 2019-12-25 04:46:33

问题


In my application i implemented several tableviewcontrollers that display a NSObject for each cell. Every tableviewcontroller has a different "filter" that is applied to the array so every table displays a specific group of these objects. In the viewdidload of each tableviewcontroller i load my array from NSUserdefaults, filter it, and display it. This works just fine, i am using NSCoder to get the Objects encoded/decoded for storing. For developing purposes only, i create each Object programmatically (initWithName:(NSString*)name etc.), add all Objects to the array and store it. But i am sure this is not the final solution. Question: I am searching for a way to implement this array of objects without creating each object by hand in code.The number of objects is fixed und their content can not change. They just need to be loaded. Add: maybe in future updates i will add new items to the list. How can i do it?

So i implemented it like @Luke Smith said and it works perfectly! Thank you!

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSArray *allElements = [[NSArray alloc] initWithContentsOfFile:plistPath];

for (NSDictionary *newObject in allElements){
    Object *tempObject = [[Object alloc] init];
    tempObject.Type = [newObject objectForKey:@"Type"];
    tempObject.isFavorite = [newObject objectForKey:@"isFavorite"];
    [Elements addObject:tempObject];//MutableArray storing all Elements
}

But as i did a little bit of brain-storming i came to the conclusion that i would need to edit some entries. Unfortunately since the .plist is in the MainBundle it is not editable... Is it possible to store the .plist in the documents directory from the beginning ( maybe in xcode )? This is my .plist :

<plist version="1.0">
<array>
<dict>
    <key>isFavorite</key>
    <false/>
    <key>Name</key>
    <string>Obj1</string>
</dict><dict>
    <key>isFavorite</key>
    <true/>
    <key>Name</key>
    <string>Obj2</string>
</dict>
</array>
</plist>

So when the user pushes a specific button, i would want to change the isFavorite value in the .plist file for this Item.


回答1:


Look at loading your values from a plist.

You need to create a new plist file in your project and add your properties to it (looks a bit like a JSON editor - is fairly easy to understand)

In code, you can then load your plist into memory using a line of code like this :

NSString *pathToMyPlist = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];

NSDictionary *myPlistContents = [[NSDictionary alloc] initWithContentsOfFile:pathToMyPlist];

You then have an NSDictionary containing the keys and values you specified in your plist. You can loop through these keys and values to initialize your objects.




回答2:


If I am understanding your question correctly, this is something I ALWAYS do when handling arrays with different structures.

Using categories implementation on NSArray or MSMutableArray

Header file

static const int DIARIES_title = 0;

@interface NSArray (DIARIES_)

+ (id)arrayNEWDiary;
+ (id)arrayNEWDiaryWithTitle:(NSString *)stringTitleValue;
- (NSString *)objectDiary_title;

@end

Implantation file

@implementation NSArray (DIARY_)

+ (id)arrayNEWDiary {
   return [NSArray arrayNEWDiaryWithTitle: @"Untitled"];

}

+ (id)arrayNEWDiaryWithTitle:(NSString *)stringTitleValue {
   return [NSArray arrayWithObjects: stringTitleValue, nil];

}

- (NSString *)objectDiary_title {
   return [self objectAtIndex: DIARIES_title];

}

@end

This will keep you from trying to memorize what all those indexes like objectAtIndex: 0 Also adding a new item to this array structure is easy and quick:

Adding New Items

First add and update the int storing the index value

static const int DIARIES_id = 0;
static const int DIARIES_title = 1;
static const int DIARIES_dateCreated = 2;

You would use these variables when using either - objectAtIndex: or - replaceObjectAtIndex: withObject:

Updating the arrayNEW Prototype and Function Definition

Then you would have to enter any new arguments to the prototype:

+ (id)arrayNEWDiaryWithId:(NSInteger)idValue title:(NSString *)stringTitleValue dateCreated:(NSDate *)dateCreatedValue;

You would have to update in your code where this function was used because you will get a undefinded function something like that. And then also add the variables from the parameter list to the return statement in the implementation file:

+ (id)arrayNEWDiary {
   return [NSArray arrayNEWDiaryWithId: 0 title: @"Untitled" dateCreated: [NSDate date]];

}

+ (id)arrayNEWDiaryWithId:(NSInteger)idValue title:(NSString *)stringTitleValue dateCreated:(NSDate *)dateCreatedValue {
   return [NSArray arrayWithObjects: idValue, stringTitleValue, dateCreated, nil];

}

Accessing the Objects

And lastly you have to add the functions to access each value in the array:

- (NSInteger)objectDiary_id;
- (NSString *)objectDiary_title;
- (NSDate *)objectDiary_dateCreated;

This will keep you from having to update all over your code. Also this will tell you what data type each function will return super helpful. Updating index numbers. Saves time because it is all done in one place.

I hope this was what your question was referring to :)



来源:https://stackoverflow.com/questions/31345123/ios-store-array-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!