Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:28:57+00:00 2026-06-02T06:28:57+00:00

I am reading Apple’s Key Value Programming Guide. In the topic, Simple Collection Operators,

  • 0

I am reading Apple’s Key Value Programming Guide.
In the topic, Simple Collection Operators, I read:

The following example returns the average value of the transaction amount for the objects in transactions:
NSNumber *transactionAverage=[transactions valueForKeyPath:”@avg.amount”];

I attempt to write a program in which I can use the above. Below are the files of that program. I am unable to understand why the above does not work for me.

Transaction.h

    #import <Foundation/Foundation.h>

@interface Transaction : NSObject
{
NSString *payee;
NSNumber *amount;
NSDate *date;
}
- (void) setPayee: (NSString *) aP 
    setAmount: (NSNumber *) aN
      setDate: (NSDate *) aD;

@end

Transaction.m
#import “Transaction.h”

@implementation Transaction
- (void) setPayee: (NSString *) aP 
    setAmount: (NSNumber *) aN
      setDate: (NSDate *) aD 
{
payee = aP;
amount = aN;
date = aD;
} 
@end

TransactionDataBase.h

#import <Foundation/Foundation.h>
#import "Transaction.h"
@interface TransactionDataBase : NSObject
{
NSMutableArray *tArray;
}
  - (id) objectInTArrayAtIndex: (NSUInteger) index;
- (void) insertObject: (Transaction *) tA
inTArrayAtIndex: (NSUInteger) index;
  - (NSUInteger) countOfTArray;
  @end

TransactionDataBase.m

     #import "TransactionDataBase.h"

   @implementation TransactionDataBase
  - (id) init
  {
self = [super init];
if (self) {
    tArray = [[NSMutableArray alloc] initWithCapacity:40];
   }
    return self;
  }

    - (id) objectInTArrayAtIndex: (NSUInteger) index
  {
  return [tArray objectAtIndex:index];
  }

  - (void) insertObject: (Transaction *) tA
    inTArrayAtIndex: (NSUInteger) index
 {
[tArray insertObject:tA atIndex:index];
   }

 - (NSUInteger) countOfTArray
  {
return [tArray count];
    }
   @end


  main.m:


  #import <Foundation/Foundation.h>
    #import "Transaction.h"
   #import "TransactionDataBase.h"

    int main(int argc, const char * argv[])
   {

      @autoreleasepool {

    Transaction *t1 = [[Transaction alloc] init];
    Transaction *t2 = [[Transaction alloc] init];
    Transaction *t3 = [[Transaction alloc] init];

    NSDate *nowDate = [[NSDate alloc]  initWithString:@"2.4.2012"];
    NSDate *nowDate2 = [[NSDate alloc] initWithString:@"2.4.2012"];
    [t1 setPayee:@"c" setAmount:[NSNumber numberWithInt:3000] setDate:nowDate];

    [t2 setPayee:@"c" setAmount:[NSNumber numberWithInt:5000] setDate:nowDate2];


    [t3 setPayee:@"y" setAmount:[NSNumber numberWithInt:1000] setDate:nowDate2];

    TransactionDataBase *db1 = [[TransactionDataBase alloc] init];
    [db1 insertObject:t1 inTArrayAtIndex:0];
    [db1 insertObject:t2 inTArrayAtIndex:1];
    [db1 insertObject:t3 inTArrayAtIndex:2];
    NSNumber *avgT = [db1 valueForKeyPath: "@avg.amount"];
    NSLog(@"%@", avgT);


}
return 0;
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:28:59+00:00Added an answer on June 2, 2026 at 6:28 am

    I think you want NSNumber *avgT = [db1 valueForKeyPath:@”@avg.amount”]; You want to average over all the “amount” values in your array, not just in the t1 object — it doesn’t make sense to average over one value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading Apple's document Concurrent Programming Guide, and I think of that OperationQueue is
I was reading on a blog that there's a GLImageProcessing example from apple. I
I'v been reading through the Apple Docs, read through a couple tutorials online, and
After reading apple's example: MultipleDetailViews which demonstrates how to use UIPopOverController with two detail
I'm reading the following about UIScrollView from Apple UIScrollView Class Reference Documentation: Because a
I was reading Apple's documentation, The Objective-C Programming Language (PDF link). On pg. 18,
When I'm reading the apple iOS Developer Guide, it explains me a bit about
From reading the Apple Docs on Core Data, I've learned that you should not
I was reading the apple documentation for memory management , and came across something
I was reading at an apple site that shark captures everything that is running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.