HMQuestion.h
#import <Foundation/Foundation.h>
@interface HMQuestion : NSObject
@property (nonatomic, copy) NSString *answer;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) NSArray *options; - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)questionWithDict:(NSDictionary *)dict; /** 返回所有题目数组 */
+ (NSArray *)questions;
@end
HMQuestion.m
#import "HMQuestion.h" @implementation HMQuestion // 在成员方法中,如果给self赋值,只能在initXXX方法中进行
// 语法约定:
// 1> 所有的方法首字母小写
// 2> 当单词切换的时候,单词首字母大写(驼峰法)
// 3> 类名要大写
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
} + (instancetype)questionWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} + (NSArray *)questions
{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil]]; NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
[arrayM addObject:[self questionWithDict:dict]];
} return arrayM;
} // 对象描述方法,类似于java中的toString(),便于跟踪调试的
// 建议:如果是自定义的模型,最好编写description方法,可以方便调试
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p> {answer: %@, icon: %@, title: %@, options: %@}", self.class, self, self.answer, self.icon, self.title, self.options];
} @end
HMViewController.m文件
#import "HMViewController.h"
#import "HMQuestion.h" @interface HMViewController ()
@property (nonatomic, strong) NSArray *questions;
@end @implementation HMViewController
//懒加载
- (NSArray *)questions
{
if (_questions == nil) {
_questions = [HMQuestion questions];
}
return _questions;
} - (void)viewDidLoad
{
[super viewDidLoad]; NSLog(@"%@", self.questions);
// for (HMQuestion *obj in self.questions) {
// NSLog(@"%@", obj);
// }
} /** 调整状态栏颜色 */
/**
UIStatusBarStyleDefault 黑色状态栏
UIStatusBarStyleLightContent 亮色状态栏
*/
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
} @end