search

Home  >  Q&A  >  body text

Objective-c - Why is the result of array traversal operation in OC a string of addresses?

Header file

#import "LSTStockHolding.h"

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

main function

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;

The running results are as shown in the figure
2016-09-08 15:36:20.988 Chapter 20 [863:97992] The cid is:24.00, the vid is:30.00.
2016-09-08 15:36 :20.990 Chapter 20[863:97992] Here is a list:<LSTStockHolding: 0x1001055d0>
Program ended with exit code: 0

I want to enumerate the objects of the array list and output the value of the LSTStockHolding object by traversing the array. How should I do this? The current running result is that an address is returned. . Beginners don’t quite understand

过去多啦不再A梦过去多啦不再A梦2852 days ago512

reply all(1)I'll reply

  • 怪我咯

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

    Because what is printed is a pointer, if you want to print more detailed information
    you can rewrite the LSTStockHolding 类的description method, as shown below
    tutorial

    - (NSString *)description{
        
    }

    reply
    0
  • Cancelreply