从模型类返回对象

时间:2013-02-26 21:53:17

标签: objective-c object nsmutabledictionary

我希望我不要在这里刮胡子! 我尝试在我的模型类中调用一个方法,并将一个对象返回给调用控制器。

我的AppDelegate.h:

#import <Cocoa/Cocoa.h>
@class ResultItem;

@interface AppDelegate : NSObject <NSApplicationDelegate>{
    ResultItem *ri;
}
-(void)calculateResults;
@end

我的AppDelegate.m:

#import "AppDelegate.h"
#import "ResultItem.h"

@implementation AppDelegate
    //other stuff

- (void)calculateResults{
    self->ri = [[ResultItem alloc]init];         //ResultItem is my model
    [xx calculateUniformDistribution:(double)x1
            withxMinus:(double)x2
            withxAvg:(double)x3
            withCount:(double)numberCounter];
} ...

我的ResultItem.h

#import <Foundation/Foundation.h>
@interface ResultItem : NSObject

-(NSMutableDictionary *)calculateUniformDistribution:(double)xPlus
        withxMinus:(double)xMinus
        withxAvg:(double) xAvg
        withCount:(int) count;

我的ResultItem.m

#import "ResultItem.h"
@implementation ResultItem

- (NSMutableDictionary *)calculateUniformDistribution:(double)xPlus
        withxMinus:(double)xMinus
        withxAvg:(double)xAvg
        withCount:(int)count{

NSMutableArray *dimensionRandomArray = [[NSMutableArray alloc] init];
NSMutableDictionary *newResultItem = [[NSMutableDictionary alloc]init];

//calculation loop

[newResultItem setValue:[NSNumber numberWithDouble:max] forKey:@"maxValue"];
[newResultItem setValue:[NSNumber numberWithDouble:min] forKey:@"minValue"];
[newResultItem setValue:[NSNumber numberWithDouble:avg] forKey:@"averageValue"];
[newResultItem setValue:dimensionRandomArray forKey:@"randomValueArray"];

return newResultItem;
}

ResultItem方法被称为罚款。我想创建一个NSMutableDictionary对象,稍后我需要在AppDelegate中处理它。

我的问题:

  1. 之后如何在newResultItem方法中访问AppDelegate
  2. 我不知道如何使用newResultItem方法AppDelegate中的calculateResults添加更多值。有人可以帮帮我吗?
  3. 谁实际上是newResultItem对象ResultItemAppDelegate的所有者?
  4. 设置这样的代码看起来不对。我尝试尽可能地遵循MVC模式。
  5. 请不要打我...

2 个答案:

答案 0 :(得分:1)

理想情况下,您的ResultsItem应该只封装属性而不包含计算方法。这是一个简单的例子(添加更多值,你可以使用这样的字典):

   @implementation  ResultItem  
   @synthesize max, min, ...;     
   - (id)initWithDictionary:(NSDictionary*)dictionary {
      if ((self = [super init])) {
          self.max = [dictionary objectForKey:@"maxValue"];
          self.min = [dictionary objectForKey:@"minValue"];
          ......
                     }
      return self; 
    }
    @end

接下来我将创建一个Calculation类,您可以在其中执行所有计算方法,像这样创建ResultItem对象

ResultItem *resultItem = [[ResultItem alloc] initWithDictionary:yourDictionary];

并将此对象返回到AppDelegate。

答案 1 :(得分:0)

首先,我建议删除ResultItem类。我会用控制器类中的方法替换它 - 在您的情况下,将calculateUniformDistribution方法移动到AppDelegate。或者至少将calculateUniformDistribution移动到一个合适的Model类(你的app应用于汽车,公共汽车,飞机等的实际对象)。

关于实际返回字典和添加值,这里有一个简化的例子,假设你坚持你当前的模式:

在AppDelegate.m中

self.ri = [[ResultItem alloc] init];
NSMutableDictionary* dict = [self.ri calculateUniformDistribution:(double)x1
        withxMinus:(double)x2
        withxAvg:(double)x3
        withCount:(double)numberCounter];
[dict setValue:[NSNumber numberWithDouble:1.0] forKey:@"additionalKey"];

在ResultItem.m

NSMutableArray* dimensionRandomArray = [[NSMutableArray alloc] init];
NSMutableDictionary* returningDict = [[NSMutableDictionary alloc] init] autorelease];

//calculation loop

[newResultItem setValue:[NSNumber numberWithDouble:max] forKey:@"maxValue"];
[newResultItem setValue:[NSNumber numberWithDouble:min] forKey:@"minValue"];
[newResultItem setValue:[NSNumber numberWithDouble:avg] forKey:@"averageValue"];
[newResultItem setValue:dimensionRandomArray forKey:@"randomValueArray"];
[dimensionRandomArray release];

return newResultItem;

注意returnsDict的自动释放和dimensionRandomArray的发布。此外,您必须在之后释放self.ri。如果你想在AppDelegate中保留dict,你必须添加retain-release。