Assigning an arrays count to an int, NSNumber or NSUInteger fails - objective-c

Assigning an int, NSNumber and NSUInteger to an arrays count fails...
this is my line of code
int diamondCount = [diamonds count];
where diamonds is an nsarray
I built the app and it threw an EXC_BAD_ACESS on that line
So i looked at the NSArray count method and found it returns an nsuinteger
So i did:
NSUInteger diamondCount = [diamonds count];
that still threw me EXC_BAD_ACCESS so at last resort i assigned it to an nsnumber with numberWithInteger:
it stilled crashed.... any thoughts?

diamonds does not point to a real (and valid) NSArray. Check its initialization.

Try this:
NSArray *myArray;
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
NSString *aString = #"a string";
myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];
int i = [myArray count];
NSLog(#"%d",i);
NSInteger j = [myArray count];
NSLog(#"%d",j);
NSNumber *k = [myArray count];
NSLog(#"%d",k);
/*output
2011-11-21 17:01:48.272 test[48840:1303] 3
2011-11-21 17:01:48.272 test[48840:1303] 3
2011-11-21 17:01:48.273 test[48840:1303] 3*/

Related

sum quantity of dictionaries in array

I have a NSArray containing a list of NSDictionary. Like:
NSArray *array = ...;
NSDictionary *item = [array objectAtIndex:0];
NSLog (#"quantity: %#", [item objectForKey: #"quantity"]);
How can I sum all the quantities contained in all dictionaries of the array?
I think you can try KVC
NSMutableArray *goods = [[NSMutableArray alloc] init];
NSString *key = #"quantity";
[goods addObject:#{key:#(1)}];
[goods addObject:#{key:#(2)}];
[goods addObject:#{key:#(3)}];
NSNumber *sum = [goods valueForKeyPath:#"#sum.quantity"];
NSLog(#"sum = %#", sum);
If you call valueForKey: on an array it gives you an array of all the values for that key, so [array valueForKey:#"quantity"] will give you an array which you can loop over and sum all the values.
NSMutableArray *quantityArray = [item objectForKey: #"quantity"];
int total =0;
for(int i=0;i<[quantityArray count];i++)
{
total+= [quantityArray objectAtIndex:i];
}

NSMutableDictionary returns empty NSMutableArray

At mouseDown and mouseDragged:
locll = [self convertPoint: [event locationInWindow] fromView:nil];
NSValue *locationValuell = [NSValue valueWithPoint:locll];
[vertices addObject:locationValuell];
at mouseUp
NSString *index = [NSString stringWithFormat:#"%d", aIndex++];
[aDict setObject:vertices forKey:index];
NSArray *allKeys = [aDict allKeys];
NSLog(#"dict count: %ld", [allKeys count]);
NSString *index1 = [NSString stringWithFormat:#"%d", aIndex];
NSMutableArray *a = [aDict objectForKey:index1];
NSLog(#"a count:%li", [a count]);
at initWithCoder
int aIndex = 0;
dict count returns how many objects are stored in dictionary. And it works. But later when I try to get array back from dictionary, I check how much objects array has ([a count]) and it returns 0. So i guess NSMutableDictionary empties my NSMutableArray, or I am taking it back in wrong way.
Here
NSString *index = [NSString stringWithFormat:#"%d", aIndex++];
you increment aIndex after using it (post-increment). So if index is "0" then index1 will be "1". Therefore
NSMutableArray *a = [aDict objectForKey:index1];
returns nil.
You are increasing your aIndex by 1 after using it for saving vertices in aDict. And you access this key after it(aIndex) is increased by 1. Naturally your aDict doesn't contain any array for that key.
Just try out this:
NSMutableArray *a=[NSMutableArray arrayWithObjects:[aDict objectForKey:index1], nil];
It should be work.

Get Integer value from NSNumber in NSArray

I have an NSArray with NSNumber objects that have int values:
arrayOfValues = [[[NSArray alloc] initWithObjects: [NSNumber numberWithInt:1], [NSNumber numberWithInt:3], [NSNumber numberWithInt:5], [NSNumber numberWithInt:6], [NSNumber numberWithInt:7], nil] autorelease];
[arrayOfValues retain];
I'm trying to iterate through the array like this:
int currentValue;
for (int i = 0; i < [arrayOfValues count]; i++)
{
currentValue = [(NSNumber *)[arrayOfValues objectAtIndex:i] intValue];
NSLog(#"currentValue: %#", currentValue); // EXE_BAD_ACCESS
}
What am I doing wrong here?
You are using the wrong format specifier. %# is for objects, but int is not an object. So, you should be doing this:
int currentValue;
for (int i = 0; i < [arrayOfValues count]; i++)
{
currentValue = [(NSNumber *)[arrayOfValues objectAtIndex:i] intValue];
NSLog(#"currentValue: %d", currentValue); // EXE_BAD_ACCESS
}
More information in the docs.
This worked for me when creating an NSArray of enum values I wanted to call (which is useful to know that this is the proper solution for that) I'm using Xcode 6.4.
int currentValue = (int)[(NSNumber *)[arrayOfValues objectAtIndex:i] integerValue];
sosborn's answer will throw a warning since it is still necessary to cast the NSInteger to an int.
Typecasting in ObjC is the bane of my existence. I hope this helps someone!

How to put in array a different types of field in objective-c

I'm writing an app for iPhone in objective-c. I want to declare an array that will hold different type of fields , like: int, NSString, bool.
Can I do it?
You can put whatever items in an NSArray as long as they are objects. So you have to wrap items that are not objects (such as BOOL, int and CGPoint) in some kind of objects such as NSNumber or NSValue.
NSMutableArray *array = [[NSMutableArray] alloc] init];
[array addObject:myString];
[array addObject:[NSNumber numberWithInt:1]];
[array addObject:[NSNumber numberWithFloat:1.0]];
[array addObject:[NSValue valueWithPoint:myPoint]]; // myPoint is a CGPoint
[array addObject:[NSValue valueWithRect:myRect]]; // myRect is a CGRect
Yes, you can
NSMutableArray *array = [NSMutableArray array];
NSString *string = #"str";
[array addObject:string]; //string
NSNumber *num = [NSNumber numberWithInt:1];
[array addObject:num]; //int
NSNumber *boolNum = [NSNumber numberWithBool:YES];
[array addObject:boolNum]; //bool
Use NSMutableArray.
NSMutableArray *array = [[NSMutableArray alloc]init];
Now use addObject: method to add objects. for adding int, bool value create NSNumber object.
It is possible. Just create the array and add the objects you want added.

finding a number in array

I have an Array {-1,0,1,2,3,4...}
I am trying to find whether an element exist in these number or not, code is not working
NSInteger ind = [favArray indexOfObject:[NSNumber numberWithInt:3]];
in ind i am always getting 2147483647
I am filling my array like this
//Loading favArray from favs.plist
NSString* favPlistPath = [[NSBundle mainBundle] pathForResource:#"favs" ofType:#"plist"];
NSMutableDictionary* favPlistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:favPlistPath];
NSString *favString = [favPlistDict objectForKey:#"list"];
NSArray *favList = [favString componentsSeparatedByString:#","];
//int n = [[favList objectAtIndex:0] intValue];
favArray = [[NSMutableArray alloc] initWithCapacity:100];
if([favList count]>1)
{
for(int i=1; i<[favList count]; i++)
{
NSNumber *f = [favList objectAtIndex:i];
[favArray insertObject:f atIndex:(i-1)];
}
}
That's the value of NSNotFound, which means that favArray contains no object that isEqual: to [NSNumber numberWithInt:3]. Check your array.
After second edit:
Your favList array is filled with NSString objects. You should convert the string objects to NSNumber objects before inserting them in favArray:
NSNumber *f = [NSNumber numberWithInt:[[favList objectAtIndex:i] intValue]];