NSMutableArray * val;
val = [[NSMutableArray alloc] initWithCapacity:15];
/*int outlineWidth = barOutlineWidth;
int outlineHalfWidth = (outlineWidth > 1) ? outlineWidth * 0.5f : 0;
*/
for ( Bar * obj in values )
{
// calcualte the bar size
float value = [obj value];
float scale = ( value / maxValue );
// shift the bar to the top or bottom of the render line
int pointY = lineHeight + ( (lineWidth * 0.5f) * ( ( value >= 0.0f ) ? -1 : 1 ) );
int barHeight = height * scale;
NSLog(#"%d", barHeight);
CGRect barRect = CGRectMake(pointX, pointY, width, -barHeight);
[val addObject:[NSNumber numberWithInt:barHeight]];
NSLog(#"%d", val);
I want to add the barheight (int) into array val.
Is this possible?
while running the code,
session started at 2010-09-16 13:21:50 +0530.]
2010-09-16 13:21:53.791 BarGraphSample[3168:20b] 78
2010-09-16 13:21:53.797 BarGraphSample[3168:20b] 69398112
2010-09-16 13:21:53.807 BarGraphSample[3168:20b] 235
2010-09-16 13:21:53.812 BarGraphSample[3168:20b] 69398112
2010-09-16 13:21:53.813 BarGraphSample[3168:20b] 156
2010-09-16 13:21:53.814 BarGraphSample[3168:20b] 69398112
this is the output.
Here the actual barheights are 78,235,156,
while printing the array val.
Im getting like values like "69398112"
What should I do?
You can only add pointers to objects to an NSMutableArray. If you use the NSNumber class though to wrap your integer, you should then be able to add that to the array.
int x = 10;
NSNumber* xWrapped = [NSNumber numberWithInt:x];
NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:15];
[array addObject:xWrapped];
int xOut = [[array lastObject] intValue]; //xOut == x;
Hope this helps.
Add a number instead.
NSNumber* foo = [NSNumber numberWithInteger:42];
or using boxed literals:
NSNumber* foo = #(42);
then add foo.
The issue is with your NSLog string namely
NSLog(#"%d", val);
%d is in fact the format specifier for an integer (int). You are passing it a NSMutableArray object.
Change the NSLog to
NSLog(#"%#", val);
The %# expects an object, and this will typically print out [myObject description], in this case a description of your val array.
Convert Int to NSNumber then add to your array
This is a simple way: #(yourInt)
int value = 5;
NSMutableArray * anArray = [[NSMutableArray alloc] init];
[anArray addObject: #(value)];
more information about at-compiler-directives
Related
I have a simple looking piece of code that has me completely flummoxed.
NSInteger ymax;
NSInteger ymin;
NSInteger numberIndex1;
NSInteger numberIndex2;
for (NSNumber *theNumber in array2)
{
if ([theNumber integerValue] > ymax) {
ymax = [theNumber integerValue];
numberIndex1 = [array2 indexOfObject:theNumber];
}
}
for (NSNumber *theNumber in array2)
{
if ([theNumber integerValue] < ymin) {
ymin = [theNumber integerValue];
numberIndex2 = [array2 indexOfObject:theNumber];
}
}
NSLog(#"Highest number: %d at index: %d", ymax, numberIndex1);
NSLog(#"Lowest number: %d at index: %d", ymin, numberIndex2);
The NSLog is outputted as:
Highest number: 129171656 at index: -1073752392 (Huh??)
Lowest number: 57 at index: 5 (Correct)
How do you explain this odd behaviour? Both the functions look the same. One is working and one isn't? I've played around a lot with this, but I still can't put my finger on it. Any help would be appreciated/
You can get maximum and minimum number as below code. It may help you
NSNumber * max = [array2 valueForKeyPath:#"#max.intValue"];
NSNumber * min = [array2 valueForKeyPath:#"#min.intValue"];
NSUInteger numberIndex1 = [array indexOfObject:min];
NSUInteger numberIndex2 = [array indexOfObject:max];
NSLog(#"Max Value = %d and index = %d",[max intValue],numberIndex1);
NSLog(#"Min Value = %d and index = %d",[min intValue],numberIndex2);
If I am not wrong you are considering the default value of NSInteger is 0, No, it isn't guaranteed to be zero, since it's a local automatic variable. Without initialization, its value is indeterminate.
so you need to set default values for your var, start with ymax = -1;
Please initialize NSInteger ymax = 0;
NSInteger ymin = 0 ;
NSInteger numberIndex1 = 0;
NSInteger numberIndex2 = 0;
It will fix your issue.
Otherwise it is checking with a garbage value and giving wrong result.
enjoy with my answer.......happy coding
NSArray *arr1=[[NSArray alloc]initWithObjects:#"0.987",#"0.951",#"0.881",#"0.784",#"0.662",#"0.522",#"0.381",#"-0.265",#"-0.197",
#"0.189",#"-0.233",#"0.310",#"0.402",#"0.402",#"0.988",#"0.633",#"0.661",#"0.656",#"0.617",#"0.634",#"0.690",#"0.767",#"0.836",nil];
NSNumber * max = [arr1 valueForKeyPath:#"#max.floatValue"];
NSString *str=[NSString stringWithFormat:#"%#",max];
NSInteger path=[arr1 indexOfObject:str];
NSIndexPath *indepath=[NSIndexPath indexPathForItem:path inSection:0];
NSNumber * min = [arr1 valueForKeyPath:#"#min.floatValue"];
NSString *str1=[NSString stringWithFormat:#"%#",min];
NSInteger path1=[arr1 indexOfObject:str1];
NSIndexPath *indepath1=[NSIndexPath indexPathForItem:path1 inSection:0];
NSLog(#"Max Value = %f and index = %ld",[max floatValue],(long)indepath.row);
NSLog(#"Min Value = %f and index = %ld",[min floatValue],(long)indepath1.row);
A more legitimate solution would be:
NSArray *originalArray = #[[NSNumber numberWithInt:91],
[NSNumber numberWithInt:12],
[NSNumber numberWithInt:99123],
[NSNumber numberWithInt:9],
[NSNumber numberWithInt:43234]];
NSArray *sortedArray = [originalArray sortedArrayUsingSelector:#selector(compare:)];
NSNumber *minNumber = [sortedArray objectAtIndex:0];
NSNumber *maxNumber = [sortedArray lastObject];
NSInteger minIndex = [originalArray indexOfObject:minNumber];
NSInteger maxIndex = [originalArray indexOfObject:maxNumber];
I am trying to assign a tag to button. The normal command is:
button.tag = 1;
The tag must be an integer.
My problem is that I would like to assign an integer which I stored in an array (tabReference) which is yet again part of a class (currentNoteBook). So I need this:
int k = 0;
button.tag = [currentNoteBook.tabReference objectAtIndex:k]; // This is where I get the warning.
This doesn't seem to work, however, as xCode tells me: Passing argument 1 of setTag: makes integer from pointer without a cast.
My array looks like this (I tried to use integers...):
NSMutableArray *trArray = [[NSMutableArray alloc] init];
NSNumber *anumber = [NSNumber numberWithInteger:1];
[trArray addObject: anumber];
[trArray addObject: anumber];
[trArray addObject: anumber];
[trArray addObject: anumber];
currentNoteBook.tabReference = trArray;
An NSMutableArray stores a modifiable array of objects. You can't directly store an integer in an NSMutableArray. That's why you have to do something like this to store a bunch of integers:
NSMutableArray *the_array = [[NSMutableArray alloc] init];
int max = 100;
for (int i = 0; i < max; i++)
{
NSNumber *temp_number = [NSNumber numberWithInt:arc4random() % max];
[the_array addObject:temp_number];
}
Of course, you could do pretty much the same thing and store something else in there:
NSMutableArray *the_array = [[NSMutableArray alloc] init];
int max = 100;
int max_x = 50;
int max_y = 25;
int max_w = 100;
int max_h = 200;
for (int i = 0; i < max; i++)
{
CGFloat temp_x = arc4random() % max_x;
CGFloat temp_y = arc4random() % max_y;
CGFloat temp_w = arc4random() % max_w;
CGFloat temp_h = arc4random() % max_h;
CGRect temp_rect = CGRectMake(temp_x, temp_y, temp_w, temp_h);
[the_array addObject:[NSValue valueWithCGRect:temp_rect]];
}
When you go to retrieve these values you need to specify what it is you want out of the array because the same array can contain very different objects.
For your integers:
for (int i = 0; i < max; i++)
{
NSLog(#"%i: %i", i, [[the_array objectAtIndex:i] intValue]);
}
For the CGRect example:
for (int i = 0; i < max; i++)
{
CGRect temp_rect = [[the_array objectAtIndex:i] CGRectValue];
NSLog(#"%i: x:%f y:%f w:%f h:%f", i, temp_rect.origin.x, temp_rect.origin.y, temp_rect.size.width, temp_rect.size.height);
}
In a nutshell, you are storing objects not integers in your code. You have to pull them out of there as objects and then extract your integer to get your data back.
Just found the answer in another question I posed:
it must be:
btn.tag = [[currentNoteBook.tabReference objectAtIndex:k] intValue];
My array's for loop currently results in numbers like: 0,1,2,3,4,5,6 ....to 1000.
Here is the code:
pickerArray = [[NSMutableArray alloc] initWithCapacity:700];
for ( int i = 1 ; i <= 1000 ; i = i++)
[pickerArray addObject:[NSString stringWithFormat:#"%i", i]]
I want it instead, to have the following pattern, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, etc.
And it is displayed using this code:
float weight = [[pickerArray objectAtIndex:row] intValue];
label.text = [NSString stringWithFormat:#"%f lb", weight];
pickerArray = [[NSMutableArray alloc] initWithCapacity:700];
for ( float i = 0.0 ; i <= 1000.0 ; i = i + 2.5)
{
//[pickerArray addObject:[NSString stringWithFormat:#"%.1f", i]]
[pickerArray addObject:[NSNumber numberWithFloat:i];
}
float weight = [[pickerArray objectAtIndex:row] floatValue];
label.text = [NSString stringWithFormat:#"%.1f", weight];
You can do this in the loop:
for ( float f = 0; f <= 1000; f = f + 2.5 )
[pickerArray adObject:[NSString stringWithFormat:#"%.1f", f]];
Or you can just multiply it when creating the strings:
[pickerArray adObject:[NSString stringWithFormat:#"%.1f", 2.5*i]];
Also, if you are immediately putting 1000 objects in, why start with an initial capacity of 700? And are you really sure you want all those strings in this array?
EDIT: and having looked at your edit, are you really sure you want to do this seemingly-redundant conversion back and forth to strings? Well, if you're sure:
float weight = [[pickerArray objectAtIndex:row] floatValue];
Try something like that:
pickerArray = [[NSMutableArray alloc] initWithCapacity:1001];
for ( int i = 0 ; i <= 1000 ; ++i)
[pickerArray addObject:[NSString stringWithFormat:#"%.1f", 2.5*i]]
I'm trying to calculate the median of a (small) set of NSNumbers in an NSArray. Every object in the NSArray is a NSNumber.
Here is what I'm trying, but it's not working:
NSNumber *median = [smallNSArray valueForKeyPath:#"#median.floatValue"];
NSArray *sorted = [smallNSArray sortedArrayUsingSelector:#selector(compare:)]; // Sort the array by value
NSUInteger middle = [sorted count] / 2; // Find the index of the middle element
NSNumber *median = [sorted objectAtIndex:middle]; // Get the middle element
You can get fancier. For example, the median of a set with an even number of numbers is technically the average of the middle two numbers. You could also wrap this up into a neat one-line method in a category on NSArray:
#interface NSArray (Statistics)
- (id)median;
#end
#implementation NSArray (Statistics)
- (id)median
{
return [[self sortedArrayUsingSelector:#selector(compare:)] objectAtIndex:[self count] / 2];
}
#end
For anyone who has the unusual need for this function, here's a category method on NSArray that will work with both an odd and an even number of elements:
NSARRAY CATEGORY METHOD
- (float)median {
if (self.count == 1) return [self[0] floatValue];
float result = 0;
NSUInteger middle;
NSArray * sorted = [self sortedArrayUsingSelector:#selector(compare:)];
if (self.count % 2 != 0) { //odd number of members
middle = (sorted.count / 2);
result = [[sorted objectAtIndex:middle] floatValue];
}
else {
middle = (sorted.count / 2) - 1;
result = [[#[[sorted objectAtIndex:middle], [sorted objectAtIndex:middle + 1]] valueForKeyPath:#"#avg.self"] floatValue];
}
return result;
}
TEST
NSArray * singleElement = #[#1];
NSArray * oddNumberOfElements = #[#3, #5, #7, #12, #13, #14, #19, #20, #21, #22, #23, #29, #39, #40, #56];
NSArray * evenNumberOfElements = #[#3, #5, #7, #12, #13, #14, #19, #20, #21, #22, #23, #29, #40, #56];
NSLog(
#"oddNumberOfElements: %f, evenNumberOfElements: %f singleElement: %f",
[oddNumberOfElements median], [evenNumberOfElements median], [singleElement median]
);
//oddNumberOfElements: 20.000000, evenNumberOfElements: 19.500000 singleElement: 1.000000
Swift Extension
extension Array where Element: Comparable {
var median: Element {
return self.sort(<)[self.count / 2]
}
}
I have an NSArray of NSNumbers and want to find the maximum value in the array. Is there any built in functionality for doing so? I am using iOS4 GM if that makes any difference.
The KVC approach looks like this:
int max = [[numbers valueForKeyPath:#"#max.intValue"] intValue];
or
NSNumber * max = [numbers valueForKeyPath:#"#max.intValue"];
with numbers as an NSArray
NSArray * test= #[#3, #67, #23, #67, #67];
int maximumValue = [[test valueForKeyPath: #"#max.self"] intValue];
NSLog(#" MaximumValue = %d", maximumValue);
// Maximum = 67
Here is the swift version
let maxValue = (numbers.value(forKeyPath: "#max.self") as! Double)
Hope will helpful to you.
NSArray * arrayOfBarGraphValues = #[#65, #45, #47 ,#87 , #46, #66 ,#77 ,#47 ,#79 ,#78 ,#87 ,#78 ,#87 ];
int maxOfBarGraphValues = [[arrayOfBarGraphValues valueForKeyPath: #"#max.self"] intValue];
NSLog(#" MaximumValue Of BarGraph = %d", maxOfBarGraphValues);