0

I want to add more data at 0th index of my array. There is already an array stored at 0th index of my array. Here is the line of code where data is stored at 0th index of my array.

    if ([[albumDetailsResponse valueForKey:@"result"] isEqualToString:@"IsValidUser"]) {
        [albumURLArray addObject:[albumDetailsResponse valueForKey:@"data"]];
    }

Here, albumURLArray is a NSMutableArray and at its 0th index, the object will be stored. Now, I want to add more URL at 0th index of the same array. So, How can I achieve this?

Thanks in advance.

How to add object at first index of NSArray In this question's answer, the data is added at new index in the NSMutableArray while what I want is I want to add new data at 0th index of that array. So, my requirement is different then this question.

8
  • the method is insertObject:atIndex: Commented Mar 31, 2015 at 10:39
  • I think your albumURLArray is actually a dictionary. The function you are using to store the data is not supported on arrays and is a dictionary function. Commented Mar 31, 2015 at 10:39
  • I have already tried insetObject:atIndex: method but it didn't work. Commented Mar 31, 2015 at 10:44
  • possible duplicate of How to add object at first index of NSArray Commented Mar 31, 2015 at 10:54
  • @BurhanuddinSunelwala I have edited my question once again to explain my problem in detail. Commented Mar 31, 2015 at 12:05

8 Answers 8

2
if([albumURLArray count]>0)
    {
       [albumURLArray removeAllObjects];
       [albumURLArray addObject:[albumDetailsResponse  valueForKey:@"data"]];

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

Comments

1

You can create one array and use that array as the 0th Index of Your albumURLArray

For e.g

NSMutableArray *arrtemp=[[NSMutableArray alloc]init];
[arrtemp addObject:[albumDetailsResponse valueForKey:@"data"]];
[arrtemp addObject:[albumDetailsResponse valueForKey:@"URL"]];


[albumURLArray addObject:arrtemp];

To get the URL,

Use [[albumURLArray objectAtIndex:0] objectAtIndex:1]]

Comments

0

Just use:

[albumURLArray insertObject:[albumDetailsResponse valueForKey:@"data"] atIndex:0]

Comments

0
[albumURLArray insertObject:[albumDetailsResponse valueForKey:@"data"] atIndex:0];

Comments

0

You can use-

[albumURLArray insertObject:[albumDetailsResponse valueForKey:@"data"] atIndex:0];

Comments

0

Instead adding array to 0th Index. Add a NSMutableDictionary. And add one key for response data and an NSArray for URL's.

NSMutableDictionary * dataDictionary = [NSMutableDictionary alloc] init];
dataDictionary setValue:[albumDetailsResponse valueForKey:@"data"] forKey:@"data"];

NSMutableArray * urlArray = @[[NSURl urlWithString:@"http://someurl.com"], [NSURl urlWithString:@"http://someurl1.com"]];

dataDictionary setValue:urlArray forKey:@"URLS"];

Comments

0

have already tried insetObject:atIndex: method but it didn't work.

The only three reasons it wouldn't work:

  • NSMutableArray isn't initialized. (i.e. albumURLArray = [@[] mutableCopy];or albumURLArray = [[NSMutableArray alloc] init];)
  • The index isn't correct (i.e. no item added yet but index 1 instead 0 used.)
  • The array is a plain NSArray

2 Comments

1. If I'll initialize array then I will lost all the data at 0th index, which I do not want to do. 2. I want to add data after the last index and the data is contained at the 0th index of my array. 3. I have already mentioned that my array is not a simple one but it is a mutable array.
if it wasn't initialised before, there is no data.
0

To add an object to the front of the array, use 0 as the index:

[albumURLArray insertObject:myObject atIndex:0];

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.