I have 2 NSArray's that are holding values...
For example NSArray 1 has values 1 2 4 in it
and NSArray 2 has values 1 2 4 5 6 in it.
How can I write code to compare these 2 arrays to get the following information...
Count the values that are the same (so in this case 3) and count the values that are not the same (in this case 2).
I am simply populating the arrays like this:
NSString *s = #"1,2,4";
NSArray *numbers = [s componentsSeparatedByString:#","];
where *s is actually getting the text from a UITextField. If sorting mattering in comparing can you show me code to sort to make sure the user doesnt put the numbers in order?
If you are fine with sets instead of arrays, you can use NSMutableSet instead of NSArray. NSMutableSet has nice methods like intersectSet: and minusSet:
I would probably use the following method of the NSArray class:
enumerateObjectsUsingBlock.
and code the block testing for membership in the other array with the method:
indexOfObjectIdenticalTo.
If this isn't clear to you let me know.
Related
I am developing an camera app ,and showing the taken photos in thumbnail view with the numbers .i am displaying the numbers from array index value in ascending order.Like this.In this image numbers are in 1 , 2, 3, i want the numbers in 3 ,2 ,1
Now i want the numbers in descending order.Please help me to do this .my code is ..
NSString *the_index_path = [NSString stringWithFormat:#"%li", (long)indexPath.row+1];
Cell.waterMark_lbl.text = the_index_path;
this is code is in cellforitem at index method in collection view .
Thanks in advance !!!
You can rearrange the data model in reverse order and from there the data will be displayed.
Plan B, more convoluted, it would not change the order. When you have to show the cell 0, display the last, with cell 1 shows the penultimate, etc. You should generate a new indexPath every time you have to show the data.
The other option I understand is that for the first cell you want to show the last number. The operation would be similar, but without generating the new indexPath.
You have to do the subtraction of the total number of elements. Suppose the total number of elements is NUMBER_OF_ELEMENTS
Cell.waterMark_lbl.text = [NSString stringWithFormat:#"%li",NUMBER_OF_ELEMENTS - indexPath.row ]
In the case of reorder the cells without reorder the data, you have to créate a new indexPath and get the data value for display
NSIndexPath *ip = [NSIndePath indexPathForRow:NUMBER_OF_ELEMENTS - indexPath.row inSection:indexPath.section ]
sorry for lexical errors. I have the objective c somewhat forgotten
My dictionary looks like this
#{#"Blue": #"Big",
#"Red": #"medium",
#"Yellow": #"small"}
I would like to know that the highest key length is 6, because Yellow is the longest key
You can try this. Suppose a is your dictionary. You can find the source here: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html
NSArray *array = a.allKeys;
NSNumber* maxLength= [array valueForKeyPath:#"#max.length"];
NSLog(#"Longest is %lu",maxLength.integerValue);
My class has some #properties (strong); apples, bananas and oranges of NSArray* type; and I'm wondering if this:
for(NSArray* __strong fruit in #[apples, bananas, oranges]) {
fruit = [fruit sortedArrayUsingComparator:comparator];
}
is the same as this:
apples = [apples sortedArrayUsingComparator:comparator];
bananas = [bananas sortedArrayUsingComparator:comparator];
oranges = [oranges sortedArrayUsingComparator:comparator];
comparator is an NSComparator.
I think they should be the same but I'm not sure how the __strong relates to the for loop in this context.
this is not the same code, the loop will not change the values of the variables apples, bananas and oranges
in your loop you assigning the sort-result to the local variable fruit, this will not affect the contents of the values stored in apples, bananas or oranges.
in the 'unrolled' code you assigning the sort-result to the original variables, therefore overwriting the content of this variables.
Also i think your loop-type Fruit is wrong, unless apples, bananas and oranges are of type Fruit and not of type NSArray which the rest of the code suggests.
No, it's not the same , the same would be:
NSArray* fruits= [apples sortedArrayUsingComparator: comparator ];
fruits= [bananas sortedArrayUsingComparator: comparator];
fruits= [oranges sortedArrayUsingComparator: comparator];
So the first two sorts are useless, because you assign fruits just to the last sorted array: oranges.
A possible solution
It's not clear what you want to achieve, maybe something like this:
NSArray* sortedFruits;
for(NSArray* fruit in #[apples, bananas, oranges])
{
NSArray* sorted = [fruit sortedArrayUsingComparator:comparator];
[sortedFruits addObject: sorted];
}
This way you get an array with 3 items: the sort results of apples, bananas and oranges.
I wanted to ask if the following scenario is somehow possible to happen.
I have an Nsmutablearray and i have in it 5 objects. I load the Nsmutablearray on an uitableview. I want to insert a new object at the top of the other five objects but also move down all the previously inserted objects down by one. let me give you an example
0 one
1 two
2 three
3 four
4 five
i want to result like this after the new object inserted on the top
0 six
1 one
2 two
3 three
4 four
5 five
Any help appreciated.
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
This method of NSMutableArray is what you are looking for.
If index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room.
The method you are looking for is: - [NSMutableArray insertObject:atIndex:].
In your example, the index would be 0.
I have 3 NSArrays with:
item: amount
A: 1
B: 2
C: 3
A: 2
E: 1
F: 6
C: 5
D: 1
F: 3
After "combining" these into one, I need:
A: 3
B: 2
C: 8
D: 1
E: 1
F: 9
Do I first combine all the arrays into one and then sum and remove the duplicates?
You could use an NSCountedSet. I'm not clear on the structure of the data in your arrays, but by assuming that your B: 2 means that you have two B's in the array, then something like this would work:
NSCountedSet *set = [NSCountedSet setWithCapacity:[array1 count]+[array2 count]+[array3 count]];
[set addObjectsFromArray:array1];
[set addObjectsFromArray:array2];
[set addObjectsFromArray:array3];
// Test it out!
NSUInteger countForC = [set countForObject:objC];
// countForC == 8
Instead of using a NSArray you could try using a NSMutableDictionary where the key is inherent in the objects structure. That will allow you to iterate through each of your arrays of letters and counts then query for the value with the key, get the value and add to the value, then continue processing.
One possibility would be to use:
Use predicates to extract like sets of data (by item) into separate arrays. See Collection predicates guide
Key Value Coding to sum the value field of each of the resulting arrays (by item). See KVO collection operators.
Pop the results in whatever structure you like (NSArray or NSDictionary).
There may be performance considerations to explore. Alternatively, iterate the array, pulling out matching items in a separate NSDictionary (keyed on item) and summing as you go.