Jump to content

A small problem with Key Path


  • Please log in to reply
3 replies to this topic

#1
Netaro

Netaro

    InsanelyMac Protégé

  • Members
  • Pip
  • 32 posts
So, I've began learning how to program in obj-c.

As for now, it goes well, but I'm stuck at the moment. The problem is, i have a NSMutableArray that contains NSNumber. I want to use @avg, @min and like.

My code doesn't work. It shows 0. So, what's the problem here?

srand(time(NULL));
	
	for(int i=0;i<10;i++){
		NSNumber* number = [[NSNumber alloc] initWithInt:(rand()%80)];
		NSLog(@"%d",[number intValue]);
		[array addObject:number];
	}
	
	NSNumber* avg = [array valueForKeyPath:@"@avg"];
	NSLog(@"%s:%d","AverageValue",avg);
	NSNumber* count = [array valueForKeyPath:@"@count.intValue"]; 
	NSLog(@"%s:%d","Count",count);

I've used two different syntaxes for this. First, i tried the count.intValue, and then i tried only avg, without anything (how's that supposed to work, i do not know, it was nothing but wild guessing on my side here. Idiotic, i know.).

So, how it should look like here?

#2
iPoco

iPoco

    InsanelyMac Plumber

  • Supervisors
  • 1,473 posts
  • Gender:Male
  • Location:Canada
You could do a loop and add every object that the array contains. And add then divided by the count to get the average. :s There must be a better way of doing this though.

As for counting NSArray implements count which returns an int with the amount of items in the array. So you could do:

NSInteger count = [array count];

Unless you want an NSNumber:

NSNumber* count = [NSNumber numberWithInt:[array count]];

iPoco

#3
Netaro

Netaro

    InsanelyMac Protégé

  • Members
  • Pip
  • 32 posts
Theoretically, you are right. I can implement these things myself. However, why ram the doors that are already open? I've read about using key paths in the Hillegass's book, he gave an example how to use it, and I tried to use it.

Because in my case the problem is always between the laptop and the chair, it didn't work. And as such, I'm asking, what to do to make it work?

Or, guessing there's another problem between a laptop and a chair, is my thinking correct?

#4
iPoco

iPoco

    InsanelyMac Plumber

  • Supervisors
  • 1,473 posts
  • Gender:Male
  • Location:Canada
This may be something for you to look at: http://developer.app...yOperators.html I've implemented something like this in the past. I had an NSArray populated with NSDictionaries. Each dictionary had a status key. To get the sum of all of the status keys I used:
NSNumber* sum = [myArray valueForKeyPath:@"@sum.status"];
You may want to instead of adding NSNumbers directly to your array either store the info in an NSDictionary (or NSMutableDictionary if it requires editing) or an object such as in Apple's example. This would also allow you to have other keys with the same item. As for counting, looking through Apple's class references a little I would do the following. If you need an NSInteger then use:
NSInteger count = [myArray count];
If you need an NSNumber then use the code below since the value that the method returns is an NSNumber.
NSNumber* count = [myArray valueForKeyPath:@"@count"];
The second option also allows a little more flexibility because you can specify a path but if you require a simple count of the array then the first is just as good.

A final note on the code you posted at first. This
NSNumber* number = [[NSNumber alloc] initWithInt:(rand()%80)];
will result in a memory leak. After you have added number to the array then release it using
[number release];

The Apple memory management documentation here is something that is a must to know and understand. You are right though there is no need to re-invent the wheel with coding. I'm not sure I understand what you mean in the last 2 lines :).

iPoco





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

© 2014 InsanelyMac  |   News  |   Forum  |   Downloads  |   OSx86 Wiki  |   Mac Netbook  |   PHP hosting by CatN  |   Designed by Ed Gain  |   Logo by irfan  |   Privacy Policy