I have the following code in C# that returns a Dictionary<string, List<Discount>>.
static void Main(string[] args)
{
List<Discount> list = new List<Discount>();
list.Add(new Discount { Id = 1, Title = "Adam" });
list.Add(new Discount { Id = 2, Title = "Ben" });
list.Add(new Discount { Id = 3, Title = "Alex" });
list.Add(new Discount { Id = 4, Title = "Daniel" });
list.Add(new Discount { Id = 5, Title = "Ethan" });
list.Add(new Discount { Id = 6, Title = "Howard" });
list.Add(new Discount { Id = 7, Title = "Peter" });
list.Add(new Discount { Id = 8, Title = "Tazz" });
list.Add(new Discount { Id = 9, Title = "Steve" });
list.Add(new Discount { Id = 10, Title = "Lyle" });
Dictionary<string, List<Discount>> dic = new Dictionary<string, List<Discount>>();
foreach (Discount d in list)
{
string range = GetRange(d.Title);
if (dic.ContainsKey(range))
dic[range].Add(d);
else
dic.Add(range, new List<Discount> { d });
}
}
static string GetRange(string s)
{
char c = s.ToLower()[0];
if (c >= 'a' && c <= 'd')
return "A - D";
else if (c >= 'e' && c <= 'h')
return "E - H";
else if (c >= 'i' && c <= 'l')
return "I - L";
else if (c >= 'm' && c <= 'p')
return "M - P";
else if (c >= 'q' && c <= 't')
return "Q - T";
else if (c >= 'u' && c <= 'z')
return "U - Z";
return "";
}
Unfortunately, the SOAP wrapper I’m using isn’t returning the Dictionary properly, which means I now need to convert my C# code into Objective-C.
Instead of returning a Dictionary, I’ll simply return all of the data instead from my web service and create an NSDictionary.
Note, the following code assumes you’re running with ARC (although, converting it to NON-ARC is trivial).
This is a simple one-to-one conversion of what you have in C#.
There may be better ways of trying to do what you want… but this is what you asked for, so here ya go…