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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:23:18+00:00 2026-05-27T12:23:18+00:00

-(id) initWithData:(NSString *)lastName: (NSString *)firstName{ self->firstname = firstName; self->lastname = lastName; return self; }

  • 0
-(id) initWithData:(NSString *)lastName: (NSString *)firstName{
self->firstname =  firstName;
self->lastname =  lastName;

return self;
}


-(void) printData {
NSLog(@"firstname: %@", firstname);
NSLog(@"lastname: %@", lastname);
}

so whenever I create a new object using the above init function. And Add objects to a NSMutableArray, using the addObject function.

NSMutableArray *objectArray = [[NSMutableArray alloc] init];

CustomObject *tempObject = [[CustomObject alloc] initWithData: @"smith": @"john"];
CustomObject *tempObjectb = [[CustomObject alloc] initWithData: @"brown": @"william"];
[objectArray addObject:tempObject];
[objectArray addObject:tempObjectb];

[[objectArray objectAtIndex:0] printData];
[[objectArray objectAtIndex:1] printData];

objects at index 1, and 0 always equal the whichever object was added to the array last.
This also happens if I use a for loop, or have more than 2 objects, all values when printed, turn to the values of the last added object to the objectArray. Let me know if there is any information that I am missing.
Is there something that I am missing?

  • 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-27T12:23:18+00:00Added an answer on May 27, 2026 at 12:23 pm

    Fix your initWithData:lastName: implementation as following:

    -(id) initWithData:(NSString *)lastName: (NSString *)firstName
    {
    
    self = [super init];
    if ( nil != self ) {
      self->firstname =  [firstName retain];
      self->lastname =  [lastName retain];
    }
    
    return self;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using: NSData *output1 = [NSData dataWithContentsOfFile:@~/centralUtilOut.tmp]; NSString *output = [[NSString alloc]initWithData:output1 encoding:NSUTF8StringEncoding]; NSLog(@%@,
I wanna have a NSString object as NSData . Later this NSData-Object should return
I am using following code to parse JSON- NSString *responseString = [[NSString alloc] initWithData:responseData
When I use this code: NSLog(@- (void)connectionDidFinishLoading:(NSURLConnection *)%@, connection); [connection release]; NSString *responseString =
-(void)messageSend:(NSString *)message; { NSLog(@messageSend); urlString = [[NSString alloc] initWithFormat:@http://someaddress/message/send?from=%@&msg=%@&latitude=0&longitude=0,appDelegate.userName,message]; NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]
My code: stringFromRecievedData = [[NSString alloc]initWithData:_data1 encoding:NSUTF8StringEncoding]; if (![stringFromRecievedData isEqualToString:lastStringFromRecievedData]) { [lastStringFromRecievedData setString: stringFromRecievedData];
initWithData does not convert my data object into a string properly. When I check
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error]; NSString *data = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
I have this code NSString *tr = [self sendUrl:@http://google.com/]; But for some reason '
I am new to Objective-C and try to convert a malformed UTF8 encoded NSString

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.