Last active
September 15, 2015 08:44
-
-
Save macbaszii/6cbd2182526c5e10d9dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (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