代码规范初体验

  • app.h 文件
//
//  App.h
//  01-应用管理
//
//  Created by JoKerTower on 15/6/19.
//  Copyright (c) 2015年 JoKerTower. All rights reserved.
//

#import <Foundation/Foundation.h>
/**
 copy:NSString
 strong : 一般对象
 weak :UI控件
 assign : 基本数据类型
 */
@interface App : NSObject
/**
 * 名称
 */
@property (nonatomic,copy)NSString *name;
/**
 *  图标
 */
@property (nonatomic,copy)NSString *icon;

/**
 *  通过字典初始化模型
 *  自定义构造方法(id)
 *  @param dict 字典对象
 *
 *  @return 已经初始化完毕的模型对象
 */
//- (id)initWithDict:(NSDictionary *) dict;
/**
 *  id 改为instancetype,因为id是万能指针 接受类型写错了都不知道
 *  eg:  NSString *app = [App appWithDict:dict];用NSString接收它不会报错,
 *  实际上这种写法是错误的
 */
- (instancetype)initWithDict:(NSDictionary *) dict;
/**
 *  类方法,主要是为了代码规范
 *
 *  @param dict <#dict description#>
 *
 *  @return <#return value description#>
 */
+ (instancetype)appWithDict:(NSDictionary *) dict;
@end
  • app.m 文件
//
//  App.m
//  01-应用管理
//
//  Created by JoKerTower on 15/6/19.
//  Copyright (c) 2015年 JoKerTower. All rights reserved.
//

#import "App.h"

@implementation App
/**
 *  实现封装
 *
 *  @param dict <#dict description#>
 *
 *  @return <#return value description#>
 */
- (id) initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        //3.2 将字典中的所有属性赋值给模型
        self.name = dict[@"name"];
        self.icon = dict[@"icon"];

    }
    return self;
}
/**
 *  [[App alloc] initWithDict:dict] -> [[self alloc] initWithDict:dict]
 *   改为self,睡调用改方法,self指向睡
 *  @param dict <#dict description#>
 *
 *  @return <#return value description#>
 */
+ (id)appWithDict:(NSDictionary *) dict
{
    return [[self alloc] initWithDict:dict];
}

@end

-调用

-(NSArray *) apps{

    if(_apps == nil){   //懒加载
        //初始化操作

        //1.获得plist的全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];

        //2.加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];

        NSMutableArray *appArray = [NSMutableArray array];
        //3. 将dictArray里面的所有字典转成模型对象,放到新的数组
        for (NSDictionary *dict in dictArray) {
            //3.1 创建模型
            App *app = [App appWithDict:dict];

            //3.3 添加模型对象到数组中
            [appArray addObject:app];

        }
        _apps = appArray;

    }
    return _apps;
}

备用,指点自己

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值