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.
Related
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);
I currently have several arrays of strings which join together into one array of strings for a search. #"%# | %# | %# | %# | %# | %#". Now I am trying to separate the contents of the large array of strings, #"%#", back into the original six strings. Is there any way I can do this. Yes it sounds redundant but it is necessary at this point of development.
Edit: For clarification, I am using six parallel arrays and inserting each corresponding value between the pipes and converting it into a combined array of strings. I want to separate
"Word A | Word B | Word C | Word D | Word E | Word F", into
NSString A = "Word A";
NSString B = "Word B";
etc.
To separate this string you can use:
NSArray *array = [yourStrngToSeparate componentsSeparatedByString:#" | "];
If you want to put it back to string you don't have to use stringWithFormat, you can do:
NSString *string = [array componentsJoinedByString:#" | "];
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.
Let's say I have a set of objects with properties:
Object Quantity Color Shape Kind
----------------------------------------
APPLE 12 RED ROUND FRUIT
APPLE 3 GREEN ROUND FRUIT
ORANGE 6 ORANGE ROUND FRUIT
CARROT 0 RED CONICAL VEGETABLE
RADISH 24 RED ROUND VEGETABLE
Object and all properties except quantity are represented as strings. Quantity is a number.
I must compose a random list of objects, based on user's query.
Query contains values for all string properties (that is, all properties except quantity).
Value in query may be either exact property value, or a wildcard (meaning "any value would do for this property"), or a negation — "NOT this exact property value".
Query result is an object, picked by weighted random from all object with matching properties. Weight for the random pick is the quantity.
For example:
Query -> Probabilities -> Example
random result
-----------------------------------------------------------------------------
* ROUND FRUIT -> APPLE 12 / APPLE 3 = APPLE 15 -> APPLE
!GREEN ROUND FRUIT -> APPLE 12 / ORANGE 6 -> ORANGE
RED * * -> CARROT 0 / APPLE 12 / RADISH 24
= APPLE 12 / RADISH 24 -> RADISH
RED CONICAL VEGETABLE -> CARROT 0
= (none) -> (none)
For self-education purposes, I would like to build this system using Redis for data storage.
The question is — how to do this elegantly and with least amount of application logic (as opposed to in-Redis operations)? Weights and negation kind of spoil the picture. Otherwise it would be nicely doable with sets.
Any hints are welcome.
Since redis can only query keys and not values, a good option is to store the individual values of each object in seperate redis lists.
For example, when you add the object ...
APPLE 12 RED ROUND FRUIT
you would store it as
hmset obj:1 name apple qty 12 color red shape round kind fruit
and then ...
sadd name:apple obj:1,
sadd color:red obj:1
sadd shape:round obj:1
This way you have a way to interrogate sets directly and be able to pick the object using a random number based on, for example, the total number of items in the set returned.
Hope that helps. If you need more explanation, hit me up.
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.