如何在子类中使用main.m中的数据

时间:2014-03-25 13:13:10

标签: objective-c cocoa

我正在从“大书呆子牧场指南”一书中做作业。在这个问题中,我在main.m中有两个数组。一个来自子类,另一个来自超类。 BNRItem是超类。 BNRContainer是子类。在实现文件中,我有描述字符串,说明NSLog应该说什么。我需要从main.m数组中使用的超类中获取数据,并在子类中使用它,以便子类数组可以工作。有一个简单的答案吗? 在main.m中,我有以下内容:

//  main.m
//  RandomItems

#import <Foundation/Foundation.h>
#import "BNRItem.h"
#import "BNRContainer.h"

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

        NSMutableArray *items = [[NSMutableArray alloc] init];

        for (int i = 0; i < 3; i++) {
            BNRItem *item = [BNRItem randomItem];
            [items addObject:item];
        }

        for (BNRItem *item in items) {
            NSLog(@"%@", item);
        }

        NSMutableArray *containers = [[NSMutableArray alloc] init];

        for (int i = 0; i < 3; i++) {
            BNRContainer *container = [BNRContainer randomContainer];
            [containers addObject:container];
        }

        for (BNRContainer *container in containers) {
            NSLog(@"%@", container);
        }

        items = nil;
        containers = nil;
    }
    return 0;
}

在BNRItem.h中:

//  BNRItem.h
//  RandomItems
//
//  Created by Meghan on 3/19/14.
//  Copyright (c) 2014 Meghan. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BNRItem : NSObject
{
    NSString *_itemName;
    NSString *_serialNumber;
    int _valueInDollars;
    NSDate *_dateCreated;
}

+ (instancetype)randomItem;

//Designated initializer for BNRItem
- (instancetype)initWithItemName:(NSString *)name
                  valueInDollars:(int)value
                    serialNumber:(NSString *)sNumber;

-(instancetype)initWithItemName:(NSString *)name;

- (NSString *)description;


- (void)setItemName:(NSString *)str;
-( NSString *)itemName;

- (void)setSerialNumber:(NSString *)str;
- (NSString *)serialNumber;

- (void)setValueInDollars:(int)v;
- (int)valueInDollars;

- (NSDate *)dateCreated;
@end

在BNRContainer.h中:

//  BNRContainer.h
//  RandomItems
//
//  Created by Meghan on 3/20/14.
//  Copyright (c) 2014 Meghan. All rights reserved.
//

#import "BNRItem.h"

@interface BNRContainer : BNRItem
{
    NSString *_containerName;
    int _containerItemsSum;
    int _containerValue;
    int _totalContainerValue;
}

- (NSString *)description;

+ (instancetype)randomContainer;

//Designated initializer for BNRContainer
- (instancetype)initWithContainerName:(NSString *)name
                  containerItemsSum:(int)iSum
                    containerValue:(int)value
                totalContainerValue:(int)tvalue;

- (instancetype)initWithContainerName:(NSString *)name;

- (instancetype)init;

- (void)setContainerName:(NSString *)str;
- (NSString *)containerName;
- (void)setContainerItemsSum:(int)v;
- (int)containerItemsSum;
- (void)setContainerValue:(int)v;
- (int)containerValue;
- (void)setTotalContainerValue:(int)v;
- (int)totalContainerValue;


@end

BNRContainer中的变量containerItemsSum应该是几个项的valueInDollars的总和,并且当它循环时,该值在main.m中生成。

//  BNRItem.m
//  RandomItems
//
//  Created by Meghan on 3/19/14.
//  Copyright (c) 2014 Meghan. All rights reserved.
//

#import "BNRItem.h"

@implementation BNRItem

- (instancetype)initWithItemName:(NSString *)name
                  valueInDollars:(int)value
                    serialNumber:(NSString *)sNumber
{
    self = [super init];

    if (self) {
        //Give the instance variables initial values
        _itemName = name;
        _serialNumber = sNumber;
        _valueInDollars = value;
        //Set dateCreated to the current date and time
        _dateCreated = [[NSDate alloc]init];
    }
                        return self;
}

-(instancetype)initWithItemName:(NSString *)name
{
    return [self initWithItemName:name
                   valueInDollars:0
                     serialNumber:@""];
}

- (instancetype)init
{
    return [self initWithItemName:@"Item"];
}

+ (instancetype)randomItem
{
    NSArray *randomAdjectiveList = @[@"Fluffy", @"Rusty", @"Shiny"];
    NSArray *randomNounList = @[@"Bear", @"Spork", @"Mac"];
    NSInteger adjectiveIndex = arc4random() % [randomAdjectiveList count];
    NSInteger nounIndex = arc4random() % [randomNounList count];

    NSString *randomName = [NSString stringWithFormat:@"%@ %@",
                            randomAdjectiveList[adjectiveIndex],
                            randomNounList[nounIndex]];
    int randomValue = arc4random() % 100;
    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                    '0' + arc4random() % 10,
                                    'A' + arc4random() % 26,
                                    '0' + arc4random() % 10,
                                    'A' + arc4random() % 26,
                                    '0' + arc4random() % 10];
    BNRItem *newItem = [[self alloc] initWithItemName:randomName
                                       valueInDollars:randomValue
                                         serialNumber:randomSerialNumber];
    return newItem;
}


- (void)setItemName:(NSString *)str
{
    _itemName = str;
}
- (NSString *)itemName
{
    return _itemName;
}

- (void)setSerialNumber:(NSString *)str
{
    _serialNumber = str;
}

- (NSString *)serialNumber
{
    return _serialNumber;
}

- (void)setValueInDollars:(int)v
{
    _valueInDollars = v;
}

- (int)valueInDollars
{
    return _valueInDollars;
}

- (NSDate *)dateCreated
{
    return _dateCreated;
}



- (NSString *)description
{
    NSString *descriptionString =
    [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
        self.itemName,
        self.serialNumber,
        self.valueInDollars,
        self.dateCreated];
    return descriptionString;
}

@end

1 个答案:

答案 0 :(得分:1)

for (BNRItem *item in items) {
        NSLog(@"%@", item);
    }

在这里,您通过了您创建的每个BNRItem。这是您收集有关物品信息的机会。

创建一个局部变量来存储总数,并将每个项目添加到其中:

int dollarTotal = 0;    
for (BNRItem *item in items) {
    NSLog(@"%@", item);
    dollarTotal += [item valueInDollars];
}
NSLog(@"Total value in dollars is %d",dollarTotal);

然后您使用此值来设置容器的总数,尽管这看起来完全落后了。 BNRContainer应该(尽管这可能是您正在做的教程中的后一点)能够使用类似于上面的循环从其自己的包含项集合中导出此总计。我也不明白你为什么要在循环中创建三个容器。这个例子没有多大意义。