NSMutableArray with Integer - objective-c

I have this mutable array:
myIntegers = [NSMutableArray array];
[myIntegers addObject:[NSNumber numberWithInteger:indexDelete - 1]];
NSLog (#"Array: %#", myIntegers);
If I execute the code twice, with indexDelete first being 1 and then 2, I get this result:
Array: (
1
)
and then:
Array: (
2
)
But I would like to store both numbers like this:
Array: (
1
2
)
Why is it not adding, but replacing the object??

You're creating a new empty array every time that code executes, on this line:
myIntegers = [NSMutableArray array];
Adding an object to an empty array always leads to there being one object in that array.

Related

Remove null objects from mutable array in objective c

I have a mutable array of checked box
NSMutableArray *arrayOfCheckedBox = [NSMutableArray arrayWithObjects:namePropertyString, lastNamePropertyString, companyPropertyString, workEmailPropertyString, personalEmailPropertyString, workPhonePropertyString, cellNumberPropertyString, nil];
[arrayOfCheckedBox removeObjectIdenticalTo:[NSNull null]]; //not working
NSLog(#"array of check box = %#", arrayOfCheckedBox);
If I click on check boxes at index 0, 1 and 4, it will only collect object at indexes 0 and 1 only and will not detect index 4 at all.
I get the values at the selected index in log before getting it in arrayOfCheckedBox. How to get checked values in this case?
The problem is that you're hitting a nil value, so the arrayWithObjects: method thinks you're at the end of the list of objects.
Something like this will work:
NSMutableArray *arrayOfCheckedBox = [NSMutableArray arrayWithCapacity:7];
if (namePropertyString)
[arrayOfCheckedBox addObject:namePropertyString];
if (lastNamePropertyString)
[arrayOfCheckedBox addObject:lastNamePropertyString];
I would suggest storing the check box values in an array of BOOLs.
Wrap them in NSNumbers first and unwrap them to retrieve them.
Wrap to store,
[NSNumber numberWithBool:YES]]
Unwrap to retrieve,
[[your_array objectAtIndex:index] boolValue]
For null values appear to be string literals #"" rather than the NSNull.So use following way to remove null object :
NSLog(#"%d", [arrayOfCheckedBox count]);
NSString *nullString = #"<null>";
[arrayOfCheckedBox removeObject:nullString];
NSLog(#"%d", [arrayOfCheckedBox count]);

How to remove elements of NSDictionary

I have NSArray of NSDictionaries I need to extract 2 values or remove the values I don't need from the dictionary in the example below I need to remove id and NumberValue. any of you knows how can I do that?
Array: (
{
customerUS= {
DisplayName = "level";
InternalName = "Number 2";
NumberValue = 1;
id = xwrf
},
customerCAN= {
DisplayName = "PurchaseAmount";
InternalName = "Number 1";
NumberValue = 3500;
id = adf;
};
}
)
I'll really appreciate your help.
First thing, You can not remove/insert/update value in (immutable) NSDictionary/NSArray you need to convert NSDictionary/NSArray to (mutable) NSMutableDictionary/NSMutableArray.
such like
NSArray *myArr = ....;
NSMutableArray *newMutableArr = [myArr mutableCopy];
Then you can change in newMutableArr.
Such like
for(int i = 0 ; i < newMutableArr.count ; i ++)
{
[[newMutableArr objectAtIndex:i] removeObjectForKey:#"id"];
[[newMutableArr objectAtIndex:i] removeObjectForKey:#"NumberValue"];
}
EDITED:
Without Use of for loop and removeObjectForKey, if you have array of dictionary and both are mutable then you can also delete a key and its object from all elements of the array like this:
[newMutableArr makeObjectsPerformSelector:#selector(removeObjectForKey:) withObject:#"id"];
[newMutableArr makeObjectsPerformSelector:#selector(removeObjectForKey:) withObject:#"NumberValue"];
I would advice you to read Apple documents.
For modifying any Collection object after it is created, you need the mutable version.
For NSDictionary we have NSMutableDictionary. Read here.
We have a method for removing objects:
- (void)removeObjectForKey:(id)aKey
There are other methods as well. You can easily refer them in the above mentioned documentation.
Find out removeObjectForKey for deleting record from NSMutabledictionary.
removeObjectForKey pass the key value whatever you have like
all this are your key
DisplayName,
InternalName,
NumberValue,
id
do like this
removeObjectForKey:#"id";
First of all you have to convert the array to mutable array and then you can remove the key-value pairs from dictionary.
NSMutableArray *mutableArray = [yourArray mutableCopy];for(int i=0;i<mutableArray.count;i++){ NSMutableDictionary *outerDictionary = [mutableArray objectAtIndex:i]; for(NSString *key in outerDictionary.allKeys){ NSMutableDictionary *innerDictionary = [outerDictionary objectForKey:key]; [innerDictionary removeObjectForKey:#"id"]; [innerDictionary removeObjectForKey:#"NumberValue"]; }
}

copy elements from one array to another array

I like to copy the array A elements to Array B elements with specific
example :
array A=[0123]
array b=[1111111111111111111]
i want `b=[1111111101231111111]
int ip=0;
[b addObjectsFromArray:[A objectsAtIndexes:[NSIndexSetindexSetWithIndexesInRange:NSMakeRange(ip, 10)]]];
i know how to copy the array element , i want to know how to replace the object start from 9 to 13 in array b to replaced by array a element , can any give me hint
NSArray *a = #[#0,#1,#2,#3];
NSArray *b = #[#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1,#1];
NSMutableArray *c = [b mutableCopy];
// The range here is index->8 (9th object) and length->4
[c replaceObjectsInRange:NSMakeRange(8,4) withObjectsFromArray:a];
You need to create mutable copy of your array and modify them:
NSMutableArray* mutableArray = [yourArray mutableCopy];
Then you will access to this methods: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

extracting data from an array of arrays

I have the following code:
NSString *movies = [[NSString alloc] initWithData:webData7 encoding:NSUTF8StringEncoding];
NSLog(#"movies: %#", movies);
array_webdata = [parsedata objectWithString:movies error:nil];
userMovies = [array_webdata valueForKey:#"movies"];
NSLog(#"userMovies: %#", userMovies);
NSMutableArray *arrayCodes = [userMovies valueForKey:#"rtid"];
NSLog(#"arrayCodes: %#", arrayCodes);
In first line I save in movies the following:
movies:
{"movies":[["rtid","770672122"],["rtid","771268706"],["rtid","771240265"],["rtid","9377"]]}
with: userMovies = [array_webdata valueForKey:#"movies"]; I store in userMovies:
userMovies: (
rtid,
770672122
),
(
rtid,
771268706
),
(
rtid,
771240265
),
(
rtid,
9377
)
From here I want to extract the numbers to create another array like :
codes: (770672122, 771268706, 771240265, 9377)
But with following line, the execution crashes:
NSMutableArray *arrayCodes = [userMovies valueForKey:#"rtid"];
The error is EXC_BAD_ACCESS. On Debug Area shows no errors
Do you know what is wrong?
Thanks!
I have edited the question to make it more understandable
The code you have is clearly not going to work because you are assigning a number (or int, or some type) to a mutable array. You can't do that. You need to ADD them to the array since they will be contained IN THE ARRAY, those numbers ARE NOT arrays...
How are you getting it from JSON? Your array is probably an array of NSDictionary's so you'll want to do something like:
for (NSDictionary *aDictionary in userMovies)
{
[arrayCodes addObject: [aDictionary valueForKey:#"rtid"]];
}
You need to start by finding out what's in your array. Do:
NSLog(#"My array contains %#'s", [[userMovies valueForKey:#"rtid"] class]);
From there, if you get NSDictionary use the code above, and if you get an array, you'll have to loop through the array like:
for (NSArray *anArray in userMovies)
{
[arrayCodes addObject: [anArray objectAtIndex:1]];
}
SOLVED!
I have deleted my last two lines of code:
NSMutableArray *arrayCodes = [userMovies valueForKey:#"rtid"];
NSLog(#"arrayCodes: %#", arrayCodes);
and added the following
NSMutableArray *moviesCodes = [[NSMutableArray alloc] init];
for (int counter = 0; counter<[userMovies count]; counter++){
[moviesCodes addObject:[[userMovies objectAtIndex:counter] objectAtIndex:1]];
}
With this I have stored all the ids in the array called moviesCodes. The problem was that userMovies is an array of arrays

Trouble removing NSStrings in NSArray

In an NSArray, I have a 10 values. The values look something like this:
Hello
Hello2
My
My2
Name
Name2
Is
Is2
XcodeDev
XcodeDev2
And I want to delete every second NSString from the NSArray, so I am left with an array like the following. The values are random, and do not have 2 appended to every second one! How can I do this?
Hello
My
Name
Is
XcodeDev
First, you are going to need an instance of NSMutableArray, because NSArrays are immutable and therefore, you cannot change its contents.
NSMutableArray *ary = [NSMutableArray arrayWithArray:anImmutableArray];
Then, you can create an index set that holds all the odd indexes:
NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
for (int i = 1; i < [ary count]; i=i+2) {
[indexSet addIndex:i];
}
Finally, just call removeObjectsAtIndexes: method on the array.
[ary removeObjectsAtIndexes:indexSet];