Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created August 12, 2012 18:11
Show Gist options
  • Save codeswimmer/3333522 to your computer and use it in GitHub Desktop.
Save codeswimmer/3333522 to your computer and use it in GitHub Desktop.
Check for multiple assignment to an ivar
NSString *const ASMultipleAssignmentException = @"Multiple Assignment Exception";
#ifndef CHECK_FOR_MULTI_ASSIGN
#define CHECK_FOR_MULTI_ASSIGN(ivar) \
do { \
if (ivar) { \
NSString *AS_reason = @"Cannot assign to " #ivar " more than once"; \
@throw [NSException exceptionWithName:ASMultipleAssignmentException \
reason:AS_reason \
userInfo:nil]; \
} \
} while(0)
#endif
- (void)setName:(NSString *)name
{
CHECK_FOR_MULTI_ASSIGN(_name);
_name = name;
}
- (void)setScore:(NSNumber *)score
{
CHECK_FOR_MULTI_ASSIGN(_score);
_score = score;
}
- (void)setWebsite:(NSURL *)website
{
CHECK_FOR_MULTI_ASSIGN(_website);
_website = website;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment