新浪微博开发之二十一(微博模型)

来源:互联网 发布:js input 选中 编辑:程序博客网 时间:2024/06/14 19:52
//
//  MyUserModel.h
//  新浪微博
//
//  Created by jose on 15-3-31.
//  Copyright (c) 2015年 jose. All rights reserved.
//  用户模型


#import <Foundation/Foundation.h>


@interface MyUserModel : NSObject
/** string 用户昵称 */
@property(nonatomic,copy)NSString *name;
/** string 用户头像地址(50x50)*/
@property(nonatomic,copy)NSString *profile_image_url;
/** 用户会员的等级 */
@property(nonatomic,assign)int mbrank;
/** 用户的会员类型 */
@property(nonatomic,assign)int mbtype;
/** 判断是否为会员 */
@property(nonatomic,assign,getter=isvip)BOOL vip;
//字典转模型
+(instancetype)UserModelWithDict:(NSDictionary *)dict;

@end


*****************************************************************************************************************************************

********************************************************************************************************************************************

**********************************************************************************************************************************************‘

//
//  MyUserModel.m
//  新浪微博
//
//  Created by jose on 15-3-31.
//  Copyright (c) 2015年 jose. All rights reserved.
//


#import "MyUserModel.h"


@implementation MyUserModel
//字典转模型
+(instancetype)UserModelWithDict:(NSDictionary *)dict{
    MyUserModel *model=[[MyUserModel alloc]init];
    model.name=dict[@"name"];
    model.profile_image_url=dict[@"profile_image_url"];
    model.mbrank=(int)dict[@"mbrank"];
    model.mbtype=(int)dict[@"mbtype"];
    return model;
}


//判断是否为会员
-(BOOL)isvip{
     //如果大于2就是会员
    return _mbtype>2;
}


@end



****************************************************************************************************************************************************************

******************************************************************************************************************************************************************

*******************************************************************************************************************************************************************

//微博模型

//
//  MyWeiboModel.h
//  新浪微博
//
//  Created by jose on 15-3-31.
//  Copyright (c) 2015年 jose. All rights reserved.
//  微博模型


#import <Foundation/Foundation.h>
#import "MyUserModel.h"


@interface MyWeiboModel : NSObject
/** string 微博创建时间 */
@property(nonatomic,copy)NSString *created_at;
/** string 字符串型的微博id */
@property(nonatomic,copy)NSString *idstr;
/** string 微博信息内容 */
@property(nonatomic,copy)NSString *text;
/** string 微博来源 */
@property(nonatomic,copy)NSString *source;
/** object 微博作者的模型 */
@property(nonatomic,strong)MyUserModel *user;
/** object 转发微博的模型 */
@property(nonatomic,strong)MyWeiboModel *retweeted_status;
/** int 转发数 */
@property(nonatomic,assign)int reposts_count;
/** int 评论数 */
@property(nonatomic,assign)int comments_count;
/** int 表态数 */
@property(nonatomic,assign)int attitudes_count;
/** object 微博配图地址 无图返回[]数组都是MyPictureTool模型 */
@property(nonatomic,copy)NSArray *pic_urls;
//字典转模型
+(instancetype)WeiboModelWithDict:(NSDictionary *)Dict;
@end


********************************************************************************************************************************

*********************************************************************************************************************************

********************************************************************************************************************************

//
//  MyWeiboModel.m
//  新浪微博
//
//  Created by jose on 15-3-31.
//  Copyright (c) 2015年 jose. All rights reserved.
//


#import "MyWeiboModel.h"
#import "MyUserModel.h"
#import "NSDate+MyNSDate.h"
@implementation MyWeiboModel
//字典转模型
+(instancetype)WeiboModelWithDict:(NSDictionary *)Dict{
    MyWeiboModel *model=[[MyWeiboModel alloc]init];
    model.text=Dict[@"text"];
    //微博作者模型
    model.user=[MyUserModel UserModelWithDict:Dict[@"user"]];
    model.created_at=Dict[@"created_at"];
    model.source=Dict[@"source"];
    model.idstr=Dict[@"idstr"];
    model.comments_count=(int)Dict[@"comments_count"];
    model.reposts_count=(int)Dict[@"reposts_count"];
    model.attitudes_count=(int)Dict[@"attitudes_count"];
    //转发微博
    NSDictionary *retweetedDict=Dict[@"retweeted_status"];
    if (retweetedDict) {
        //转发的微博进行字典转模型
        model.retweeted_status=[MyWeiboModel WeiboModelWithDict:retweetedDict];
    }
    return model;
}


//重写方法
-(NSString *)created_at{
    //_created_at = @"Mon Jul 14 16:13:07 +0800 2015";
    NSDateFormatter *fmt=[[NSDateFormatter alloc]init];
    fmt.dateFormat=@"EEE MMM dd HH:mm:ss Z yyyy";
    //微博创建时间
    NSDate *createtime=[fmt dateFromString:_created_at];
    if (createtime.IsThisYear) {
        if (createtime.IsToday) {
            //获取时间差
            NSDateComponents *cmps=[createtime DeltaWithNow];
            if (cmps.hour>=1) {
                return [NSString stringWithFormat:@"%d小时前",(int)cmps.hour];
            }
            else if (cmps.minute>=1){
                return [NSString stringWithFormat:@"%d分钟前",(int)cmps.minute];
            }
            else{
                return @"刚刚";
            }
        }
        else if (createtime.IsYesterday){
            //时分格式
            fmt.dateFormat=@"昨天 HH:mm";
            return [fmt stringFromDate:createtime];
        }
        else{
            //月日时分格式
            fmt.dateFormat=@"MM-dd HH:mm";
            return [fmt stringFromDate:createtime];
        }
    }
    else{
        //年月日格式
        fmt.dateFormat=@"yyyy-MM-dd";
        return [fmt stringFromDate:createtime];
    }
}




/****重写方法
-(NSString *)source{
    // _source == <a href="http://weibo.com/" rel="nofollow">微博 weibo.com</a>
    //截取范围
    NSRange range;
    range.location=[_source rangeOfString:@">"].location+1;
    range.length=[_source rangeOfString:@"</"].location-range.location;
    //开始截取
    NSString *subsource=[_source substringWithRange:range];
    return [NSString stringWithFormat:@"来自%@",subsource];
} ***/


//重写set方法
-(void)setSource:(NSString *)source{
    NSRange range;
    range.location=[source rangeOfString:@">"].location+1;
    range.length=[source rangeOfString:@"</"].location-range.location;
    NSString *subsource=[source substringWithRange:range];
    _source=[NSString stringWithFormat:@"来自%@",subsource];
}




//    _source = [NSString stringWithFormat:@"来自%@",subSource];


@end

0 0