0

我有一个代码序列。其中有自定义对象:ItemItemList. 这些的相关成员是Item.codeItemList.ItemDictionaryItem.code只是一个NSString唯一标识Item. 这些Items被分成几类。有 4 个类别(名为“CatOne”等)。是一个字典,ItemDictionary类别名称为键,anNSMutableArray为对象;数组中填充了Item属于该类别的对象。

基本问题是,当我尝试访问 时Item.code,字符串显示为(null).

功能是我有一个更新的ItemsupdatedItems)数组,我想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在将其设置为 Item 之前分配和初始化它。
  • 使用

    oldItemArray = [ItemListFromFile.ItemDictionary objectForKey:currentCategoryName];
    

    代替

    [oldItemArray setArray:[ItemListFromFile.ItemDictionary objectForKey:currentCategoryName]];
    
  • 我尝试更新其中的所有项目,ItemList.ItemDictionary以便在归档时,所有项目都是新鲜的,可用于代码的下一次试运行。

4

0 回答 0