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 944183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:33:05+00:00 2026-05-15T22:33:05+00:00

NSArray *planetArray = [NSArray arrayWithObjects:@Earth, @Jupiter, @Saturn, @Neptune, @Pluto, nil]; NSMutableArray *objectArray = [[NSMutableArray

  • 0
NSArray *planetArray = [NSArray arrayWithObjects:@"Earth", 
                                                 @"Jupiter", 
                                                 @"Saturn", 
                                                 @"Neptune", 
                                                 @"Pluto", nil];
NSMutableArray *objectArray = [[NSMutableArray alloc] init];

for(NSString *eachPlanet in planetArray) {
    Planet *newPlanet = [[Planet alloc] init];
    [newPlanet setValue:eachPlanet forKey:@"name"];
    [newPlanet setValue:@"TEST" forKey:@"type"];
    [newPlanet setValue:[NSNumber numberWithInt:1234] forKey:@"mass"];
    [objectArray addObject:newPlanet];
    [newPlanet release];
}

for(Planet *displayEachPlanet in objectArray) {
    NSLog(@"DATA: %@", displayEachPlanet);
}

[objectArray release];

I am curious if this is the best way to create an object and set an iVar for each item in an array. Basically I am:

  1. Creating a Planet object
  2. Setting the iVar (from the NSString array)
  3. Adding the Planet object to an array.
  4. Releasing the Planet object

  5. Printing my Planet objects

  6. Releasing the array

NB: I am just testing, this is not for anything, I was just curious …

cheers Gary

  • 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-15T22:33:06+00:00Added an answer on May 15, 2026 at 10:33 pm

    Can’t see anything drastically wrong about doing it that way. One suggestion would be to have an extended initialiser for your planet class, along the lines of:

    -(Planet*) initWithName:(NSString*)name andType:(NSString*)type withMass:(int)mass;
    

    And then create the planet with:

    Planet *newPlanet = [[Planet alloc] initWithName:eachPlanet andType:@"Test" withMass:42];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NSArray arrayWithObjects needs nil at the end, NSString stringWithFormat and NSLog() doesn't. Why? [NSArray
-(NSArray*)getSomeStuff { NSMutableArray *array = [[NSMutableArray alloc]init]; // Add some objects to array return
NSMutableArray *Number=[NSArray arrayWithObjects: @1, @2, @3, @4, @5, @6, nil]; I have an array
NSString *arrayOfStrings = [NSArray arrayWithObjects:@http://, @https://, nil]; NSString *stringToSearchWithin = webpageURLLabel.text; BOOL found=NO; for
NSArray *itemArray = [NSArray arrayWithObjects:@one, @Two, @Three, nil]; segmentControl = [[UISegmentedControl alloc] initWithItems:itemArray]; segmentControl.frame
NSArray chemConstantArray = [[NSArray alloc] initWithObjects:0.0021400, 0.0012840, 0.0010700, nil]; Gives me four errors: Incompatible
NSArray * ComicArray = [TCSDataBase fetchManagedObjectsForEntity:@ComicDB withPredicate:nil]; [ComicArray retain]; arrayOfComics = [NSMutableArray arrayWithArray:ComicArray]; [[arrayOfComics
NSArray *arr = [NSArray arrayWithObjects:@m, @f, @m, @f, @f, nil]; How can I put
NSArray *test1 = [NSArray arrayWithObjects:@1,@2, nil]; NSArray *test2 = [NSArray arrayWithObjects:@1,@2, nil]; NSArray *test3
I have an NSArray with objects : NSArray *array = [NSArray arrayWithObjects:@Saturday,@Sunday,@Monday,@tuesday,@Wednesday,@thursday,nil]; and some

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.