iOSメモモード(簡単に使用)
2925 ワード
FastCoderローカルシーケンス化、NSDataの転送、オブジェクト実装NSCopyingストレージより良い
メモセンター //
// MementoCenter.h
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import
#import "MementoCenterProtocol.h"
@interface MementoCenter : NSObject
/**
@param object
@param key
*/
+ (void)saveMementoObject:(id)object withKey:(NSString *)key;
/**
@param key
@return
*/
+ (id)mementoObjectWithKey:(NSString *)key;
@end
//
// MementoCenter.m
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import "MementoCenter.h"
@implementation MementoCenter
+ (void)saveMementoObject:(id)object withKey:(NSString *)key {
}
+ (id)mementoObjectWithKey:(NSString *)key {
return nil;
}
@end
メモセンターに格納されるオブジェクトは、このプロトコルを満たす必要があります。メモセンターに格納されます。 //
// MementoCenterProtocol.h
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import
@protocol MementoCenterProtocol
/**
@return
*/
- (id)currentState;
/**
@param state
*/
- (void)recoverFromState:(id)state;
@end
ストレージオブジェクト(アップル) //
// Apple.h
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import
#import "MementoCenterProtocol.h"
@interface Apple : NSObject
@end
//
// Apple.m
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import "Apple.h"
@implementation Apple
- (id)currentState {
return self;
}
- (void)recoverFromState:(id)state {
}
@end
使用 //
// ViewController.m
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import "ViewController.h"
#import "Apple.h"
#import "MementoCenter.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Apple *apple = [[Apple alloc] init];
///save
[MementoCenter saveMementoObject:[apple currentState] withKey:@"Apple"];
///get
[apple recoverFromState:[MementoCenter mementoObjectWithKey:@"Apple"]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// MementoCenter.h
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import
#import "MementoCenterProtocol.h"
@interface MementoCenter : NSObject
/**
@param object
@param key
*/
+ (void)saveMementoObject:(id)object withKey:(NSString *)key;
/**
@param key
@return
*/
+ (id)mementoObjectWithKey:(NSString *)key;
@end
//
// MementoCenter.m
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import "MementoCenter.h"
@implementation MementoCenter
+ (void)saveMementoObject:(id)object withKey:(NSString *)key {
}
+ (id)mementoObjectWithKey:(NSString *)key {
return nil;
}
@end
//
// MementoCenterProtocol.h
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import
@protocol MementoCenterProtocol
/**
@return
*/
- (id)currentState;
/**
@param state
*/
- (void)recoverFromState:(id)state;
@end
ストレージオブジェクト(アップル) //
// Apple.h
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import
#import "MementoCenterProtocol.h"
@interface Apple : NSObject
@end
//
// Apple.m
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import "Apple.h"
@implementation Apple
- (id)currentState {
return self;
}
- (void)recoverFromState:(id)state {
}
@end
使用 //
// ViewController.m
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import "ViewController.h"
#import "Apple.h"
#import "MementoCenter.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Apple *apple = [[Apple alloc] init];
///save
[MementoCenter saveMementoObject:[apple currentState] withKey:@"Apple"];
///get
[apple recoverFromState:[MementoCenter mementoObjectWithKey:@"Apple"]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// Apple.h
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import
#import "MementoCenterProtocol.h"
@interface Apple : NSObject
@end
//
// Apple.m
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import "Apple.h"
@implementation Apple
- (id)currentState {
return self;
}
- (void)recoverFromState:(id)state {
}
@end
//
// ViewController.m
// LearnMemento
//
// Created by on 2017/3/8.
// Copyright © 2017 ylq. All rights reserved.
//
#import "ViewController.h"
#import "Apple.h"
#import "MementoCenter.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Apple *apple = [[Apple alloc] init];
///save
[MementoCenter saveMementoObject:[apple currentState] withKey:@"Apple"];
///get
[apple recoverFromState:[MementoCenter mementoObjectWithKey:@"Apple"]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end