I have a rather small but annoying problem. I think I completed the challenge the way you were supposed to but I also wanted to add the name of the company when printing out the stock price etc. in my for loop. I tried doing this by using the ‘@’-token (%@) but when it gets logged to the console it shows up as a hexadecimal number. If anyone could help me with this problem I would be very thankful!
BNRStockHolding.h
[code]#import <Foundation/Foundation.h>
@interface BNRStockHolding : NSObject
{
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;
}
int main(int argc, const char * argv) { @autoreleasepool {
BNRStockHolding *apple = [[BNRStockHolding alloc] init];
[apple setPurchaseSharePrice:421.54];
[apple setCurrentSharePrice:411.32];
[apple setNumberOfShares:21];
BNRStockHolding *google = [[BNRStockHolding alloc] init];
[google setPurchaseSharePrice:234.56];
[google setCurrentSharePrice:132.76];
[google setNumberOfShares:20];
BNRStockHolding *yahoo = [[BNRStockHolding alloc] init];
[yahoo setPurchaseSharePrice:231.45];
[yahoo setCurrentSharePrice:542.34];
[yahoo setNumberOfShares:21];
NSArray *companyStocks = [[NSArray alloc] initWithObjects:apple, google, yahoo, nil];
int i = 0;
for (BNRStockHolding *s in companyStocks) {
NSLog(@"The company %@'s stock currently has a purchaseprice of $%.2f and have a current share price of $%.2f. You have %d shares in it, and the stock costs $%.2f and it's valued at $%.2f", s, [s purchaseSharePrice], [s currentSharePrice], [s numberOfShares], [s costInDollars], [s valueInDollars]);
i++;
}
NSLog(@"You have stock(s) from %d comapnies", i);
return 0;
}
To be able to print instances of BNRStockHolding by using the ‘%@’ directive in NSLog’s format string,
add the description method to BNRStockHolding class:
Thank you for your answer, but I am afraid I don’t really understand what you mean; How would it work? I declared the method in the .h-file but I’m not sure about the implementation. What kind of message is the “stringWith:” and what would it do? I couldn’t find it as a sendable message. Also, how would this look when you use it in the main.m? I can’t really wrap my head around it. Thank you for your time.
EDT: I solved it! I found this (https://mobiarch.wordpress.com/2013/09/09/writing-a-custom-property-getter-and-setter-in-objective-c/) article which gave me some inspiration I interpreted what ibex10 wrote above as if the whole NSString-thing only were supposed to be declared in a method (and maybe it’s possible to do that as well), but I thought about how it would work if the NSString had getter- and setter-methods and if that maybe could work, so I declared the following in the .h-file and implemented it in the .m-file, just as all the other functions have been declared and implemented in this chapter:
BNRStockHolding.h
[code]#import <Foundation/Foundation.h>
@interface BNRStockHolding : NSObject
{
float _purchaseSharePrice;
float _currentSharePrice;
int _numberOfShares;
NSString *_name;
}
[…]
(NSString *)name;
(void)setName:(NSString *)m;
[…]
@end
[/code]
BNRStockHolding.m
[code]#import “BNRStockHolding.h”
@implementation BNRStockHolding
[…]
(NSString *)name
{
return _name;
}
(void)setName:(NSString *)m
{
_name = m;
}
@end[/code]
I don’t know if what I wrote above is what you were thinking of ibex10, but if you have another solution I would be very glad to learn it!
Oh thanks! That makes a lot of more sense! There’s still a few things that I don’t understand about it though:
I read that the stringWithFormat:-message can take variables/objects/etc. much like NSLog (like this: NSLog(@“Hello %@”, name);, where name is a string), but I don’t understand how this can be used in the main.m to store the name of different things/companies. For instance, my project includes three different companies, which each are a different instance of BNRStockHolding; Wouldn’t the description-method need to have a setter-method so that you can give it a different name for every instance?
// FooBar.m
#import "FooBar.h"
@implementation Foo
@end
@implementation FooBar
- (NSString *)description
{
NSString * str = [NSString stringWithFormat:@"%s, and I live at %p.", "I am a FooBar", self];
return str;
}
@end