Mackey18
Mackey18

Reputation: 2352

iTunes Search API, finding Free prices

When searching the iTunes store, I've hit a little problem. I'm trying to change the text of my label to "FREE" when the price is 0.00, as opposed to displaying the price. The problem is, my comparison of the two NSDecimalNumbers fails. Here's where I am currently at.

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setCurrencyCode:self.searchResult.currency];
NSString *price = [formatter stringFromNumber:self.searchResult.price];

NSDecimalNumber *free = [[NSDecimalNumber alloc] initWithFloat:0.00f];

NSLog(@"Price:%@",price);
NSLog(@"Free:%@",free);
NSLog(@"self.searchResult.price: %@", self.searchResult.price);

if (self.searchResult.price == free) {
    NSString *freeText = [NSString stringWithFormat:@"FREE"];
    self.priceLabel.text = freeText;
}
else {
    self.priceLabel.text = price;
}
NSLog(@"priceLabel:%@", self.priceLabel.text);

What's really weird is that the Console even says that both free and del.searchResult.price are the same:

2012-11-24 20:01:16.178 StoreSearch[1987:c07] Price:$0.00
2012-11-24 20:01:16.178 StoreSearch[1987:c07] Free:0
2012-11-24 20:01:16.178 StoreSearch[1987:c07] self.searchResult.price: 0
2012-11-24 20:01:16.179 StoreSearch[1987:c07] priceLabel:$0.00

I'm a little confused by this to be frank. Any help would be appreciated. If you could explain why this has happened so that I can learn not to do it again, then I'd be even more grateful!

Regards,
Mike

Upvotes: 0

Views: 140

Answers (1)

Daij-Djan
Daij-Djan

Reputation: 50119

you got NSNumber and another NSNumber (NSDecimalNumber) and you do a pointer equal check you either need need to call isEqual or compare the primitive float values

== with objects doesnt compare the value but the object pointer (the memory address)


if(searchResult.price.floatValue == free.floatValue)

note that a float is inprecise and generally should not be used for any calculation or comparison. It is ok in this case though

Upvotes: 1

Related Questions