I want to add the static contents into the Mutable Array. I have parsed the data using XML parsing and stored the dynamic data into the mutable array. Now i want to add the static string into that Mutable Array. So how can i add that?
For Eg:
In my dynamic Array of content is,
{
first,
third,
fourth,
fifth.
}
I want to add the static String into the 1st index of array. Because my requirement is, displayed the static content into the 1st position.
Expected output:
{
first,
second, //Add the static data into the First index of Array
third,
fourth,
fifth.
}
So how can i do this?
Thanks.
If I understand what you mean. You can use:
[NSMutableArray insertObject:atIndex:]
Supposing myArray is the instance of NSMutableArray:
[myArray insertObject:#"second" atIndex:1];
instead of NSString you can insert every object you want.
After creating NSMutableArray from XML parsing you can implement like this,
suppoose In XMLArray you having your xml data now you want to insert static date at any location,
NSMutableArray *tempArray=[NSMutable array];
for(int i=0;i<[XMLArray count];i++)
{
if(i==1)//accrding to you
[tempArray addObject:yourStaticData];//yourStaticData is object having your data.
else
[tempArray addObject:[XMLArray objectAtIndex:i]];
}
//use tempArray according to you.
Related
I am using one NSMutableArray with same string object.
Here is the code
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:#"hello",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",nil];
NSObject *obj = [arr objectAtIndex:2];
[arr removeObject:obj];
NSLog(#"%#",arr);
When i try to remove 3rd object of array, its removing all object with "hi" string.
I am not getting why its happening.
my doubt is while removing object, NSMutableArray match string or address.
It's because you're using removeObject which removes all objects that are "equal" to the one you pass in. As per this Apple documentation:
This method uses indexOfObject: to locate matches and then removes
them by using removeObjectAtIndex:. Thus, matches are determined on
the basis of an object’s response to the isEqual: message. If the
array does not contain anObject, the method has no effect (although it
does incur the overhead of searching the contents).
You're seeing the effects of literal strings here where each of those #"hi" objects will turn out to be the same object just added many times.
What you really want to do is this:
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:#"hello",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",nil];
[arr removeObjectAtIndex:2];
NSLog(#"%#",arr);
Then you're specifically removing the object at index 2.
According to https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html
removeObject:
Removes all occurrences in the array of a given object.
which is exactly the behaviour you're seeing. If you want to remove the object at a particular position, you want removeObjectAtIndex:.
NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:#"hello",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",#"hi",nil];
NSUInteger obj = [arr indexOfObject:#"hi"]; //Returns the lowest integer of the specified object
[arr removeObjectAtIndex:obj]; //removes the object from the array
NSLog(#"%#",arr);
I have a NSMutableArray (tripHistory) that gets a NSMutableDictionary (currentUpdate) added to it every second with new data.
[currentUpdate setObject:testVariable forKey:#"Test"];
[tripHistory addObject:currentUpdate];
[currentUpdate removeAllObjects];
Yet when I loop through tripHistory calling [[tripHistory objectAtIndex:i] description] everything is null.
My loop is as follows:
for (int i=0; i<[tripHistory count]; i++)
{
NSLog(#"%#", [[tripHistory objectAtIndex:i] description]);
}
To initialize my variables, the following code is called only for the first update.
tripHistory = [[NSMutableArray alloc] init];
currentUpdate = [[NSMutableDictionary alloc] init];
Any ideas?
Adding an object to an NSArray doesn't make a copy of it. All that happens is its reference count is incremented. Therefore currentUpdate and the NSDictionary added to tripHistory are one-and-the-same. If you remove objects from currentUpdate you are also removing objects from the NSDictionary in tripHistory.
After adding currentUpdate to tripHistory all you need to do is release currentUpdate and start again with a new empty NSDictionary for the next update.
Do not remove objects. Dictionary and array are keeping references to the same objects. If you want to add another dict to array then release first dict and create a new one
You are removing all of the contents of the dictionary. Adding the dictionary to your array doesn't make a copy: The array will retain a reference to the dictionary, which you proceed to empty out each time you add something to it.
You should allocate a new dictionary each time through the loop and then add that dictionary o the array. If you are not using garbage collection or ARC you should also release the dictionary after it is added to the array.
i have created NSMutableDictionary with 10 keys.Now i want to access NSMutableDictionary keys in a same order as it was added to NSMutableDictionary (using SetValue:* forKey:* );
How can i achieve that ?
If you absolutely must use a dictionary container, you have to use a key that is sortable by the order in which you add key-value pairs. Thus, when creating your dictionary, you use a key that is an auto-incrementing integer or similar. You can then sort on the (integer) keys and retrieve the values associated with those keys.
If you do all of that, however, you may as well just use an NSMutableArray and add values to the array directly! It will be much faster and require less code. You just retrieve objects in order:
for (id obj in myArray) { /* do stuff with obj... */ }
NSMutableDictionary can't do that. Take a look at e.g. Matt Gallaghers OrderedDictionary.
I wrote a quick method to take a source array (of objects that are all out of order) and a reference array (that has objects in a desired (and totally arbitrary) order), and returns an array where the items of the source array have been reorganized to match the reference array.
- (NSArray *) reorderArray:(NSArray *)sourceArray toArray:(NSArray *)referenceArray
{
NSMutableArray *returnArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [referenceArray count]; i++)
{
if ([sourceArray containsObject:[referenceArray objectAtIndex:i]])
{
[returnArray addObject:[arrReference objectAtIndex:i]];
}
}
return [returnArray copy];
}
Note that this is very fragile. It uses NSArray's containsObject: method, which ultimately will call NSObject's isEqual:. Basically, it should work great for arrays of NSStrings, NSNumbers, and maybe NSDates (haven't tried that one yet), but outside of that, YMMV. I imagine if you tried to pass arrays of UITableViewCells or some other really complex object, it would totally sh*t itself, and either crash or return total garbage. Likewise if you were to do something like pass an array of NSDates as the reference array and an array of NSStrings as the source array. Also, if the source array contains items not covered in the reference array, they'll just get discarded. One could address some of these issues by adding a little extra code.
All that said, if you're trying to do something simple, it should work nicely. In your case, you could build up the reference array as you are looping through your setValue:forKey:.
NSMutableArray *referenceArray = [[NSMutableArray alloc] init];
NSMutableDictionary *yourDictionary = [[ NSMutableDictionary alloc] init];
for (//whatever you are looping through here)
{
[yourDictionary setValue://whatever forKey:key];
[referenceArray addObject:key];
}
Then, when you want to loop over your items in the order they came in, you just
for (NSString *key in [self reorderArray:[myDict allKeys] toArray:referenceArray])
Actually you have a reference array in order manner then why you have to add to one more array.So i guess this approach is not good.Please consider my opinion.
Although #GenralMike 's answer works a breeze, it could be optimized by leaving off the unnecessary code as follows:
1) Keep an array to hold reference to the dictionary keys in the order they are added.
NSMutableArray *referenceArray = [[NSMutableArray alloc] init];
NSMutableDictionary *yourDictionary = [[ NSMutableDictionary alloc] init];
for (id object in someArray) {
[yourDictionary setObject:object forKey:someKey];
[referenceArray addObject:someKey]; // add key to reference array
}
2) Now the "referenceArray" holds all of the keys in order, So you can retrieve objects from your dictionary in the same order as they were originally added to the dictionary.
for (NSString *key in referenceArray){
//get object from dictionary in order
id object = [yourDictionary objectForKey:key];
}
I have 1 NSMutableArray and I want to convert whatever data in array will be in NSString.
tell me code for that. Array is nothing but object of NSMutableArray class.
If you want only the elements of array then you can try componentsJoinedByString:. This method returns all the elements with separator string without other formatting info.
[array componentsJoinedByString:#","];
Here "," is separator string.
It depends on how you want your string.
One approach could be iterate through array and convert each element of it.
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
[result appendString:[obj description]];
}
NSLog(#"The concatenated string is %#", result);
You can modify the above code based on item's class.
Below code will convert Array to string with commas and other information.
NSString * result = [array description];
It depends on how do you want to use that string.
One way to convert an object to a string is to call -description (or -descriptionWithLocale:) on that object. For NSArray (or NSMutableArray) -description method returns a string that represents the contents of the receiver, formatted as a property list. The result you'll get will also depend on how -description method implemented in objects in array.
I have a plist file that I am loading into an NSDictionary *setDictionary that contains a set of fields, pictureDesc1, pictureDesc2, etc.
Once the NSDictionary is loaded, I can retrieve a value using
[setDictionary objectForKey:#"pictureDesc1"];
But I cannot do a loop like this:
for (int i=1; i<=numberOfPictures; i++) {
NSString *keyName = [NSString stringWithFormat:#"pictureDesc%d",i];
NSString *description = [NSString stringWithFormat:#"%#", [setDictionary objectForKey:keyName]];
}
Is there a different way to dynamically call keys from within an NSDictionary?
If you keys fit a numerical pattern, then it would be easier and more robust to simply store them in a array. You could even store an array as a value in the dictionary with the key of "pictureDescription".
Then when you wanted to loop through them just use a simple numerical loop:
NSArray *pictArray=[setDictionary valueForKey:#"pictureDescription"];
NSString *description;
for (i=0;i<[pictArray count];i++){
description=[pictArray objectAtIndex:i];
//... do whatever
}
If you find yourself shoehorning the functionality of an array into a dictionary or vice versa you probably should back out and just use an array or dictionary in the first place. When using a dictionary, just use an enumerator to step through the values.