I have some data like this :
1, 5, 2, 9, 7, 6, 3, 8, 0, 4
but I actually want this:
5, 6, 7, 8, 9
only start to 5 .
and I use this:
NSArray *ary = #[ #"1", #"5", #"2", #"9", #"7", #"6", #"3", #"8", #"0", #"4" ];
NSArray *myArray = [ary sortedArrayUsingDescriptors:
#[[NSSortDescriptor sortDescriptorWithKey:#"doubleValue" ascending:YES]]];
NSString *numberExpr =#"^[5-9]";
NSArray *ary = #[ #"1", #"5", #"2", #"9", #"7", #"6", #"3", #"8", #"0", #"4" ];
NSArray *myArray= [ary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self MATCHES %#",numberExpr]];
This solution treats the contents as numeric, as suggested by the question's sample code.
double startingValue = 5;
NSArray *ary = #[ #"1", #"5", #"2", #"9", #"7", #"6", #"3", #"8", #"0", #"4" ];
NSArray *myArray= [ary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"self.doubleValue >= %#", #(startingValue)]];
myArray = [myArray sortedArrayUsingDescriptors:
#[[NSSortDescriptor sortDescriptorWithKey:#"doubleValue" ascending:YES]]];
Related
I am fresher to iOS. In my application i have 3 mutable arrays with objects like
NSMutableArray *MuteItem = [NSMutableArray alloc]initWithObjects:#"a", #"b", #"b", #"c", #"c", #"c", nil]];
NSMutableArray *MuteQuantity = [NSMutableArray alloc]initWithObjects:#"1", #"1", #"1", #"1", #"1", #"1", nil]];
NSMutableArray *MutePrice = [NSMutableArray alloc]initWithObjects:#"4", #"3", #"3", #"6", #"6", #"6", nil]];
Now i need to print that 3 mutable arrays values with counting the same item's quantity and calculate the price also like objects
MuteItem = { a, b, c }
MuteQuantity = { 1, 2, 3 } // counting of same item's quantity like {1, 1+1, 1+1+1}
MutePrice = { 4, 6, 18 } // here addition of same item's prices like {4, 3+3, 6+6+6}
So anybody, would you please help me in this problem. Thanks in advance.
This code will do exactly as you requested, and will even handle any keys in MuteItem, and will generate three new arrays with the aggregate information from each of the three original arrays.
NSMutableArray* muteItem = [[NSMutableArray alloc] initWithObjects: #"a", #"b", #"b", #"c", #"c", #"c", nil];
NSMutableArray* muteQuantity = [[NSMutableArray alloc] initWithObjects: #"1", #"1", #"1", #"1", #"1", #"1", nil];
NSMutableArray* mutePrice = [[NSMutableArray alloc] initWithObjects: #"4", #"3", #"3", #"6", #"6", #"6", nil];
NSMutableArray* setItem = [NSMutableArray array];
NSMutableArray* setQuantity = [NSMutableArray array];
NSMutableArray* setPrice = [NSMutableArray array];
NSSet* itemSet = [NSSet setWithArray: muteItem];
for (NSString* key in itemSet) {
NSIndexSet* indices = [muteItem indexesOfObjectsPassingTest: ^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [obj isEqualToString: key];
}];
__block NSInteger totalQuantity = 0;
__block NSInteger totalPrice = 0;
[indices enumerateIndexesUsingBlock: ^void(NSUInteger idx, BOOL *stop) {
totalQuantity += [[muteQuantity objectAtIndex: idx] integerValue];
totalPrice += [[mutePrice objectAtIndex: idx] integerValue];
}];
[setItem addObject: key];
[setQuantity addObject: [NSNumber numberWithInteger: totalQuantity]];
[setPrice addObject: [NSNumber numberWithInteger: totalPrice]];
}
NOTE: This code assumes you are using ARC. Also, in your original code you forgot to nil terminate your array constructors.
EDIT: I notice that your prices are integers, you may want to change them to floats if your currency uses decimal fractions. This would require changing the definition of totalPrice to float and you would want to change the end of the totalPrice += line from integerValue to floatValue.
EDIT2: Renamed all variables that started with a capital letter as this violates standard naming convention. Only class names should begin with a capital letter, variables should always begin with lowercase, or an _ for instance variables. :)
How can I order these values in the array in reverse order?
_gradientArrayToWhite = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", #"8", #"9", #"10", nil];
Using one of the sorting methods of NSArray
NSArray * sorted = [_gradientArrayToWhite sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 integerValue] < [obj2 integerValue];
}];
NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],[NSNumber numberWithInt:4],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:7],[NSNumber numberWithInt:8],[NSNumber numberWithInt:9],[NSNumber numberWithInt:10], nil];
Here You can use your array directly also like this
NSArray *arr1 = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", #"8", #"9", #"10", nil];
NSArray *revArr = [[arr reverseObjectEnumerator] allObjects];
Result = <__NSArrayM 0x71348e0>(
10,
9,
8,
7,
6,
5,
4,
3,
2,
1
)
Anyone got any ideas why this Table View code is crashing?
This, in my viewDidLoad:
itemArray = [NSArray arrayWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", nil];
Then, this in my cellForRowAtIndexPath method:
cell.textLabel.text = [NSString stringWithFormat:#"Item (%#)", [itemArray objectAtIndex:indexPath.row]];
When I scroll down (i.e. so that object 1 goes off screen) then scroll back to try and see object 1, it crashes at this previous line.
It's fine if I replace the offending line with something like this:
cell.textLabel.text = #"test";
UPDATE: Answer was that the array was not being retained.
This line fixed the problem:
itemArray = [[NSArray arrayWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", nil] retain];
OR
itemArray = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", nil];
Your itemArray seems not to be retained. arrayWithObjects: returns an autoreleased object. You need to retain or copy it, or use the syntactic sugar of a retaining property.
I can not figure out why this code is bad. I thought it was ok to use static strings in dictionaryWithObjectsAndKeys however it fails at runtime with a EXC_BAD_ACCESS. I used the debugger to identify that it is in fact failing at the definition line. It never outputs "Made it here".
- (NSString *)polyName:(NSUInteger)vertices {
NSLog(#"looking for polyName with %d vertices.", vertices);
NSDictionary *polNameDict = [NSDictionary dictionaryWithObjectsAndKeys:
#"triangle", #"3",
#"square", #"4",
#"pentagon","5",
#"Hexagon", #"6",
#"Heptagon", #"7",
#"Octagon", #"8",
#"Nonagon", #"9",
#"Decagon", #"10",
#"Hendecagon", #"11",
#"Dodecagon", #"12",
nil];
NSLog(#"Made it here");
NSString *key = [NSString stringWithFormat: #"%d", vertices];
NSString *polName = [polNameDict objectForKey:key];
return (polName);
// Memory management: No need to release polNameDict, key, polName because they use
// convenience functions
}
The problem is here:
#"pentagon","5"
It expects an object (NSString) but instead there's a regular string.
Change it to:
#"pentagon", #"5"
What line does the EXC_BAD_ACCESS occur on? Utilize breakpoints and let us know.
For now, if I were you, I'd do something like this:
NSDictionary *polNameDict = [[NSDictionary alloc] initWithObjectsAndKeys:
#"triangle", #"3",
#"square", #"4",
#"pentagon",#"5",
#"Hexagon", #"6",
#"Heptagon", #"7",
#"Octagon", #"8",
#"Nonagon", #"9",
#"Decagon", #"10",
#"Hendecagon", #"11",
#"Dodecagon", #"12",
nil];
instead of how you have it now.
I'm looking for a method of turning a NSMutableArray into a string. Is there anything on a par with this Ruby array method?
>> array1 = [1, 2, 3]
>> array1.join(',')
=> "1,2,3"
Cheers!
NSArray *array1 = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];
NSString *joinedString = [array1 componentsJoinedByString:#","];
componentsJoinedByString: will join the components in the array by the specified string and return a string representation of the array.
The method you are looking for is componentsJoinedByString.
NSArray *a = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];//returns a pointer to NSArray
NSString *b = [a componentsJoinedByString:#","];//returns a pointer to NSString
NSLog(#"%#", b); // Will output 1,2,3
NSArray class reference:
NSArray *pathArray = [NSArray arrayWithObjects:#"here",
#"be", #"dragons", nil];
NSLog(#"%#",
[pathArray componentsJoinedByString:#" "]);