【问题标题】:understanding simple collection operators了解简单的集合运算符
【发布时间】:2012-04-20 15:02:55
【问题描述】:

我正在阅读 Apple 的 Key Value Programming Guide。 在主题简单集合运算符中,我读到:

以下示例返回交易中对象的交易金额平均值: NSNumber *transactionAverage=[transactions valueForKeyPath:"@avg.amount"];

我尝试编写一个可以使用上述内容的程序。以下是该程序的文件。我无法理解为什么上述方法对我不起作用。

事务.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

事务.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;
  }

【问题讨论】:

    标签: objective-c kvc


    【解决方案1】:

    我想你想要 NSNumber *avgT = [db1 valueForKeyPath:@"@avg.amount"];您希望对数组中的所有“数量”值进行平均,而不仅仅是在 t1 对象中——对一个值进行平均是没有意义的。

    【讨论】:

    • “如何让它工作”是什么意思?如果它现在不起作用,发生了什么?你有错误吗?
    • 查看我编辑的答案——关键路径是一个字符串,所以你仍然需要开头的 @ 以及前面的 avg。
    • NSNumber *avgT = [db1 valueForKeyPath: @"@avg.amount"];给出错误:由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ valueForUndefinedKey:]:此类与键 @avg 的键值编码不兼容。” *** 第一次抛出调用堆栈:( 0 CoreFoundation
    • 我不确定您的 TransactionDatabase 发生了什么,但这就是问题所在 - 如果您将 t1、t2 和 t3 添加到 NSMutable 数组中,它可以正常工作。
    • 我不知道你为什么定义 TransactionDatabase 类,它没有做任何你不能用 NSMutableArray 做的事情。通过使 TransactionDataBase 成为 NSArray 的子类并实现 count 和 objectAtIndex: 方法,我确实让你的代码工作了,但是为什么要麻烦呢?
    【解决方案2】:

    您的TransactionDataBase 类必须支持一个键路径,该路径返回一个包含它存储的值的数组。此外,Transaction 类必须支持键 @"amount" 的键值编码。您必须为这些类添加适当的属性。

    然后,您在 [db1 valueForKeyPath: "t1.@avg.amount"] 中使用的键路径必须考虑这些属性的名称,即使用您当前的代码,TransactionDataBase 中的项目数组必须可以通过键 @"t1" 访问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 2023-02-24
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多