仿美团项目-第一部分

最近想写一个个人项目,选了好久最后选择了仿照美团做一个app,好将学到的语言和开发基础知识用到具体项目上试试!

一、团购数据来源:

1. 网络数据来源(团购数据):点评网开发者SDK  (key/secret)

2. 网络数据来源(元数据) —> cities.plist(城市名字)

二、首先处理数据(Model)层:

SDK: Software Development Kit 软件开发包

API: Application Programming Interface: 应用程序接口

IDE: Intergted Development Environment: Xcode/Eclipse

 通过API获取申请到你自己的 ID 和 Key ,牢记Key!!

基本操作步骤:首先下载点评网的SDK,然后读懂如何使用SDK,最后将SDK导入到项目中。

三、开始创建项目:

1.将SDK中的类导入到Project中的方法:

(1)将需要的文件拽到项目中;

(2)然后遵循协议;

(3)实现协议的方法;

实现收据发送接收的一般步骤为:客户端发送请求->服务器 -> JSON数据 (订单数据);

2.创建YKDeal.h/.m模型类, .h中的属性针对和服务器返回的JOSN中的key要一一对应(注意点:一定要一一对应,完全一样!)

3.解析JSON数据 (对象NSDictionary转模型)

1) 针对团购订单创建模型对象YKDeal.h/.m:NSObject

2) 使用setValuesForKeysWithDictionary方法解析JSON数据:

  a.在该文件下声明的属性的名字需要和服务器返回的key要一模一样

  b.如果遇到有OC中的关键字,需要改成其他的(eg:description--->desc)

  c.遇到第b中情况,必须手动来绑定description和desc,必须要实现setValue:forUndefinedKey:方法

3)调用解析步骤

a.创建一个模型对象 

YKDeal *deal = [[YKDeal alloc] init];

b.使用setValues方法将传入的字典对象自动地和deal模型对象匹配 (通过dealDic中的key和YKDeal模型对象.h中的属性一一对应)

[deal setValuesForKeysWithDictionary:dealDic];

代码区:

YKDeal.h文件内容:

#import <Foundation/Foundation.h>

@interface YKDeal : NSObject

/*不同:
 1.在该文件下声明的属性的名字要和服务器返回回来的key要一模一样
 2.如果遇到oc中的关键字,需要将属性名改为其他的(eg:description ---> desc)
 3.遇到第二种情况,则必须要手动的绑定关键字(eg:descrption和desc),必须要实现setValue:forUndefinedKey:方法
 */
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *desc;/**<订单描述 */
@property (nonatomic, strong) NSNumber *list_price;/**<原价 */
@property (nonatomic, strong) NSNumber *current_price;/**<现价 */
@property (nonatomic, strong) NSNumber *purchase_count;/**<购买数量 */
@property (nonatomic, strong) NSString *image_url; /**<团购订单(大图)图片对应的url */
@property (nonatomic, strong) NSString *s_image_url;/**<团购订单(小图)图片对应的url */

@end
View Code

YKDeal.m文件内容:

#import "YKDeal.h"

@implementation YKDeal

//重写方法 ---> 目的:将字典中的那个description关键字和属性中的desc进行绑定
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if ([key isEqualToString:@"description"]) {
        self.desc = value;
        
    }
}

@end
View Code

到此处 处理网络JSON数据基本上就算完了! 

四、原数据文件的处理 

一)处理网络中的“元数据”:

1. 四个plist文件 (分类元数据;城市元数据;区域元数据;排序元数据)

2.采用单例方式读取每个plist文件中的数据(读取一次);

3. 类的划分:

viewController.h/.m

YKMetaDataTool.h/.m -> 处理所有和元数据相关的逻辑

YKCity.h/.m; YKCategory.h/.m; YKSort.h/.m

YKDeal.h/.m

将程序需要的四个plist文件和所有的图片拖拽到项目中;

二)创建三个模型类:

a. YKCity.h/.m; YKCategory.h/.m; YKSort.h/.m

b. 针对这三个模型类的.h中声明属性(需要和plist文件中的key一一对应)

三)YKMetaDataTool.h提供三个类方法,分别对应分类、城市、排序 方法

a.排序数据(返回所有排序模型对象)

+ (NSArray *)sorts;

b.城市数据(返回所有城市模型对象)

+ (NSArray *)cities;

c.分类数据(返回所有分类模型对象)

+ (NSArray *)categories;

代码区:

YKMetaDataTool.h文件内容:

#import <Foundation/Foundation.h>

@interface YKMetaDataTool : NSObject

/**
 *  排序数据(返回所有的排序模型对象)
 */
+ (NSArray *)sorts;

/**
 *  城市数据
 */
+ (NSArray *)cities;

/**
 *  分类数据
 */
+ (NSArray *)categories;

/**
 *  根据传入的城市,返回该城市对应的所有区域和子区域
 */
+ (NSArray *)regionWithCityName:(NSString *)cityName;

@end
View Code

YKMetaDataTool.m文件内容:

#import "YKMetaDataTool.h"
#import "YKSort.h"
#import "YKCategory.h"
#import "YKCity.h"
#import "YKRegion.h"

@implementation YKMetaDataTool

/**方式:单例模式
 *  1.从plist文件中读取数据(bounds里)---> NSDictionary
    2.循环解析从plist文件中读取的数据(数据)--->TRSort
    3.将解析好的所有模型对象存储到数组中,并返回
 */
static NSArray *_sorts;
+ (NSArray *)sorts{
    if (!_sorts) {
        _sorts = [[self alloc] getAndParseSortsFile:@"sorts.plist"];
    }
    return _sorts;
}

//从plist文件中读取数据并返回到数组中
- (NSArray *)getArrayFromPlistFile:(NSString *)fileName {
    //1.从fileName中读取数据
    NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
    NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
    return array;
}

- (NSArray *)getAndParseSortsFile:(NSString *)fileName {
    
    NSArray *sortsArray = [self getArrayFromPlistFile:fileName];
    //2.循环解析
    NSMutableArray *sortsMutableArray = [NSMutableArray array];
    for (NSDictionary *sortDic in sortsArray) {
        YKSort *sort = [YKSort new];
        [sort setValuesForKeysWithDictionary:sortDic];
        [sortsMutableArray addObject:sort];
    }
    //3.返回
    return [sortsMutableArray copy];
}


//城市
static NSArray *_cities;
+ (NSArray *)cities{
    if (!_cities) {
        _cities = [[self alloc] getAndParseCityFile:@"cities.plist"];
    }
    return _cities;
}

- (NSArray *)getAndParseCityFile:(NSString *)fileName {
    NSArray *cityArray = [self getArrayFromPlistFile:fileName];
    //循环解析
    NSMutableArray *cityMutableArray = [NSMutableArray array];
    for (NSDictionary *cityDic in cityArray) {
        YKCity *city = [YKCity new];
        [city setValuesForKeysWithDictionary:cityDic];
        [cityMutableArray addObject:city];
    }
    return [cityMutableArray copy];
}

//分类
static NSArray *_categories;
+ (NSArray *)categories{
    if (!_categories) {
        _categories = [[self alloc] getAndParseCategoryFile:@"categories.plist"];
    }
    return _categories;
}

- (NSArray *)getAndParseCategoryFile:(NSString *)fileName {
    NSArray *categoryArray = [self getArrayFromPlistFile:fileName];
    NSMutableArray *categoriesMutableArray = [NSMutableArray array];
    for (NSDictionary *categoryDic in categoryArray) {
        YKCategory *category = [YKCategory new];
        [category setValuesForKeysWithDictionary:categoryDic];
        [categoriesMutableArray addObject:category];
    }
    return [categoriesMutableArray copy];
}

//返回指定城市的所有区域
+ (NSArray *)regionWithCityName:(NSString *)cityName{
    
    if (cityName.length == 0) {
        return nil;
    }
    
    YKCity *findedCity = [YKCity new];
    //1.循环找到城市名字叫做cityName对应的城市模型对象YKCity
    NSArray *citysArray = [self cities];
    for (YKCity *city in citysArray) {
        if ([city.name isEqualToString:cityName]) {
            findedCity = city;
            break;
        }
    }
    //2.对YKCity中的区域数组进行解析(NSDictionary -> YKRegion)
    NSArray *regionArray = findedCity.regions;
    //循环解析(NSDictionary --> YKRegion)
    NSMutableArray *regionMutableArray = [NSMutableArray array];
    for (NSDictionary *regionDic in regionArray) {
        YKRegion *region = [YKRegion new];
        [region setValuesForKeysWithDictionary:regionDic];
        [regionMutableArray addObject:region];
    }
    return [regionMutableArray copy];
}
@end
View Code

YKSort.h文件内容:

#import <Foundation/Foundation.h>

@interface YKSort : NSObject

@property (nonatomic, strong) NSString *label;/**<label描述 */
@property (nonatomic, assign) int value;/**<value值 */

@end
View Code

YKCategory.h文件内容:

#import <Foundation/Foundation.h>

@interface YKCategory : NSObject

@property (nonatomic, strong) NSString *highlighted_icon;/**<分类高亮图标 */
@property (nonatomic, strong) NSString *icon;/**<分类图标 */
@property (nonatomic, strong) NSString *name;/**<分类名字 */
@property (nonatomic, strong) NSString *small_highlighted_icon;/**<分类高亮小图标 */
@property (nonatomic, strong) NSString *small_icon;/**<分类小图标 */
@property (nonatomic, strong) NSString *map_icon;/**<分类地图图标 */
@property (nonatomic, strong) NSArray *subcategories;/**<分类子分类 */

@end
View Code

YKCity.h文件内容:

#import <Foundation/Foundation.h>

@interface YKCity : NSObject

@property (nonatomic, strong) NSString *name;/**<城市名 */
@property (nonatomic, strong) NSString *pinYin;/**<城市拼音 */
@property (nonatomic, strong) NSString *pinYinHead;/**<城市首字母 */
@property (nonatomic, strong) NSArray *regions;/**<来描述当前城市所有的区域 */

@end
View Code

YKRegion.h文件内容:

#import <Foundation/Foundation.h>

@interface YKRegion : NSObject

@property (nonatomic, strong) NSString *name;/**<区域的名字 */
@property (nonatomic, strong) NSArray *subregions;/**<区域的子区域 */

@end
View Code

写到这儿Model层基本上就算搭建完了,以后有需要再添加!

ps:刚用博客不久,想上传上文中的plist文件,可是不会上传文件,希望知道上传方法的朋友能在评论里告知,十分感谢!

原文地址:https://www.cnblogs.com/YKiOS/p/4873306.html