Skip to content

Instantly share code, notes, and snippets.

@macbaszii
Last active September 15, 2015 08:44
Show Gist options
  • Select an option

  • Save macbaszii/6cbd2182526c5e10d9dd to your computer and use it in GitHub Desktop.

Select an option

Save macbaszii/6cbd2182526c5e10d9dd to your computer and use it in GitHub Desktop.
- (BOOL)canPurchaseItem:(Item *)item forUser:(User *)user withError:(NSError **)error {
if ([self.items indexOfObject:item] == NSNotFound) {
error = [NSError errorWithDomain:StoreErrorDomain
code:PurchaseOutOfStockItemCode
userInfo:@{ NSLocalizedDescriptionKey: @"Item is out of stock" }];
return NO;
}
if (user.money - item.price < 0) {
error = [NSError errorWithDomain:StoreErrorDomain
code:PurchaseOutOfStockItemCode
userInfo:@{ NSLocalizedDescriptionKey: @"Insufficient fund to purchase this item" }];
return NO;
}
[self purchaseItem:item forUser:user];
return YES;
}
NSError *error = nil;
[self canPurchaseItem:someItem forUser:someUser withError:&error];
if (error != nil) {
// do something with error occur
} else {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment