シングルトン(StackOverflowの助けを借りて) を設定しましたが、配列 "listOfHeadings"を変更/アクセスしようとすると、何も変化していないようです。 コンパイラからのエラーや警告は出ません。シングルトンで配列を変更できません
私は私のGlobalData.hでこれを持っている:
#import <Foundation/Foundation.h>
@interface GlobalData : NSObject {
NSMutableArray *listOfHeadings;
}
@property(nonatomic,retain)NSMutableArray *listOfHeadings;
+(GlobalData*)getInstance;
@end
これは私のGlobalData.mです:
#import "GlobalData.h"
@implementation GlobalData
@synthesize listOfHeadings;
static GlobalData *instance;
+(GlobalData *)getInstance{
@synchronized(self){
if(!instance){
instance= [[GlobalData alloc] init];
instance.listOfHeadings=[[NSMutableArray alloc]init]; //EDIT: This line added to resolve problem
}
}
return instance;
}
@end
そして私は、私のAppDelegate.mでシングルトンにアクセス:
#import "GlobalData.h"
...inside didFinishLaunchingWithOptions...
GlobalData *globDat=[GlobalData getInstance];
[globDat.listOfHeadings addObject:@"Message Settings"];
NSLog(@"appdel m array test %i",[globDat.listOfHeadings count]); // prints 0!
明らかに私は間違ったことをしています - 私の間違いを指摘する助けができますか?おかげさまで
これはGlobalData.hファイルでは処理されませんか? – wayneh
質問に表示されているコードにはありません。あなたが行ったのは、あなたが可変配列を持っていることを宣言することだけです。実際にどこかで/ initを割り当てる必要があります。 – jrturton
他のスレッドでサンプルコードを見つけました...ありがとう! http://stackoverflow.com/questions/6324732/using-a-singleton-to-create-an-array-accessible-by-multiple-views – wayneh