NSLog数组描述,而不是内存地址

时间:2014-01-04 18:17:41

标签: ios objective-c logging nsarray

我想查看我的数组的内容,而不是内存地址。我做错了什么?或者我必须一直使用[myArray objectAtIndex:i]

//
//  main.m
//  Stocks
//
//  Created by Vladyslav Semenchenko on 1/3/14.
//  Copyright (c) 2014 Vladyslav Semenchenko. All rights reserved.
//

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

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

    @autoreleasepool {

        // insert code here...
        NSLog(@"Hello, World!");
        BNRStockHolding *first = [[BNRStockHolding alloc] init];
        BNRStockHolding *second = [[BNRStockHolding alloc] init];
        BNRStockHolding *fourth = [[BNRStockHolding alloc] init];
        BNRStockHolding *fifth = [[BNRStockHolding alloc] init];
        BNRForeignStockHolding *third = [[BNRForeignStockHolding alloc] init];

        [first setPurchaseSharePrice:33.3];
        [first setCurrentSharePrice:28.9];
        [first setNumberOfShares:20];
        [first setSymbol:@"AAC"];

        [second setPurchaseSharePrice:23];
        [second setCurrentSharePrice:1];
        [second setNumberOfShares:34];
        [second setSymbol:@"DVF"];

        [fourth setPurchaseSharePrice:33.3];
        [fourth setCurrentSharePrice:45];
        [fourth setNumberOfShares:20];
        [fourth setSymbol:@"RFT"];

        [fifth setPurchaseSharePrice:23];
        [fifth setCurrentSharePrice:1];
        [fifth setNumberOfShares:34];
        [fifth setSymbol:@"VCX"];

        //second.convrsionRate = 0.3;

        [third setPurchaseSharePrice:34];
        [third setCurrentSharePrice:56];
        [third setNumberOfShares:46];
        third.convrsionRate = 1.3;

        NSArray *stocks = @[first, second, third];

        for (BNRStockHolding *lstock in stocks)
        {
            NSLog(@"Stock cost in dollars = %f, value in dollars = %f", [lstock costInDollars], [lstock valueInDollars]);
        }

        BNRPortfolio *myPortfolio = [[BNRPortfolio alloc] init];
        [myPortfolio addHolding:first];
        [myPortfolio addHolding:second];
        [myPortfolio addHolding:fourth];
        [myPortfolio addHolding:fifth];

        [myPortfolio topThree];
        [myPortfolio sortBySymbol];
        NSLog(@"Stocks is %@", stocks);

        NSLog(@"Total holding value is %f", [myPortfolio totalValue]);

        NSLog(@"Holding array is %@", myPortfolio.holding);

        NSArray *secondPackOfStocks = @[first];
        [myPortfolio setHolding:secondPackOfStocks];
        NSLog(@"Holding array is %@", myPortfolio.holding);

    }
    return 0;
}

我得到的是:

2014-01-04 20:09:58.852 Stocks[1697:303] Hello, World!
2014-01-04 20:09:58.854 Stocks[1697:303] Stock cost in dollars = 666.000000, value in dollars = 578.000000
2014-01-04 20:09:58.854 Stocks[1697:303] Stock cost in dollars = 782.000000, value in dollars = 34.000000
2014-01-04 20:09:58.854 Stocks[1697:303] Stock cost in dollars = 2033.199951, value in dollars = 3348.799805
2014-01-04 20:09:58.854 Stocks[1697:303] Sorted array is (
    "<BNRStockHolding: 0x10010c3c0>",
    "<BNRStockHolding: 0x10010b8a0>",
    "<BNRStockHolding: 0x100103040>"
)
2014-01-04 20:09:58.855 Stocks[1697:303] Stocks is (
    "<BNRStockHolding: 0x10010b8a0>",
    "<BNRStockHolding: 0x100103040>",
    "<BNRForeignStockHolding: 0x10010c940>"
)
2014-01-04 20:09:58.855 Stocks[1697:303] Total holding value is 1546.000000
2014-01-04 20:09:58.856 Stocks[1697:303] Holding array is (
    "<BNRStockHolding: 0x10010b8a0>",
    "<BNRStockHolding: 0x100103040>",
    "<BNRStockHolding: 0x10010c3c0>",
    "<BNRStockHolding: 0x100108fa0>"
)
2014-01-04 20:09:58.856 Stocks[1697:303] Holding array is (
    "<BNRStockHolding: 0x10010b8a0>"
)
Program ended with exit code: 0

P.S。抱歉新手问题,但我真的不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:4)

您应该覆盖description类的BNRStockHolding方法以返回相应的字符串。例如。

- (NSString *)description{
    NSString *description = [NSString stringWithFormat:@"Purchase Date : %@  CurrentSharePrice : %@",self.purchaseSharePrice,self.currentSharePrice];
    return description;
}