여기 내 마지막 질문에 대한 후속 질문 : iOS: Initialise object at start of application for all controllers to use입니다.'글로벌'개체를 합성 할 때 EXC_BAD_ACCESS
나는 다음과 같이합니다 (DB 접두어를 무시) 내 응용 프로그램을 설정 한 :
DBFactoryClass // Built a DataManaging Object for later use in the app
DBDataModel // Is created by the factory, holds all data & access methods
DBViewControllerA // Will show some of the data that DBDataModel holds
moreViewControllers that will need access to the same DBDataModel Object
은 내가 응용 프로그램을 통해 단계별로 갈 것이고, 결국 구축 할 때 내가 오류 메시지를 게시 한 후 것 .
AppDelegate.h
#import "DBFactoryClass.h"
AppDelegate.m
- (BOOL)...didFinishLaunching...
{
DBFactoryClass *FACTORY = [[DBFactoryClass alloc ]init ];
return YES;
}
DBFactoryClass.h
#import <Foundation/Foundation.h>
#import "DBDataModel.h"
@interface DBFactoryClass : NSObject
@property (strong) DBDataModel *DATAMODEL;
@end
DBFactoryClass.m
#import "DBFactoryClass.h"
@implementation DBFactoryClass
@synthesize DATAMODEL;
-(id)init{
self = [super init];
[self setDATAMODEL:[[DBDataModel alloc]init ]];
return self;
}
@end
내가 그것을 빌드 할 때
ViewControllerA.h
#import <UIKit/UIKit.h>
#import "DBDataModel.h"
@class DBDataModel;
@interface todayViewController : UIViewController
@property (strong)DBDataModel *DATAMODEL;
@property (weak, nonatomic) IBOutlet UILabel *testLabel;
@end
ViewControllerA.m
#import "todayViewController.h"
@implementation todayViewController
@synthesize testLabel;
@synthesize DATAMODEL;
- (void)viewDidLoad
{
todaySpentLabel.text = [[DATAMODEL test]stringValue];
}
@end
이 DBDataModel.h
#import <Foundation/Foundation.h>
@interface DBDataModel : NSObject
@property (nonatomic, retain) NSNumber* test;
@end
DBDataModel.m
#import "DBDataModel.h"
@implementation DBDataModel
@synthesize test;
-(id)init{
test = [[NSNumber alloc]initWithInt:4];
return self;
}
@end
, 내가 얻을 수행원 오류 : EXC_BAD_ACCESS
이 줄 : 지금까지 내가 당신의 DBFactoryClass
클래스는 어디에도 저장하지 않으며, 당신이 사용하기 때문에 당신이 (ARC를 사용하는 경우, 따라서 할당 직후 발표 결코 말할 수 DBFactoryClass.m
'@ synthesisize '가하는 일은 속성에 대한 접근 자의 구현을 자동으로 생성하는 것입니다. EXC_BAD_ACCESS는 접근 자 중 하나가 실행될 때 당신이 쓰레기에 접근하고 있음을 의미합니다.이것은 아마 당신이 이렇게 할 때 일어난다 :'[self setDATAMODEL : [[DBDataModel alloc] init]];'. '[[DBDataModel alloc] init]]'이 실제로 합법적 인 객체를 반환하는지 확인하십시오. – yuji
안녕하세요. 이 덕분에 도움이되었습니다. 문제는 DBDataModel init 함수에서 누락 된 반환 자체였습니다 ... (아마도 피곤한 ^^) 저는 첫 번째 게시물의 모든 파일을 업데이트했습니다. 이제부터는 새로운 문제가 생겼습니다. 나는 testNumber와 Label을 프로젝트에 추가했다. (숫자는 datamodel에, viewcontroller에 레이블을 붙인다.) 실제로 작동 하는지를 알기 위해서다. 어떻게 든 모델에서 데이터를 검색 할 수 없습니다. 아이디어? –
도움이 된 것을 기쁘게합니다. 답변으로 다시 게시하십시오. 다른 점은 별도의 질문으로 게시해야합니다. – yuji