iOS开发——工厂模式

  工厂模式很好用,为表诚意,我直接搞个实用的例子放这,解析一个订单的数据,并且这个订单里面可能不止一件商品的做法。

  还是直接上代码,不懂的地方,再提出来。

  1.在MyOrderDeals.h文件中

#import <Foundation/Foundation.h>

 

@interface MyOrderDeals : NSObject

@property(nonatomic,strong) NSString *id;

@property(nonatomic,strong) NSString *orderID;

@property(nonatomic) float allMoney;

@property(nonatomic) float transportationCost;

@property(nonatomic,strong) NSMutableArray *goodsArr;

 

+(id)myOrderDealsWithDict:(NSDictionary*)dict;

 

@end

 

 

 

  2.在MyOrderDeals.m文件中

#import "MyOrderDeals.h"

 

#import "SingleOrderCellDeal.h"

 

@implementation MyOrderDeals

 

+(id)myOrderDealsWithDict:(NSDictionary *)dict {

    return [[self alloc] initWithDict:dict];

}

 

-(id)initWithDict:(NSDictionary*)dict{

    self = [super init];

    if (self) {

        self.id = dict[@"id"];

        self.orderID = dict[@"order_sn"];

        self.allMoney = [dict[@"pay_amount"] floatValue];

        self.transportationCost = [dict[@"delivery_fee"] floatValue];

        

        self.goodsArr = [NSMutableArray array];

        for (NSDictionary *singleDealDict in dict[@"goods"]) {

            SingleOrderCellDeal *deal = [SingleOrderCellDeal singleOrderDealCellWithDict:singleDealDict];

            [self.goodsArr addObject:deal];

        }

    }

    return self;

}

 

@end

 

 

  3.在SingleOrderCellDeal.h文件中

#import <Foundation/Foundation.h>

 

@interface SingleOrderCellDeal : NSObject

@property(nonatomic,strong) NSString *title;

@property(nonatomic) float currentPrice;

@property(nonatomic,strong) NSString *selledCount;

@property(nonatomic,strong) NSURL *imageURL;

 

+(id)singleOrderDealCellWithDict:(NSDictionary*)dict;

 

@end

 

 

 

  4.在SingleOrderCellDeal.m文件中

#import "SingleOrderCellDeal.h"

 

@implementation SingleOrderCellDeal

 

static NSString *imageURL = @"http://www.joinbuy.cn";

 

+(id)singleOrderDealCellWithDict:(NSDictionary *)dict {

    return [[self alloc] initWithDict:dict];

}

 

-(id)initWithDict:(NSDictionary*)dict{

    self = [super init];

    if (self) {

        self.title = dict[@"name"];

        self.currentPrice = [dict[@"unit_price"] floatValue];

        self.selledCount = dict[@"number"];

        self.imageURL = [NSURL URLWithString:[imageURL stringByAppendingString:dict[@"icon"]]];

    }

    return self;

}

 

@end

 

 

  5.在需要调用的地方

#import "MyOrderDeals.h"

#import "SingleOrderCellDeal.h"

@property(nonatomic,strong) NSMutableArray *myOrders;

-(NSMutableArray *)myOrders{

    if (!_myOrders) {

        _myOrders = [NSMutableArray array];

    }

    return _myOrders;

}

  在从服务器导下一组订单数据后

    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

        for (NSDictionary *dict in array) {

                MyOrderDeals *deal = [MyOrderDeals myOrderDealsWithDict:dict];

                [self.myOrders addObject:deal];

         }

 

  这样一来,从服务器导下来的订单数据,就都解析保存在myOrders这个数组中了,需要什么数据,只要取对应的值就行了。

转载于:https://www.cnblogs.com/yyt-hehe-yyt/p/5441426.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值