NSArray& NSMutableArray混淆

时间:2014-05-13 12:33:37

标签: objective-c oop nsmutablearray nsarray

我遇到了一些书的挑战。

我在h.file中创建了一个名为“holdings”的NSArray属性,并在m.file中的类扩展名中创建了名为“_holdings”的NSMutableArray实例。 这是因为不允许其他对象访问数组。

当我编译它时,出现了错误信息,并说“....- [__ NSArrayI addObject:]:无法识别的选择器发送到实例/ Users / Toshikiinami / Desktop / Screen Shot 2014-05-13 at 21.16。 09.png0x10010b7b0 ...“enter image description here

我将_holdings设置为NSMutableArray,但是这个错误是“_holdings是一个NSArray实例”,即addObject:方法不起作用。

我感谢任何帮助:)谢谢!

BNRPortfolio.h

#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"

@interface BNRPortfolio : NSObject


@property(nonatomic, copy) NSArray *holdings;


- (float)totalValue;
- (void)addStock:(BNRStockHolding *)stockHolding;
- (void)removeStock:(unsigned int) i;




@end

BNRPortfolio.m

#import "BNRPortfolio.h"
#import "BNRStockHolding.h"

@interface BNRPortfolio()
{
    NSMutableArray *_holdings;

}
@end

@implementation BNRPortfolio

- (void)holdings:(NSArray *)a
{
    _holdings = [a mutableCopy];
}

- (NSArray *)setHoldings
{

    return [_holdings copy];

}

- (float)totalValue
{
    float total = 0;

    for (int i = 0; i < 3; i++)
    {
        float currentValue = [_holdings[i] valueInDollars];
        total += currentValue;
    }

    return total;
}

- (void)addStock:(BNRStockHolding *)stockHolding
{
    if (!_holdings)
    {
        _holdings = [[NSMutableArray alloc] init];
    }

    [_holdings addObject:stockHolding];


}

- (void)removeStock:(unsigned int)i
{
    if (_holdings)
    {
        [_holdings removeObjectAtIndex:i];

    }

}

@end

的main.m

#import <Foundation/Foundation.h>
#import "BNRStockHolding.h"
#import "BNRForeignStockHolding.h"
#import "BNRPortfolio.h"


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

    @autoreleasepool {

        BNRStockHolding *stock1 = [[BNRStockHolding alloc]init];
        BNRStockHolding *stock2 = [[BNRStockHolding alloc] init];
        BNRForeignStockHolding *stock3 = [[BNRForeignStockHolding alloc] init];

        stock1.purchaseSharePrice = 2.30;
        stock1.currentSharePrice = 4.50;
        stock1.numberOfShares = 40;
        stock1.symbol = @"XYZ";

        stock2.purchaseSharePrice = 12.19;
        stock2.currentSharePrice = 10.56;
        stock2.numberOfShares = 90;
        stock2.symbol = @"ABC";

        stock3.purchaseSharePrice = 45.10;
        stock3.currentSharePrice = 49.51;
        stock3.numberOfShares = 210;
        stock3.conversionRate = 0.94;
        stock3.symbol = @"LMN";


        NSMutableArray *mutable = [NSMutableArray array];
        [mutable addObject:stock1];
        [mutable addObject:stock2];
        [mutable addObject:stock3];

        BNRPortfolio *portfolio = [[BNRPortfolio alloc] init];
        portfolio.holdings = [mutable copy];

        float totalAmount = [portfolio totalValue];

        NSLog(@"total amount is %f",totalAmount);


//        for (BNRStockHolding *n in mutable)
//        {
//            NSLog(@"%f",[n costInDollars]);
//            NSLog(@"%f", [n valueInDollars]);
//        }


/*

        for (int i = 0; i < [mutable count]; i++ )
        {
            NSLog(@" %d 's costInDollars = %.1f",i,[mutable[i] costInDollars]);
            NSLog(@" %d 's valueInDollars = %.1f",i,[mutable[i] valueInDollars]);

        }

 */
       BNRStockHolding *stock4 = [[BNRStockHolding alloc] init];

        stock4.purchaseSharePrice = 42.10;
        stock4.currentSharePrice = 22.51;
        stock4.numberOfShares = 230;
        stock4.symbol = @"LMN";


        [portfolio addStock:stock4];

        NSLog(@"count portfoio %ld",[portfolio.holdings count]);

    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

我没有直接了解您的代码无法正常工作的原因,因为您发布的代码中没有[NSArray alloc],因此它必须位于其他位置。

- (void)holdings:(NSArray *)a
{
    _holdings = [a mutableCopy];
}

- (NSArray *)setHoldings
{

    return [_holdings copy];

}

这些确实需要改为

- (void)setHoldings:(NSArray *)a
{
    _holdings = [a mutableCopy];
}

- (NSArray *)holdings
{
    return [_holdings copy];
}

我不知道代码portfolio.holdings = [mutable copy];

会发生什么

它会调用[portfolio setHoldings:[mutable copy]],但您目前没有带有参数的名为setHoldings的方法。由于点语法,我怀疑它只是调用[portfolio setHoldings]并将_holdings数组的副本设置为值[mutable copy],这样就不会因为数组返回而无效副本。