Use "for" to assignment objects value from the same class


#1

stockholding.h

[code]#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
float purchaseSharePrice;
float currentSharePrice;
int numberOfShares;
}

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

-(float)costInDollars;
-(float)valueInDollars;

@end[/code]

stockholding.m

[code]#import “StockHolding.h”

@implementation StockHolding

@synthesize purchaseSharePrice,currentSharePrice,numberOfShares;

-(float)costInDollars
{
return purchaseSharePricenumberOfShares;
}
-(float)valueInDollars
{
return currentSharePrice
numberOfShares;
}

@end[/code]

main.m

[code]#import <Foundation/Foundation.h>
#import “StockHolding.h”

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

@autoreleasepool {
    //creat an array numString to store all the numbers
    NSString *numString = @"2.30|4.50|40|12.19|10.56|90|45.10|49.51|210";
    NSArray *numArray = [[NSMutableArray alloc]init];
    numArray = [numString componentsSeparatedByString:@"|"];
    

    //assignment stocks value
    NSMutableArray *stocks = [[NSMutableArray alloc]init];
    
    for (int i = 0; i<9;i=i+3) {
        StockHolding *tempStock = [[StockHolding alloc]init];
        [tempStock setPurchaseSharePrice:[[numArray objectAtIndex:i] floatValue]];
        [tempStock setCurrentSharePrice:[[numArray objectAtIndex:i+1] floatValue]];
        [tempStock setNumberOfShares:[[numArray objectAtIndex:i+2] intValue]];
        [stocks addObject:tempStock];
    }
    
    
    //output
    for (StockHolding *a in stocks ) {
    NSLog(@"purchaseprices:%f,currentprices:%f,numberofshares:%d.cost in dollars:%f,value in dollars:%f",
              [a purchaseSharePrice],[a currentSharePrice],[a numberOfShares],[a costInDollars],[a valueInDollars]);
    }
    
}
return 0;

}[/code]

take me three hours to dig documents…