0

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

6
  • 1
    Please post code,not screenshot of code Commented Oct 16, 2015 at 7:59
  • Your requirement is not clear, in which language/script you are trying to combine two static arrays? Commented Oct 16, 2015 at 8:01
  • Does this thread ask a similar question? Also, I would check here as well. Commented Oct 16, 2015 at 8:04
  • I agree with @IBajwa - what language is this? I also stumbled on this thread that seemed similar. Commented Oct 16, 2015 at 8:07
  • You cannot combine those arrays as they are in different scopes. Make them instance variables so any code in the class can access them. Commented Oct 18, 2015 at 9:30

3 Answers 3

1
NSArray *dailyPortfolioPrices=[[self dailyPortfolioPrices1] arrayByAddingObjectsFromArray:[self dailyPortfolioPrices2]];
Sign up to request clarification or add additional context in comments.

1 Comment

Please consider editing your answer to include an explanation of how your code works.
0

I think this would help you:

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

Comments

0

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.

3 Comments

Those arrays are local and cannot be accessed from outside the method.
The arrays are passed as return values from their respective functions. They are not being accessed from inside the method.
Yeah that's certainly true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.