This question already has answers here:
NSArray of united Arrays
(6 answers)
Closed 8 years ago.
I have been using an NSDictionary to store my JSON response. Now I tried to store them inside an array, so I can iterate over each values.
I used: valueForKeyPath and now each of those values are inside an NSArray.
The problem is instead of returning self.myarray.count (3), I only get (1). It wrote
the NSDictionary Keys into myarray at Index 0. So I have an Array inside myarray at Index 0 containing values (2,5,3).
Is there a way to quickly fix this and putting this like: index 0 : 2 , index 1: 5 , index 2: 3
and not index 0 : (2,5,3)?
Thanks I am still learning Obj-C.
As I understand you have perform something like following: [self.myarray addObject: dict.keys];
Use following instead: self.myarray = dict.keys;
If you do this only because you want to iterate over each value you could do the same with dictionary: for(id key in dict) or for(id key in dict.keys) iterates over each key. for(id value in dict.values) iterates over each value.
Related
I have 2 array of strings, lets say for simplicity it will be Array 1 - #[#"a",#"b",#"c"] and Array 2 will be - #[#"b",#"c", #"d"].
What i want is, to create an array from strings (yes, there will be strings, sometimes long enough, i put characters just for simplicity) that exclude strings contained in previous array. And second array, that otherwise, contain string that exist in first array and not exist in second.
So, with array i privded, it would be :
resultArray1 = #[#"d] (exist in second array, but not in first)
resultArray2 = #[#"a"] (exist in first array, but not in second)
How to enumerate through this arrays to get what i want to? Thanks.
Make a NSMutableArray from the array with the items you want to keep, and call removeObjectsInArray: on it:
NSMutableArray res1 = [arr1 mutableCopy];
[res1 removeObjectsInArray:arr2];
NSMutableArray res2 = [arr2 mutableCopy];
[res2 removeObjectsInArray:arr1];
This question already has answers here:
NSMutableArray addObject not working
(2 answers)
Closed 7 years ago.
I have created a function within my code that converts whatever is in the textfield of my app to append to my array. However, when I set a breakpoint in the code, it returns that my array, _descriptionArray is nil. Why is that?
I want whatever value is in the descriptionTextField.text to append to my _descriptionArray.
Thanks for the help!
-(NSMutableArray *)descriptionConversion{
[_descriptionArray addObject: (descriptionTextField.text)];
return _descriptionArray;
}
Did you ever initialize _descriptionArray? If not, be sure to initialize the array with _descriptionArray = [NSMutableArray array];. If you don't initialize the array, it will be nil. You can only add an object to an array; not to nil.
Is there anyway I can add specific objects in NSMutableArray to another array in objective c? I can accomplish that in Java but cannot figure it our for objective c
For example I have an array of 7 strings and I only want indexes 1, 3 ,7 stored in another array.
Here is one way of creating the array from the string values at particular indexes:
NSMutableArray *array = ...; // Array with strings
NSArray *someOtherArray = #[ array[1], array[3], array[7] ];
So both array[1] and someOtherArray[0] point to the same (NSString) instance, etc.
This is what NSIndexSet (and NSMutableIndexSet) is for.
You can build it manually or use helper methods on NSArray like:
indexesOfObjectsPassingTest:
to build an index set from a block. You can then enumerate over the NSIndexSet using a for loop - using the index to call into the original array.
i am having 2 NSMutableDictionaries that each has 5 NSArrays inside,
these 2 NSMutableDictionaries have excactly the same 5 keys and NSArrays but with different content,
i need to replace only 2 Arrays with the Arrays of the other NSMutableDictionary
so
dic1 = dic2; //isn't a solution...will replace all the contents of the one to the other...
i need something that can replace the object of a specific key with the other same key.
i tried
[dic1 setObject:[dic2 objectForKey:#"key1"] forKey:#"key1"];
[dic1 setObject:[dic2 objectForKey:#"key2"] forKey:#"key2"];
and i get ERROR: attempt to insert nil value (key: key1)'
The error just means [dic2 objectForKey:#"key1"] is returning a nil value. In other words, that key does not exist in the dictionary. Likewise, you can't set a nil value inside of an NSDictionary, so thats why the error is sent out.
Print out each of the dictionaries and ensure your keys/values are what you expect. You can use an NSLog or just pause the program with a breakpoint, and enter po dic1 in the console.
This question already has answers here:
Finding a substring in a NSString object
(7 answers)
NSString search whole text for another string
(3 answers)
Closed 9 years ago.
I am performing a method call that places any objects found in the background into an NSArray object called "objects".
When I NSLog the counts property of "objects" it tells me that the array contains 1 object which is correct.
When I NSLog the NSArray object called "objects" it prints out the following:
(
"<PFUser:nzyjeVFgjU:(null)> {\n email = \"hahshshs#aol.com\";\n username = hzhjsshhsppppp;\n verificationCode = 6449;\n}"
)
Here is my problem. I need to create an if statement that takes another value I already have and compares it to the 4 digit number that verificationCode is equal to eg. Above in the code it says "verificationCode = 6449"
I am basically trying to compare a 4 digit code that I already have to the verificationCode that is contained in this single NSArray object.
I know how to write if statements, but i have no idea how to specifically focus on "verificationCode = 6449" since it is just text inside of a string.
I have been trying to figure out a way to do this for an hour now so any help is greatly appreciated thank you.
Just found the answer here: https://stackoverflow.com/a/7574136/3117509
I tried searching earlier but was searching for "search array for string" when I should have been searching for something like "search for string within a string" or "search for text within a string."
If I understand your question correctly, you're trying to iterate through an array and find if a string is there? Don't have access to a Mac right now, so I'm not sure if this will compile.
NSArray* someArray = [[NSArray alloc] initWithObjects:#"test", #"test1", #"test2", nil];
for(int i = 0; i < (int)[someArray count]; i++) {
if([[someArray objectAtIndex:i] isEqualToString #"test"]) {
//match found. handle the match
}
}