Easier way?


#1

Is there an easier way to set each argument instead of going one by one like this? Like setPurchasePrice/setCurrentPrice/setNumberofShares all on the same line of code? I am sure there is i just can’t find it/do not know what to search for.

    [stockOne setPurchasePrice:2.30];
    [stockOne setCurrentPrice:4.50];
    [stockOne setNumberofShares:40];
    
    [stockTwo setPurchasePrice:12.19];
    [stockTwo setCurrentPrice:10.56];
    [stockTwo setNumberofShares:90];
    
    [stockThree setPurchasePrice:45.10];
    [stockThree setCurrentPrice:49.51];
    [stockThree setNumberofShares:210];

#2

You can define a new method just to do that:

@interface MyStock : NSObject
...
- (void)setPurchasePrice:(float)pc currentPrice:(float)cp numberOfShares:(unsigned)ns;
...
@end

But that would be unorthodox in Objective-C and related worlds.


#3

So ibex10 - are you saying the way dyarbough has done it is the best way to do it?

Personally I prefer it that way - makes code reading easier. Yes a little repetitive but it’s not that many lines of code!


#4

Is there an easier way create multiple variables for this?

So instead of the below :
StocksHolding *stock1 = [[StocksHolding alloc] init];
StocksHolding *stock2 = [[StocksHolding alloc] init];
StocksHolding *stock3 = [[StocksHolding alloc] init];

I was thinking of:

StocksHolding *stock1, *stock2, *stock3 = [[StocksHolding alloc] init];

However stock1 and stock2 does not get initialize and only stock3 is working after some testing. Can anyone give a reason why as to me its the same thing as declaring multiple variables with type int, string and etc…
Does this mean we can we can only use (allocation and init) per variable? Is there no other ways?


#5

I would stick with this:

StocksHolding *stock1 = [[StocksHolding alloc] init];
StocksHolding *stock2 = [[StocksHolding alloc] init];
StocksHolding *stock3 = [[StocksHolding alloc] init];
...

But if you find yourself in a situation in which you need to create a large number of them, then you can use an array to store the instances rather than declaring them one by one.

NSMutableArray *stocks = [NSMutable array];
for (long x = 0; x < NS; ++x) {
      [stocks addObject:[[StocksHolding alloc] init]];
}

#6

[quote=“kentzhu”]Is there an easier way create multiple variables for this?
StocksHolding *stock1, *stock2, *stock3 = [[StocksHolding alloc] init];
However stock1 and stock2 does not get initialize and only stock3 is working after some testing. Can anyone give a reason why as to me its the same thing as declaring multiple variables with type int, string and etc…[/quote]

1/ *stock1 has to be considered as a variable or a pointer ?
Well, [[StocksHolding alloc] init] returns the address of the pointer of the instance of an object and not the value of a variable, yes ? no ? …
2/ The way you declare the pointers looks fine to me StocksHolding *stock1, *stock2, *stock3;
3/ The question is : will the message [[StocksHolding alloc] init] create and return one instance and so one pointer, or multiple instances and pointers on demand ?
Looks like *stock1 is created but override by *stock2, which is created and override by *stock3 without producing any errors or throwing any exceptions (thanks to the @autoreleasepool?!)

:unamused: I think so. (but I was wrong, see below)


#7

[quote=“kentzhu”]
Does this mean we can we can only use (allocation and init) per variable? Is there no other ways?[/quote]
Collection classes is the other way !
Have a look for it at chapters 19 & 21