2016-12-20 17 views
0

iOSアプリケーションでは、followtonのようなシングルトンクラスを作成しています。同じクラスにはいくつかのプロパティがあります。プロパティに値を設定すると、すべてのデータがメモリにキャッシュされます。シングルトンクラスのキャッシュデータをリセットするにはどうすればよいですか?

[Model sharedInstance] = nilのようにするにはどうすればよいですか?

Model.m 

    + (instancetype)sharedInstance 
    { 
     static Model *objModel = nil; 
     static dispatch_once_t predicate; 
     dispatch_once(&predicate, ^{ 
      objModel = [[Model alloc]init]; 
     }); 
     return objModel; 
    } 
+0

あなたが各プロパティに対して行います '+(無効)reset'メソッドを作成することができます[モデルsharedInstance] .property = nilなど – Larme

答えて

0

できません。シングルトンとは何か一度初期化されるエンティティ。あなたが "再インスタンス化"したい場合は、シングルトンパターンではなくなります。おそらく、あなたはグローバル変数を使う傾向があるでしょうか?

しかし、あなたはこのようなシングルトン何かのプロパティをクリアすることができます:hereを引用[Object instance].myMutableProperty = nil;

を:In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

関連する問題