这是我最后一个问题的后续问题:iOS: Initialise object at start of application for all controllers to use

我已经按照以下步骤设置了应用程序(忽略数据库前缀):

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:
@synthesize DATAMODEL;

DBFactoryClass.m

最佳答案

@synthesize的作用是自动生成属性访问器的实现。那里的EXC_BAD_ACCESS意味着您在执行访问器之一时正在访问垃圾。

这可能发生在这里:

[self setDATAMODEL:[[DBDataModel alloc]init ]];

确保DBDataModelinit实现实际返回合法对象。

10-08 05:48
查看更多