fork download
  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface MyObject : NSObject <NSCoding>{
  4. NSNumber *newCheck_;
  5. NSNumber *favoraite_;
  6. }
  7.  
  8. @property(nonatomic, retain) NSNumber* newCheck_;
  9. @property(nonatomic, retain) NSNumber* favoraite_;
  10.  
  11. -(void)readData:(NSString*)path;
  12. -(void)writeData:(NSString*)path;
  13.  
  14. @end
  15.  
  16. @implementation MyObject
  17.  
  18. @synthesize newCheck_;
  19. @synthesize favoraite_;
  20.  
  21. - (id)init
  22. {
  23. if (self = [super init]) {
  24. self.newCheck_ = [NSNumber numberWithInt:0];
  25. self.favoraite_ = [NSNumber numberWithInt:0];
  26. }
  27. return self;
  28. }
  29.  
  30. - (void)encodeWithCoder:(NSCoder *)coder {
  31. // シリアライズ時に呼び出される。
  32. [coder encodeObject:newCheck_ forKey:@"newCheck"];
  33. [coder encodeObject:favoraite_ forKey:@"favoraite"];
  34. }
  35.  
  36. - (id)initWithCoder:(NSCoder *)coder {
  37. // デシリアライズ時に呼び出されるイニシャライザ
  38. if (self = [super init]) {
  39. // オブジェクトの生成ではないので、retainを忘れないように。
  40. newCheck_ = [[coder decodeObjectForKey:@"newCheck"] retain];
  41. favoraite_ = [[coder decodeObjectForKey:@"favoraite"] retain];
  42. }
  43. return self;
  44. }
  45.  
  46. -(void)readData:(NSString*)path{
  47.  
  48. NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  49.  
  50. // 空なら初期化
  51. if( [defaults objectForKey:path] == NULL ){
  52. self.newCheck_ = [NSNumber numberWithInt:0];
  53. self.favoraite_ = [NSNumber numberWithInt:0];
  54. }else{
  55. NSData* readData = (NSData*)[defaults objectForKey:path];
  56. self = [NSKeyedUnarchiver unarchiveObjectWithData:readData];
  57. }
  58.  
  59. // test用
  60. newCheck_ = [NSNumber numberWithInt:1];
  61. favoraite_ = [NSNumber numberWithInt:1];
  62.  
  63. NSLog( @"A %@ %@ ", newCheck_, self.favoraite_ );
  64.  
  65. }
  66.  
  67. -(void)writeData:(NSString*)path{
  68.  
  69. NSData* writeData = [NSKeyedArchiver archivedDataWithRootObject:self];
  70.  
  71. NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
  72. [defaults setObject:writeData forKey:path];
  73. [defaults synchronize];
  74.  
  75. }
  76.  
  77. @end
  78.  
  79. @interface TestObj
  80. @end
  81.  
  82. @implementation TestObj
  83. int main() {
  84. struct {
  85. int row;
  86. } indexPath;
  87. NSNumber *newCheck_ = [[NSNumber alloc] initWithInt:1];
  88. struct {
  89. NSNumber *favoraite_;
  90. } self;
  91. indexPath.row = 346;
  92. self.favoraite_ = [[NSNumber alloc] initWithInt:1];
  93.  
  94. // 実装部分
  95. MyObject *myObj = [[MyObject alloc] init];
  96. NSString *path = [NSString stringWithFormat:@"DATA_%03d", (int)indexPath.row];
  97. [myObj readData:path];
  98. NSLog( @"B %@ %@ ", newCheck_, self.favoraite_ );
  99.  
  100. return 0;
  101. }
  102. @end
Success #stdin #stdout #stderr 0.03s 10872KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
2015-02-26 01:26:33.970 prog[352] autorelease called without pool for object (96846e0) of class GSCInlineString in thread <NSThread: 0x9652798>
2015-02-26 01:26:33.971 prog[352] autorelease called without pool for object (968ae90) of class NSUserDefaults in thread <NSThread: 0x9652798>
2015-02-26 01:26:33.971 prog[352] autorelease called without pool for object (9682740) of class GSArrayEnumerator in thread <NSThread: 0x9652798>
2015-02-26 01:26:33.971 prog[352] A 1 1 
2015-02-26 01:26:33.971 prog[352] B 1 1