Jestin Saji Chacko
Jestin Saji Chacko

Reputation: 23

how to combining two static array

I want to combine two Static Arrays into another Static Array. My two static arrays are dailyPortfolioPrices1 & dailyPortfolio2.

- (NSArray *)dailyPortfolioPrices1  //my first array
{
    static NSArray *prices = nil;
    if (!prices)
    {
        prices = [NSArray arrayWithObjects:
                  [NSDecimalNumber numberWithFloat:582.13],
                  [NSDecimalNumber numberWithFloat:604.43],
                  [NSDecimalNumber numberWithFloat:32.01],
                  nil];
    }
    return prices;

- (NSArray *)dailyPortfolioPrices2 //my second array
{
    static NSArray *prices2 = nil;
    if (!prices2)
    {
        prices2 = [NSArray arrayWithObjects:
                  [NSDecimalNumber numberWithFloat:476.13],
                  [NSDecimalNumber numberWithFloat:534.43],
                  [NSDecimalNumber numberWithFloat:32.01],
                  nil];
    }
    return prices2;
}

Can anyone tell me how to combine these two Arrays into an another array named dailyPortfolioPrices. And I need to display dailyPortfolioPrices using an index.

Thanks for help in advance

Upvotes: 0

Views: 73

Answers (3)

Pramod Tapaniya
Pramod Tapaniya

Reputation: 1266

NSArray *dailyPortfolioPrices=[[self dailyPortfolioPrices1] arrayByAddingObjectsFromArray:[self dailyPortfolioPrices2]];

Upvotes: 1

Daniel Zhang
Daniel Zhang

Reputation: 5858

The arrays can be concatenated into a new array using

NSArray *dailyPortfolioPrices = [self.dailyPortfolioPrices1 arrayByAddingObjectsFromArray:self.dailyPortfolioPrices2];

The values in the array can be retrieved along with their index using

for (NSInteger i = 0; i < dailyPortfolioPrices.count; i++) {
    NSLog(@"index %ld, item %@", (long)i, [newArray objectAtIndex:i]);
}

where I have added an NSLog to output the values to the console.

Upvotes: 0

AP7
AP7

Reputation: 134

I think this would help you:

NSArray *dailyPortfolioPrices=[[self dailyPortfolioPrices1] arrayByAddingObjectsFromArray:[self dailyPortfolioPrices2]];

Upvotes: 0

Related Questions