2 Replies - 6223 Views - Last Post: 01 December 2011 - 01:58 AM

#1 schultz2146  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 22-December 08

Object-C NSArray / NSMutableArray overwriting last addObject

Posted 27 November 2011 - 03:03 AM

All, I'm probably to close to the problem to see what I'm doing wrong. Essentially it looks like Iím overwriting all the indexes with the last entry. Being new to Object-C and having an instructor who tells you to read a chapter but never provides lecture to explain the chapter, simple problems tend to become big ones when you don't know the basics. Any guidance is appreciated. /r Richard

Example:

What I should have:
2011-11-27 18:55:12.816 SongDemo[5439:707] #1: Artist: Derek and the Dominos Title: Layla
2011-11-27 18:55:12.819 SongDemo[5439:707] #2: Artist: Simon and Garfunkel Title: Bridge Over Troubled Water
2011-11-27 18:55:12.820 SongDemo[5439:707] #3: Artist: The Beatles Title: Let It Be
2011-11-27 18:55:12.821 SongDemo[5439:707] #4: Artist: Elton John Title: Your Song
2011-11-27 18:55:12.822 SongDemo[5439:707] #5: Artist: James Brown Title: Get Up (I Feel Like Being A) Sex Machine
2011-11-27 18:55:12.823 SongDemo[5439:707] #6: Artist: The Kinks Title: Lola
2011-11-27 18:55:12.823 SongDemo[5439:707] #7: Artist: Creedence Clearwater Revival Title: Who'll Stop the Rain
2011-11-27 18:55:12.824 SongDemo[5439:707] #8: Artist: James Taylor Title: Fire and Rain
2011-11-27 18:55:12.825 SongDemo[5439:707] #9: Artist: Black Sabbath Title: Paranoid
2011-11-27 18:55:12.826 SongDemo[5439:707] #10: Artist: Free Title: All Right Now

What I get:
2011-11-27 18:56:07.307 SongDemo[5455:707] #1: Artist: Free Title: All Right Now
2011-11-27 18:56:07.313 SongDemo[5455:707] #2: Artist: Free Title: All Right Now
2011-11-27 18:56:07.315 SongDemo[5455:707] #3: Artist: Free Title: All Right Now
2011-11-27 18:56:07.316 SongDemo[5455:707] #4: Artist: Free Title: All Right Now
2011-11-27 18:56:07.317 SongDemo[5455:707] #5: Artist: Free Title: All Right Now
2011-11-27 18:56:07.317 SongDemo[5455:707] #6: Artist: Free Title: All Right Now
2011-11-27 18:56:07.318 SongDemo[5455:707] #7: Artist: Free Title: All Right Now
2011-11-27 18:56:07.319 SongDemo[5455:707] #8: Artist: Free Title: All Right Now
2011-11-27 18:56:07.321 SongDemo[5455:707] #9: Artist: Free Title: All Right Now
2011-11-27 18:56:07.323 SongDemo[5455:707] #10: Artist: Free Title: All Right Now


int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    Song *mySong = [[Song alloc] init];
    
    NSMutableArray *songArray = [[NSMutableArray alloc]init];
 
    NSArray *songArtist = [NSArray arrayWithObjects:@"Derek and the Dominos", @"Simon and Garfunkel",@"The Beatles",@"Elton John",@"James Brown",@"The Kinks",@"Creedence Clearwater Revival",@"James Taylor",@"Black Sabbath",@"Free", nil];
    
    NSArray *songName = [NSArray arrayWithObjects:@"Layla",@"Bridge Over Troubled Water",@"Let It Be",@"Your Song",@"Get Up (I Feel Like Being A) Sex Machine",@"Lola",@"Who'll Stop the Rain",@"Fire and Rain",@"Paranoid",@"All Right Now", nil];
    
    for (int x=0; x<10; x++) {
        mySong.name = [songArtist objectAtIndex:x];
        mySong.title = [songName objectAtIndex:x];
        [songArray addObject:mySong];
            //NSLog(@"#%i: Artist: %@  Title: %@",x+1, [[songArray objectAtIndex:x]name], [[songArray objectAtIndex:x]title]);
    }
    
    for (int x=0; x<10; x++) {
        NSLog(@"#%i: Artist: %@  Title: %@",x+1, [[songArray objectAtIndex:x]name], [[songArray objectAtIndex:x]title]);
    }
    
    [pool drain];
    return 0;
}





#import <Foundation/Foundation.h>

@interface Song : NSObject
{
    NSString *name;
    NSString *title;
}

@property(nonatomic, retain) NSString *name, *title;

@end



#import "Song.h"

@implementation Song

@synthesize name, title;

@end



Is This A Good Question/Topic? 0
  • +

Replies To: Object-C NSArray / NSMutableArray overwriting last addObject

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,082
  • Joined: 08-August 08

Re: Object-C NSArray / NSMutableArray overwriting last addObject

Posted 29 November 2011 - 08:52 PM

mySong is a pointer, and you're making every item in songArray use that pointer so at the end they all point to the same memory location and that contains the last object added. You need to reallocate the pointer:
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    
    NSMutableArray *songArray = [[NSMutableArray alloc]init];
    
    NSArray *songArtist = [NSArray arrayWithObjects:@"Derek and the Dominos", @"Simon and Garfunkel",@"The Beatles",@"Elton John",@"James Brown",@"The Kinks",@"Creedence Clearwater Revival",@"James Taylor",@"Black Sabbath",@"Free", nil];
    
    NSArray *songName = [NSArray arrayWithObjects:@"Layla",@"Bridge Over Troubled Water",@"Let It Be",@"Your Song",@"Get Up (I Feel Like Being A) Sex Machine",@"Lola",@"Who'll Stop the Rain",@"Fire and Rain",@"Paranoid",@"All Right Now", nil];
    
    for (int x=0; x<10; x++) {
        Song *mySong = [[Song alloc] init];
     
        mySong.name = [songArtist objectAtIndex:x];
        mySong.title = [songName objectAtIndex:x];
        [songArray addObject:mySong];
        NSLog(@"#%i: A____Artist: %@  Title: %@",x+1, [[songArray objectAtIndex:x]name], [[songArray objectAtIndex:x]title]);
    }
    NSLog(@"\n");
    for (int x=0; x<10; x++) {
        NSLog(@"#%i: Artist: %@  Title: %@",x+1, [[songArray objectAtIndex:x]name], [[songArray objectAtIndex:x]title]);
    }
    
    [pool drain];
    return 0;
}


Was This Post Helpful? 1
  • +
  • -

#3 schultz2146  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 22-December 08

Re: Object-C NSArray / NSMutableArray overwriting last addObject

Posted 01 December 2011 - 01:58 AM

Now that you pointed out my error it makes perfect since. Thank you for the assist.


Richard

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    NSMutableArray *songArray = [[NSMutableArray alloc]init];
 
    NSArray *songArtist = [NSArray arrayWithObjects:@"Derek and the Dominos", @"Simon and Garfunkel",@"The Beatles",@"Elton John",@"James Brown",@"The Kinks",@"Creedence Clearwater Revival",@"James Taylor",@"Black Sabbath",@"Free", nil];
    
    NSArray *songName = [NSArray arrayWithObjects:@"Layla",@"Bridge Over Troubled Water",@"Let It Be",@"Your Song",@"Get Up (I Feel Like Being A) Sex Machine",@"Lola",@"Who'll Stop the Rain",@"Fire and Rain",@"Paranoid",@"All Right Now", nil];
    
    for (int i=0; i<10; i++) {
        Song *mySong = [[Song alloc] init];
        mySong.name = [songArtist objectAtIndex:i];
        mySong.title = [songName objectAtIndex:i];
        [songArray addObject:mySong];
    }
    
    NSLog(@"         The Top 10 Hits of 1970. ");
    NSLog(@"-----------------------------------------------");
    for (int i=0; i<10; i++) {
        NSLog(@"#%i: %@ - %@",i+1, [[songArray objectAtIndex:i]name], [[songArray objectAtIndex:i]title]);
    }
    
    [pool drain];
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1