Created
February 24, 2014 04:54
-
-
Save keicoder/9182146 to your computer and use it in GitHub Desktop.
objective-c : give object a unique numeric ID (itemId)
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
| //give object a unique numeric ID (itemId) | |
| //ChecklistItem.h | |
| @property (nonatomic, assign) NSInteger itemId; | |
| //ChecklistItem.m | |
| //initWithCoder - for view controllers that are automatically loaded from a nib or Storyboard | |
| //for unfreezing the objects from the file | |
| - (id)initWithCoder:(NSCoder *)aDecoder | |
| { | |
| if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
| if ((self = [super init])) { | |
| //opposite from encodeWithCoder | |
| self.text = [aDecoder decodeObjectForKey:@"Text"]; | |
| self.checked = [aDecoder decodeBoolForKey:@"Checked"]; | |
| //for local notifications | |
| self.dueDate = [aDecoder decodeObjectForKey:@"DueDate"]; | |
| self.shouldRemind = [aDecoder decodeBoolForKey:@"ShouldRemind"]; | |
| self.itemId = [aDecoder decodeIntegerForKey:@"ItemID"]; | |
| } | |
| return self; | |
| } | |
| #pragma mark - encode protocol | |
| - (void)encodeWithCoder:(NSCoder *)aCoder | |
| { | |
| if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
| [aCoder encodeObject:self.text forKey:@"Text"]; | |
| [aCoder encodeBool:self.checked forKey:@"Checked"]; | |
| //for local notifications | |
| [aCoder encodeObject:self.dueDate forKey:@"DueDate"]; | |
| [aCoder encodeBool:self.shouldRemind forKey:@"ShouldRemind"]; | |
| [aCoder encodeInteger:self.itemId forKey:@"ItemID"]; | |
| } | |
| #import "DataModel.h" | |
| #pragma mark - 초기화 | |
| - (id)init { | |
| if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
| if (self = [super init]) { | |
| self.itemId = [DataModel nextChecklistItemId]; | |
| } | |
| return self; | |
| } | |
| //DataModel.h | |
| //class method | |
| + (NSInteger)nextChecklistItemId; | |
| //DataModel.m | |
| #pragma mark - for local notifications | |
| + (int)nextChecklistItemId { | |
| NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
| NSInteger itemId = [userDefaults integerForKey:@"ChecklistItemId"]; | |
| [userDefaults setInteger:itemId + 1 forKey:@"ChecklistItemId"]; | |
| [userDefaults synchronize]; //force NSUserDefaults to write changes to disk immediately | |
| return itemId; | |
| } | |
| #pragma mark - register Defaults Value for NSUserDefaults | |
| - (void)registerDefaults | |
| { | |
| if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
| //register Defaults Value for NSUserDefaults | |
| NSDictionary *dictionary = @{ @"ChecklistIndex" : @-1, | |
| @"FirstTime" : @YES, | |
| @"ChecklistItemId" : @0, //Add a default value for “ChecklistItemId” | |
| }; | |
| [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; | |
| } | |
| //ChecklistViewController.m | |
| - (void)configureTextForCell:(UITableViewCell *)cell withChecklistItem:(ChecklistItem *)item | |
| { | |
| if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
| UILabel *label = (UILabel *)[cell viewWithTag:1000]; //tag can get a reference to a control without making @property | |
| //label.text = item.text; | |
| //to test itemId | |
| label.text = [NSString stringWithFormat:@"%d: %@", item.itemId, item.text]; //test ChecklistItemId | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment