В этом примере мой NSDictionary инициализируется с 0 парами ключ / значение, как показано в моем отладчике.Он будет правильно инициализироваться, когда я сделаю то же самое в моем ViewController, но я бы предпочел придерживаться дизайна MVC и иметь NSDictionary в моей модели.
ShakespeareViewController.h
#import <UIKit/UIKit.h>
@interface ShakespeareViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *sonnetDisplay;
@end
ShakespeareViewController.m
#import "ShakespeareViewController.h"
#import "ShakespeareModel.h"
@interface ShakespeareViewController ()
@property (nonatomic, strong) ShakespeareModel *sonnet;
@end
@implementation ShakespeareViewController
@synthesize sonnetDisplay = _sonnetDisplay;
@synthesize sonnet = _sonnet;
- (IBAction)sonnetButton:(UIButton *)sender
{
self.sonnetDisplay.text = [self.sonnet grabSonnet:@"19"];
}
@end
ShakespeareModel.h
#import <Foundation/Foundation.h>
@interface ShakespeareModel : NSObject
-(NSString *)grabSonnet:(NSString *)atNumber;
@end
ShakespeareModel.m
#import "ShakespeareModel.h"
@interface ShakespeareModel()
@property (nonatomic, strong) NSDictionary *sonnets;
@end
@implementation ShakespeareModel
@synthesize sonnets = _sonnets;
-(NSDictionary *)sonnets
{
if (!_sonnets)
{
_sonnets = [[NSDictionary alloc] initWithObjectsAndKeys:@"19", @"19", nil];
}
return _sonnets;
}
-(NSString *)grabSonnet:(NSString *)atNumber
{
NSString *chosenSonnet = [self.sonnets objectForKey:@"19"];
return chosenSonnet;
}
@end
Любые идеи о том, что я делаю неправильно, очень ценятся.Я не могу понять, почему это не будет инициализироваться с объектом 19 со значением ключа 19.