私はばかげた問題に遭遇し、ほとんどすべてを試しました(3冊の本を購入し、Google全体を調べました:))が、何も役に立ちませんでした. そして、解決策は非常に単純であるべきだと私には思えます...
Objective-C でシングルトンを宣言する必要があり (それが重要な場合は iOS アプリの場合)、他のクラスから更新する必要があるいくつかのプロパティが必要です。しかし、私にはそれができません。プロパティが更新されず、「init」メソッドに同じ値が設定されています。
この問題をテストするための簡単なアプリを作成しました。それが私がやったことです:
まず、シングルトンのプロパティとして使用するサンプル クラスとそのサブクラスを宣言しました。
@interface Entity : NSObject
@property (nonatomic, strong, readwrite) NSMutableString * name;
@end
@implementation Entity
@synthesize name;
@end
@interface Company : Entity
@property (nonatomic, strong, readwrite) NSMutableString * boss;
@property (nonatomic) int rating;
@end
@implementation Company
@synthesize boss, rating;
@end
次に、「Big Nerd Ranch による iOS プログラミング ガイド」の本に記載されている方法に基づいて、シングルトン自体を宣言します。わかりやすくするために、カスタム クラスと標準の NSMutableString の両方をプロパティとして使用しています。
@class Company;
@interface CompanyStore : NSObject
{
NSMutableString * someName;
}
@property (nonatomic, strong, readwrite) Company * someCompany;
@property (nonatomic, strong, readwrite) NSMutableString * someName;
+ (CompanyStore *) store;
- (void) modifyCompanyProperties;
@end
@implementation CompanyStore
@synthesize someCompany, someName;
// Declaring the shared instance
+ (CompanyStore *) store
{
static CompanyStore * storeVar = nil;
if (!storeVar) storeVar = [[super allocWithZone:nil] init];
return storeVar;
}
// Replacing the standard allocWithZone method
+ (id) allocWithZone:(NSZone *)zone
{
return [self store];
}
次に、すべてのプロパティを初期値で初期化します。
- (id) init
{
self = [super init];
if (self) {
someCompany = [[Company alloc] init];
[someCompany setBoss:[NSMutableString stringWithFormat:@"John Smith"]];
[someCompany setName:[NSMutableString stringWithFormat:@"Megasoft"]];
[someCompany setRating:50];
someName = [[NSMutableString alloc] initWithString:@"Bobby"];
}
return self;
}
そして、別のクラス (ビューにコンテンツを表示するビュー コントローラー) から:
1.シングルトンのプロパティの値を取得します。すべて問題ありません。「John Smith」、「Megasoft」、「Bobby」、および int 値として 50 を取得します。私のinitメソッドからの値。
2.そのView Controllerからシングルトンのプロパティを変更します(いくつかの方法を使用します-どれが正しいかわかりません):
- (IBAction)modify2Button:(id)sender {
CompanyStore * cst = [CompanyStore store];
NSMutableString * name = [[NSMutableString alloc] initWithString:@"Microcompany"];
NSMutableString * boss = [[NSMutableString alloc] initWithString:@"Larry"];
[[[CompanyStore store] someCompany] setName:name];
cst.someCompany.boss = boss;
NSMutableString * strng = [[NSMutableString alloc] initWithString:@"Johnny"];
[cst setSomeName:strng];
}
...そして、値を再度取得しようとしています。「John Smith」、「Megasoft」などの古いセットを取得していますが、文字列の 1 つにブレークポイントを設定すると、シングルトンの name プロパティが「Megasoft」ではなく「Microcompany」であることがわかります。休憩の時間… でも割り当てられていないようです。
3.次に、別のことを試しています。View Controller から、別の値のセットをプロパティに割り当てるシングルトンのプライベート メソッドを呼び出しています。これは、シングルトンのメソッドです。
- (void) modifyCompanyProperties
{
NSMutableString * boss = [[NSMutableString alloc] initWithString:@"George"];
NSMutableString * name = [[NSMutableString alloc] initWithString:@"Georgeland"];
[someCompany setBoss:boss];
[someCompany setName:name];
[someCompany setRating:100000];
[someName setString:@"Nicholas"];
}
4. View Controller から更新されたプロパティ値を再度取得しようとしていますが、それでも「John Smith」、「Megasoft」を取得します... 何も変わりません。
シングルトンのプロパティは一度だけ設定されているようで、属性が「readwrite」として宣言されていても変更できません。
単純なことを理解していないようです。シングルトンでプロパティを正しく宣言および更新する方法を誰かが説明できれば、とても感謝しています。