0

How can I add an array value to another array?

I get the array using:

NSMutableArray *pointsArray = [[result componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] retain];

I want to add the first and the last value of pointsArray to another array.

2
  • do you want to append values in existent array? Commented Jul 5, 2011 at 9:21
  • Yes I want append old array values in new array Commented Jul 5, 2011 at 9:41

5 Answers 5

2
[array addObject:[pointsArray objectAtIndex:0]]; //First Object
[array addObject:[pointsArray lastObject]]; //Last Object

But this array should be an NSMutableArray.

Sign up to request clarification or add additional context in comments.

Comments

1

Get the value of array using objectAtIndex: method of NSArray.

Comments

0

as follows

NSUInteger totObjects = [pointsArray count];
[yourOtherArray addObject:[pointsArray objectAtIndex:0]]; //First Object
[yourOtherArray addObject:[pointsArray objectAtIndex:totObjects-1]]; //Last Object

6 Comments

is that the yourOtherArray start index from the 0 to 2
if i add the array into the other array than how can i get the new array 1st and last array value .Which has old array value
@Shima you mean to say yourOtherArray already has few objects in that ? and you want to get first and last value of that also ?
pointsArray = [[result componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] retain]; NSInteger totobjects = [pointsArray count]; [annArray addObject:[pointsArray objectAtIndex:0]]; [annArray addObject:[pointsArray lastObject]]; NSLog(@"This fro thr %@",[annArray objectAtIndex:0]);
@Shima have you initialized annArray ?
|
0

Its simple just add like this

NSArray *otherArray = [[NSArray alloc] initWithObjects: [pointsArray objectAtIndex:0], [pointsArray objectAtIndex:[pointsArray count]-1],nil];

2 Comments

pointsArray = [[result componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] retain]; NSInteger totobjects = [pointsArray count]; [annArray addObject:[pointsArray objectAtIndex:0]]; [annArray addObject:[pointsArray lastObject]]; NSLog(@"This fro thr %@",[annArray objectAtIndex:0]);
did you alloc init the annArray
0

This can be achieved by using this:

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

//add one object to the array
[recentPhotos addObject: selectPhotos];

Comments

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.