我有一系列代码。其中有自定义对象:Item
和ItemList
。这些成员的相关成员是Item.code
和ItemList.ItemDictionary
。 Item.code
只是一个唯一标识NSString
的{{1}}代码(如“AVK”)。这些Item
分为几类。共有4个类别(命名为“CatOne”等)。 Items
是一个字典,类别名称为键,ItemDictionary
为对象;数组中填充了属于该类别的NSMutableArray
个对象。
基本问题是,当我尝试访问Item
时,字符串显示为Item.code
。
功能是我有一个更新的(null)
(Items
)数组,我想用这些新项目更新updatedItems
。
以下是对象的所有属性,并在主文件中合成。
ItemList.ItemDictionary
代码:
@synthesize ItemListFromFile;
@synthesize upDatedItems;
@synthesize tempPassedManifest;
我尽量做到尽可能彻底,但不要犹豫要求澄清。
我一直在尝试很多解决方案,但似乎没有任何效果。我尝试过的事情:
-(id) upDatedItems:(NSArray *)newItems manifest:(Manifest *)manifest {
ItemListFromFile = [[ItemList alloc] init];
ItemListFromFile.ItemDictionary = [[NSMutableDictionary alloc] init];
ItemListFromFile = [NSKeyedUnarchiver unarchiveObjectWithFile:manifest.ItemListSavePath];
updatedItems = newItems
tempPassedManifest = manifest;
[self UpdateItemList];
return self;
}
-(void)UpdateItemList {
NSMutableArray *newItemArray = [[NSMutableArray alloc] init];
NSMutableArray *oldItemArray = [[NSMutableArray alloc] init];
// Go through each category. "i" is category Number
for (int i=1; i <= [Number of Categories]; i++)
{
NSString *currentCategoryName = [Get Category Name]; //This works...
// ********* Debug statements ************
// This Loop is where NSLog shows something's not right
// These conditions work fine.
for (int j = 0; j < [[ItemListFromFile.ItemDictionary objectForKey:currentCategoryName] count]; j++)
{
// This Log outputs (null) when it should output the code from the Item
NSLog(@"Code from File: %@", [[[ItemListFromFile.ItemDictionary objectForKey:currentCategoryName] objectAtIndex:j] code]);
}
// ************** Debug ******************
if ([[ItemListFromFile.ItemDictionary allKeys] containsObject:currentCategoryName])
{
[oldItemArray setArray:[ItemListFromFile.ItemDictionary objectForKey:currentCategoryName]];
for (Item *anItem in oldItemArray)
{
NSLog(@"OldItemArray Code: %@", anItem.code);
}
}
else
{
[oldItemArray removeAllObjects];
}
[newItemArray removeAllObjects];
// Go through each Item in category i.
for (NSString *currentCode in [array of codes in category i (this works)])
{
// There's two options of where to grab each Item from: either upDatedItems or oldItemArray
BOOL inOldArray = YES;
for (Item *anItem in upDatedItems)
{
if ([anItem.code isEqualToString:currentCode])
{
[newItemArray addObject:anItem];
inOldArray = NO;
NSLog(@"%@ was in updatedItems", currentCode);
break;
}
}
if (inOldArray)
{
// Checking in old Item Array
for (Item *anItem in oldItemArray)
{
NSLog(@"Checking %@ against %@", anItem.code, currentCode);
// (anItem.code is null)
if ([anItem.code isEqualToString:currentCode])
{
[newItemArray addObject:anItem];
NSLog(@"%@ was in oldItems", currentCode);
break;
}
}
}
}
//We've created the array, so add it to dictionary
[ItemListFromFile.ItemDictionary setObject:newItemArray forKey:currentCategoryName];
NSLog(@"New Dance Array: %@", newDanceArray);
}
[NSKeyedArchiver archiveRootObject:ItemListFromFile toFile:tempPassedManifest.ItemListSavePath];
}
的对象,而不是循环遍历数组内容的*anItem
循环,从for
循环,然后将对象设置为我在循环之前创建的项的索引。我认为这可能会有所帮助,以便我可以在将i=0..[Count]
设置为任何项目之前对其进行分配和初始化。使用
Item.code
而不是
oldItemArray = [ItemListFromFile.ItemDictionary objectForKey:currentCategoryName];
我尝试更新[oldItemArray setArray:[ItemListFromFile.ItemDictionary objectForKey:currentCategoryName]];
中的所有项目,以便在归档时,所有项目都是新的,以便下次试运行。