搜尋

首頁  >  問答  >  主體

objective-c - OC中遍歷陣列操作的結果為何是一串位址?

頭檔

#import "LSTStockHolding.h"

@implementation LSTStockHolding
-(float)costInDollars
{
    float c = [self purchaseSharePrice];
    return c*[self numberOfShares];
}
-(float)valueInDollars
{
    return [self currentSharePrice]*[self numberOfShares];
    

main函數

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        LSTStockHolding *China = [[LSTStockHolding alloc]init];
        China.purchaseSharePrice = 4.0;
        China.currentSharePrice = 5.0;
        China.numberOfShares = 6.0;
        float cid =[China costInDollars];
        float vid =[China valueInDollars];
        NSLog(@"The cid is:%.2f,the vid is:%.2f.",cid,vid);
        NSArray *list = @[China];
        for(NSString *s in list)
        {
            NSLog(@"Here is a list:%@",s);
        }
    }
    return 0;

運行結果如圖
2016-09-08 15:36:20.988 20章[863:97992] The cid is:24.00,the vid is:30.00.
2016-24.00,the vid is:30.00.
2016-09-08 15:396963:3966-09-6863:3966990963:39663:39663:396963:39669:39669:396963:396969:3969393993999399399393933位[3933999939分話[ :97992] Here is a list:

Program ended with exit code: 0

我想透過遍歷數組的操作,枚舉數組list的物件並且輸出LSTStockHolding物件的值,該如何操作呢?目前運行結果是回傳了一個位址。 。初學者不太懂🎜
过去多啦不再A梦过去多啦不再A梦2757 天前497

全部回覆(1)我來回復

  • 怪我咯

    怪我咯2017-05-02 09:31:55

    因為印刷的是指針,如果你想要打印更詳細的信息
    你可以重寫LSTStockHolding 类的description方法,如下
    教程

    - (NSString *)description{
        
    }

    回覆
    0
  • 取消回覆