问题描述
头文件
#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-09-08 15:36:20.990 20章[863:97992] Here is a list:<LSTStockHolding: 0x1001055d0>Program ended with exit code: 0
我想通过遍历数组的操作,枚举数组list的对象并且输出LSTStockHolding对象的值,该如何操作呢?目前运行结果是返回了一个地址。。初学者不太懂
问题解答
回答1:因为打印的是指针,如果你想要打印更详细的信息你可以重写LSTStockHolding 类的description方法,如下教程
- (NSString *)description{ }