Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6690235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:36:48+00:00 2026-05-26T05:36:48+00:00

I have the following code in C# that returns a Dictionary<string, List<Discount>> . static

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:36:49+00:00Added an answer on May 26, 2026 at 5:36 am

    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…

    #import <Foundation/Foundation.h>
    
    #pragma mark -
    #pragma mark Discount Class Interface
    
    @interface Discount : NSObject
    
    @property (nonatomic, assign) int identity;
    @property (nonatomic, copy) NSString * title;
    
    - (id)initWithTitle:(NSString *)title andId:(int)identity;
    
    @end
    
    #pragma mark -
    #pragma mark Discount Class Implementation
    
    @implementation Discount
    
    @synthesize title = _title;
    @synthesize identity = _identity;
    
    - (id)initWithTitle:(NSString *)title andId:(int)identity
    {
        self = [super init];
        if (self)
        {
            self.title = title;
            self.identity = identity;
        }
        return self;
    }
    
    @end
    
    #pragma mark -
    #pragma mark Program Implementation
    
    static NSString * GetRange(NSString * s);
    
    int main (int argc, const char * argv[])
    {
    
        @autoreleasepool {
    
            NSMutableArray * discounts = [NSMutableArray array];
    
            [discounts addObject:[[Discount alloc] initWithTitle:@"Adam" andId:1]];
            [discounts addObject:[[Discount alloc] initWithTitle:@"Ben" andId:2]];
            [discounts addObject:[[Discount alloc] initWithTitle:@"Alex" andId:3]];
            [discounts addObject:[[Discount alloc] initWithTitle:@"Daniel" andId:4]];
            [discounts addObject:[[Discount alloc] initWithTitle:@"Ethan" andId:5]];
            [discounts addObject:[[Discount alloc] initWithTitle:@"Howard" andId:6]];
            [discounts addObject:[[Discount alloc] initWithTitle:@"Peter" andId:7]];
            [discounts addObject:[[Discount alloc] initWithTitle:@"Tazz" andId:8]];
            [discounts addObject:[[Discount alloc] initWithTitle:@"Steve" andId:9]];
            [discounts addObject:[[Discount alloc] initWithTitle:@"Lyle" andId:10]];
    
            NSMutableDictionary * result = [NSMutableDictionary dictionary];
    
            for (int i = 0; i < [discounts count]; i++) 
            {
                Discount * d = (Discount *)[discounts objectAtIndex:i];
                NSString * range = GetRange(d.title);
    
                NSMutableArray * list = [result valueForKey:range];
                if (list == nil)
                {
                    list = [NSMutableArray array];
                    [result setObject:list forKey:range];
                }
    
                [list addObject:d];
            }
    
            // Here "result" has what you want.
    
        }
    
        return 0;
    }
    
    static NSString * GetRange(NSString * s)
    {
        unichar c = [[s lowercaseString] characterAtIndex: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 @"";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that sets a cookie: string locale = ((DropDownList)this.LoginUser.FindControl(locale)).SelectedValue; HttpCookie
I have the following code that I need to add an additonal object to
I have the following snippet of code that's generating the Use new keyword if
I have the following code that shows either a bug or a misunderstanding on
I have the following code that won't compile and although there is a way
I have the following code that creates two objects (ProfileManager and EmployerManager) where the
I have the following JQuery code that worked perfect in C#/Asp.net 2.0 to call
I have the following html.erb code that I'm looking to move to Haml: <span
I have several blocks of the following code that each use there own matrix.
I have the following code in an Autofac Module that is used in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.