I tried looking for what I need but I did so unsuccessfully.
I am reading in an XML string from a webservice and I need to store my information into core data. I have to have it sorted starting with Department which points to all the Subdepartments in the selected Department and then when a Subdepartment is selected it will list all of the items in that Subdepartment. The problem with this is I am receiving all my information in Data Tables so it has duplicates of departments and subdepartments. So to sort it I want to store the information to where I have an array of Dictionaries that hold the name of the departments in each and then an array of Dictionaries that hold the name of the subdepartments in each and then another array of Dictionaries that hold all the item info.
I need it to be like the following...
- Array of Department Dictionaries
- Department Dictionary
- String holding Department Name
- Array Of Subdepartment Dictionaries
- Subdepartment Dictionary
- String holding Subdepartment Name
- Array of Item Dictionaries
- Item Dictionary
- String holding Item Name
- String holding more Item Info
- Item Dictionary
- Subdepartment Dictionary
- Department Dictionary
(sorry its not pretty I can't figure out how to put it any other way...)
XML Code Example:
<QSR_VIEWS_INVENTORY_ITEMS_LIST diffgr:id="QSR_VIEWS_INVENTORY_ITEMS_LIST1" msdata:rowOrder="0"><CompanyID>104</CompanyID><QSRInventoryItemID>111</QSRInventoryItemID><Description>Test Item 111</Description><Department>_</Department><Subdepartment>_</Subdepartment><SequenceNumber>0</SequenceNumber><CountDisplayUnitName>CA</CountDisplayUnitName><CountDisplayUnitInCase>1.0000</CountDisplayUnitInCase><ReorderAt>0.0000</ReorderAt><ReorderTo>0.0000</ReorderTo><CaseUnitName>CA</CaseUnitName><CaseInCase>1.0000</CaseInCase><PackUnitName>_</PackUnitName><PacksInCase>0.0000</PacksInCase><StackUnitName>_</StackUnitName><StacksInCase>0.0000</StacksInCase><EachUnitName>_</EachUnitName><EachInCase>0.0000</EachInCase><InLocation1>N</InLocation1><InLocation2>N</InLocation2><InLocation3>N</InLocation3><InLocation4>N</InLocation4><InLocation5>N</InLocation5&开发者_JAVA百科gt;<InLocation6>N</InLocation6><InLocation7>N</InLocation7><InLocation8>N</InLocation8><InLocation9>N</InLocation9><InLocation10>N</InLocation10><InLocation11>N</InLocation11><InLocation12>N</InLocation12><InLocation13>N</InLocation13><InLocation14>N</InLocation14><InLocation15>N</InLocation15><OnShiftCountSheet>Y</OnShiftCountSheet><OnDayCountSheet>Y</OnDayCountSheet><OnWeekCountSheet>Y</OnWeekCountSheet><OnMonthCountSheet>Y</OnMonthCountSheet><OnWasteCountSheet>Y</OnWasteCountSheet><EquivalentToItemID>0</EquivalentToItemID><EquivalentCaseFactor>0.0000</EquivalentCaseFactor></QSR_VIEWS_INVENTORY_ITEMS_LIST>
I think all you need is one extra dictionary which has a key for all of the individual departments and sub departments, then you use that dictionary to check if you already have the new department or not, and if you don't, create it in the appropriate place. The memory overhead should be negligible, the extra dictionary will just have pointers to existing objects.
Answer provided by Alex Mayfield:
NOTE: There are probably way better ways to do this than what I have done.
BOOL addI = YES;
BOOL addS = YES;
BOOL addD = YES;
int I;
int S;
int D;
for(int i = 0; i < [DArray count]; i++)
{
DDictionary = [DArray objectAtIndex:i];
//NSLog([NSString stringWithFormat:@"%@ vs %@", soapResults12, [DDictionary objectForKey:@"Title"]]);
if([soapResults12 isEqualToString:[DDictionary objectForKey:@"Title"]])
{
D = i;
//NSLog(@"GOT IT");
SArray = [DDictionary objectForKey:@"Subdepartments"];
for(int k = 0; k < [SArray count]; k++)
{
SDictionary = [SArray objectAtIndex:k];
soapResults11 = [soapResults11 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//NSLog([NSString stringWithFormat:@"%@ vs %@", soapResults11, [SDictionary objectForKey:@"Title"]]);
if([soapResults11 isEqualToString:[SDictionary objectForKey:@"Title"]])
{
S = k;
//NSLog(@"GOT IT");
IArray = [SDictionary objectForKey:@"Items"];
for(int r = 0; r < [IArray count]; r++)
{
IDictionary = [IArray objectAtIndex:r];
//NSLog([NSString stringWithFormat:@"%@ vs %@", soapResults1, [IDictionary objectForKey:@"Title"]]);
if([soapResults1 isEqualToString: [IDictionary objectForKey:@"Title"]])
{
//NSLog(@"GOT IT");
I = r;
r = [IArray count];
addI = NO;
addS = NO;
addD = NO;
}
else
{
addI = YES;
addS = NO;
addD = NO;
}
}
k = [SArray count];
}
else
{
addS = YES;
addD = NO;
}
}
i = [DArray count];
}
else
{
addD = YES;
}
}
if(addI && !addS && !addD)
{
IDictionary = [[NSMutableDictionary alloc] init];
[IDictionary setObject:soapResults1 forKey:@"Title"];
[IArray addObject:IDictionary];
[SDictionary setObject:IArray forKey:@"Items"];
[SArray replaceObjectAtIndex:S withObject:SDictionary];
[DDictionary setObject:SArray forKey:@"Subdepartments"];
[DArray replaceObjectAtIndex:D withObject:DDictionary];
}
if(addS && !addD)
{
IDictionary = [[NSMutableDictionary alloc] init];
IArray = [[NSMutableArray alloc] init];
[IDictionary setObject:soapResults1 forKey:@"Title"];
[IArray addObject:IDictionary];
SDictionary = [[NSMutableDictionary alloc] init];
[SDictionary setObject:soapResults11 forKey:@"Title"];
[SDictionary setObject:IArray forKey:@"Items"];
[SArray addObject:SDictionary];
[DDictionary setObject:SArray forKey:@"Subdepartments"];
[DArray replaceObjectAtIndex:D withObject:DDictionary];
}
if(addD)
{
IDictionary = [[NSMutableDictionary alloc] init];
IArray = [[NSMutableArray alloc] init];
[IDictionary setObject:soapResults1 forKey:@"Title"];
[IArray addObject:IDictionary];
SDictionary = [[NSMutableDictionary alloc] init];
SArray = [[NSMutableArray alloc] init];
[SDictionary setObject:soapResults11 forKey:@"Title"];
[SDictionary setObject:IArray forKey:@"Items"];
[SArray addObject:SDictionary];
DDictionary = [[NSMutableDictionary alloc] init];
[DDictionary setObject:soapResults12 forKey:@"Title"];
[DDictionary setObject:SArray forKey:@"Subdepartments"];
[DArray addObject:DDictionary];
}
精彩评论