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

  • SEARCH
  • Home
  • 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 710615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:38:27+00:00 2026-05-14T04:38:27+00:00

First off I am very new to Objective C and iPhone programming. Now that

  • 0

First off I am very new to Objective C and iPhone programming. Now that that is out of the way. I have read through most of the Apple documentation on this and some third party manuals.

I guess I just want to know if I’m going about this the correct way …

- (NSMutableArray *)makeModel {

    NSString *api = @"http://www.mycoolnewssite.com/api/v1";

    NSArray *namesArray = [NSArray arrayWithObjects:@"News", @"Sports", @"Entertainment", @"Business", @"Features", nil];

    NSArray *urlsArray = [NSArray arrayWithObjects:
                      [NSString stringWithFormat:@"%@/news/news/25/stories.json", api],
                      [NSString stringWithFormat:@"%@/news/sports/25/stories.json", api],
                      [NSString stringWithFormat:@"%@/news/entertainment/25/stories.json", api],
                      [NSString stringWithFormat:@"%@/news/business/25/stories.json", api],
                      [NSString stringWithFormat:@"%@/news/features/25/stories.json", api], nil];

    NSMutableArray *result = [NSMutableArray array];

    for (int i = 0; i < [namesArray count]; i++) {
        NSMutableDictionary *objectDict = [NSMutableDictionary dictionary];
        NSString *name = (NSString *)[namesArray objectAtIndex:i];
        NSString *url = (NSString *)[urlsArray objectAtIndex:i];
        [objectDict setObject:name forKey:@"NAME"];
        [objectDict setObject:url forKey:@"URL"];
        [objectDict setObject:@"NO" forKey:@"HASSTORIES"];
        [result addObject:objectDict];
    }

    return result;
}

The output of the result is …

(
    {
    HASSTORIES = NO;
    NAME = News;
    URL = "http://www.mycoolnewssite.com/api/v1/news/news/25/stories.json";
},
    {
    HASSTORIES = NO;
    NAME = Sports;
    URL = "http://www.mycoolnewssite.com/api/v1/news/sports/25/stories.json";
},
    {
    HASSTORIES = NO;
    NAME = Entertainment;
    URL = "http://www.mycoolnewssite.com/api/v1/news/entertainment/25/stories.json";
},
    {
    HASSTORIES = NO;
    NAME = Business;
    URL = "http://www.mycoolnewssite.com/api/v1/news/business/25/stories.json";
},
    {
    HASSTORIES = NO;
    NAME = Features;
    URL = "http://www.mycoolnewssite.com/api/v1/news/features/25/stories.json";
}
)

Any insight would be appreciated 😉

  • 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-14T04:38:28+00:00Added an answer on May 14, 2026 at 4:38 am

    It looks fine. There can be some minor improvements if you care.

    1.

    [NSString stringWithFormat:@"%@/news/news/25/stories.json", api]
    

    can be replaced by

    [api stringByAppendingString:@"/news/news/25/stories.json"]
    

    if there’s no chance the api appears in the middle or accepts other arguments.

    2.

        NSString *name = (NSString *)[namesArray objectAtIndex:i];
        NSString *url = (NSString *)[urlsArray objectAtIndex:i];
    

    The explicit cast is unnecessary. An id can be implicitly casted to and from other ObjC objects.

    3.

    You could use a convenient method -dictionaryWithObjectsAndKeys: to construct the dictionary in one-shot, so you don’t need a temperary dictionary:

    [result addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
       name, @"NAME",
       url, @"URL",
       @"NO", @"HASSTORIES", nil]];
    

    4. (optional)

    This transform is not useful if the function is not a hot spot.

    Since the arrays are only used locally, it’s more efficient to use a C array.

    static const int arraySize = 5;
    NSString* namesCArray[] = {@"News", @"Sports", @"Entertainment", @"Business", @"Features"};
    NSString* urlsCArray[arraySize];
    urlsArray[0] = [api stringByAppendingString:@"/news/news/25/stories.json"];
    ...
    for (int i = 0; i < arraySize; ++ i) {
      ...
      NSString* name = namesCArray[i];
      NSString* url = urlsCArray[i];
      ...
    }
    

    this removes the repeated -count and -objectAtIndex: calls which is very slow compared with direct element access.

    5. (optional)

    This transform is not useful if the array is short.

    You could use fast-enumeration to loop over an ObjC container:

    int i = 0;
    for (NSString* name in namesArray) {
      NSString* url = [urlsArray objectAtIndex:i];
      ...
      ++ i;
    }
    

    6.

    Usually we use [NSNumber numberWithBool:NO] to represent a boxed true/false value, instead of a string @"NO". NSNumber is also used a lot whenever a primitive number (int, float, etc.) cannot be used (e.g. to be stored in an NSArray). I don’t know if your API explicitly requires a string NO, so it may not unsuitable for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.