![](/img/trans.png)
[英]ios how to store time values in nsmutablearray and acessing objects from the array
[英]iOS store Array of Objects
在我的應用程序中,我實現了幾個tableviewcontrollers,它們為每個單元格顯示一個NSObject。 每個tableviewcontroller都有一個應用於數組的不同“過濾器”,因此每個表都顯示這些對象的特定組。 在每個tableviewcontroller的viewdidload中,我從NSUserdefaults加載數組,對其進行過濾並顯示。 這工作得很好,我正在使用NSCoder來獲取編碼/解碼的對象進行存儲。 僅出於開發目的,我以編程方式創建每個對象(initWithName:(NSString *)名稱等),將所有對象添加到數組中並進行存儲。 但是我確信這不是最終的解決方案。 問題:我正在尋找一種方法來實現這種對象數組而不用手工在代碼中創建每個對象。對象的數量是固定的,它們的內容不能更改。 他們只需要加載。 添加:也許在將來的更新中,我會將新項目添加到列表中。 我該怎么做?
所以我像@Luke Smith說的那樣實現了它,並且效果很好! 謝謝!
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
}
但是,當我做了一些腦力激盪時,我得出的結論是我需要編輯一些條目。 不幸的是,由於.plist位於MainBundle中,因此不可編輯...是否可以從頭開始將.plist存儲在documents目錄中(也許在xcode中)? 這是我的.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>
因此,當用戶按下特定按鈕時,我想更改此項目的.plist文件中的isFavorite
值。
查看從plist加載值。
您需要在項目中創建一個新的plist文件並向其中添加屬性(看起來有點像JSON編輯器-相當容易理解)
在代碼中,您可以使用以下代碼行將plist加載到內存中:
NSString *pathToMyPlist = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSDictionary *myPlistContents = [[NSDictionary alloc] initWithContentsOfFile:pathToMyPlist];
然后,您將獲得一個NSDictionary
其中包含您在plist中指定的鍵和值。 您可以遍歷這些鍵和值來初始化對象。
如果我正確理解了您的問題,那么在處理具有不同結構的數組時,我總是會這樣做。
static const int DIARIES_title = 0;
@interface NSArray (DIARIES_)
+ (id)arrayNEWDiary;
+ (id)arrayNEWDiaryWithTitle:(NSString *)stringTitleValue;
- (NSString *)objectDiary_title;
@end
@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
這將使您避免記住所有這些索引(例如objectAtIndex: 0
並且輕松,快速地向此數組結構添加新項:
首先添加並更新存儲索引值的int
static const int DIARIES_id = 0;
static const int DIARIES_title = 1;
static const int DIARIES_dateCreated = 2;
在使用- objectAtIndex:
或- replaceObjectAtIndex: withObject:
時,將使用這些變量。
然后,您必須為原型輸入任何新參數:
+ (id)arrayNEWDiaryWithId:(NSInteger)idValue title:(NSString *)stringTitleValue dateCreated:(NSDate *)dateCreatedValue;
您將不得不在使用該函數的代碼中進行更新,因為您將獲得一個未定義的函數 。 然后還將參數列表中的變量添加到實現文件中的return語句中:
+ (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];
}
最后,您必須添加函數以訪問數組中的每個值:
- (NSInteger)objectDiary_id;
- (NSString *)objectDiary_title;
- (NSDate *)objectDiary_dateCreated;
這將使您不必更新整個代碼。 這也將告訴您每個函數返回哪種數據類型將非常有用 。 更新索引號。 節省時間,因為它們都在一個地方完成。
我希望這就是您的問題所指的:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.