DataModel Object for archiving


/ Published in: Objective C
Save to your folder(s)

Here's a simple object class that has been set up so it can be archived using the NSKeyedUnarchiver and NSKeyedArchiver classes. This process in another snippet.


Copy this code and paste it in your HTML
  1. /* INTERFACE */
  2.  
  3. #define kfavesArray @"favesArray"
  4. #define kCurrentFave @"currentFave"
  5. #define kCurrentFaveIndex @"currentFaveIndexPath"
  6.  
  7. #import <Foundation/Foundation.h>
  8.  
  9. @class FaveVO;
  10.  
  11.  
  12. @interface DataModel : NSObject <NSCoding>
  13. {
  14. NSMutableArray *favesArray;
  15. FaveVO *currentFave;
  16. int faveIndex;
  17. }
  18.  
  19. @property (nonatomic, retain) NSMutableArray *favesArray;
  20. @property (nonatomic, retain) FaveVO *currentFave;
  21. @property int faveIndex;
  22.  
  23. -(void)addNewFaveLocation:(FaveVO *)fave;
  24. -(void)createNewFaveVOFromFaveVO:(FaveVO *)fave;
  25. -(void)setFaveLocation:(int)index;
  26. -(void)removeFaveLocation;
  27.  
  28. @end
  29.  
  30. /* IMPLEMENTATION */
  31.  
  32. #import "DataModel.h"
  33. #import "FaveVO.h"
  34.  
  35. @implementation DataModel
  36.  
  37. @synthesize favesArray;
  38. @synthesize currentFave;
  39. @synthesize faveIndex;
  40.  
  41. //------------------------------------------------------
  42. -(id)init
  43. {
  44. if(self = [super init])
  45. {
  46. favesArray = [[NSMutableArray alloc] init];
  47. }
  48.  
  49. return self;
  50. }
  51. //--------------------------------------------------
  52. -(void)addNewFaveLocation:(FaveVO *)fave
  53. {
  54. if(!favesArray) favesArray = [[NSMutableArray alloc] init];
  55.  
  56. [favesArray insertObject:fave atIndex:0];
  57. currentFave = [favesArray objectAtIndex:0];
  58. faveIndex = 0;
  59.  
  60. NSLog(@"DataModel currentFave: %@ faveIndex: %i", currentFave,faveIndex);
  61. }
  62. //--------------------------------------------------
  63. -(void)setFaveLocation:(int)index
  64. {
  65. currentFave = [favesArray objectAtIndex:index];
  66. faveIndex = index;
  67.  
  68. NSLog(@"DataModel currentFave: %@ faveIndex: %i", currentFave,faveIndex);
  69. }
  70. //--------------------------------------------------
  71. -(void)removeFaveLocation
  72. {
  73. currentFave = nil;
  74.  
  75. NSLog(@"DataModel currentFave: %@ faveIndex: %i", currentFave,faveIndex);
  76. }
  77. //--------------------------------------------------
  78. -(void)createNewFaveVOFromFaveVO:(FaveVO *)fave
  79. {
  80. double lat = fave.lat;
  81. double lon = fave.lon;
  82.  
  83. FaveVO *newFaveVO = [[FaveVO alloc] initWithLabel:[NSString stringWithString:fave.label]
  84. streetAddress:[NSString stringWithString:fave.streetAddress]
  85. city:[NSString stringWithString:fave.city]
  86. state:[NSString stringWithString:fave.state]
  87. zip:[NSString stringWithString:fave.zip]
  88. lat:lat
  89. lon:lon];
  90. [favesArray addObject:newFaveVO];
  91. [newFaveVO release];
  92. }
  93. //--------------------------------------------------
  94. -(void)dealloc
  95. {
  96. [favesArray release];
  97. [currentFave release];
  98. [super dealloc];
  99. }
  100. //--------------------------------------------------
  101.  
  102.  
  103. //--------------------------------------------------
  104. #pragma mark NSCoding
  105. - (void)encodeWithCoder:(NSCoder *)encoder
  106. {
  107. [encoder encodeObject:favesArray forKey:kfavesArray];
  108. [encoder encodeObject:currentFave forKey:kCurrentFave];
  109. [encoder encodeInt:faveIndex forKey:kCurrentFaveIndex];
  110. }
  111. //--------------------------------------------------
  112. - (id)initWithCoder:(NSCoder *)decoder
  113. {
  114. NSLog(@"DataModel initWithCoder: %@", decoder);
  115.  
  116. if (self = [super init])
  117. {
  118. self.favesArray = [decoder decodeObjectForKey:kfavesArray];
  119.  
  120. self.faveIndex = [decoder decodeIntForKey:kCurrentFaveIndex];
  121. self.currentFave = [favesArray objectAtIndex:faveIndex];
  122. }
  123. return self;
  124. }
  125.  
  126. @end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.