Jump to content
Sign in to follow this  

A small problem with Key Path

4 posts in this topic

Recommended 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?



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"];
NSNumber* count = [array valueForKeyPath:@"@count.intValue"]; 


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?

Share this post

Link to post
Share on other sites

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]];



Share this post

Link to post
Share on other sites

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?

Share this post

Link to post
Share on other sites

This may be something for you to look at: http://developer.apple.com/mac/library/doc...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 :).



Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.