IOS项目搭建

 

 

首先是目录结构,参考了网上普遍的一种MVC的目录结构

1677581-3b75be52f913eacd.jpg

1529473878960.jpg

  • Http:存放的是网络请求基类和一些网络数据模型类
  • Class:放的是ViewController、View、Model、ViewModel(ViewModel里面只存放了网络数据的一些处理,不属于真正的ViewModel)
  • Expand:实现了一些扩展类和工具类
    • UserDefaults:封装了UserDefault方法
    • Account:单例保存了一些基本的用户信息
    • RouterManager:实现路由跳转
    • Tool:工具类
    • Category:分类
    • PCH:.pch文件全局文件配置
    • Macro:常用宏定义
  • Vendor:简单控件类
  • Resource:资源类
  • Base:基类
  • AppDelegate:AppDelegate类

详细说一下工程中用到的一些知识点

网络请求基类参考了网上的AFNetworking的封装,具体实现可以百度,网上有很多例子

/**
 * GET:获取资源,不会改动资源
 * POST:创建记录
 * PATCH:改变资源状态或更新部分属性
 * PUT:更新全部属性
 * DELETE:删除资源
 */
typedef NS_ENUM(NSUInteger, HTTPMethod) {
    
    GET,
    POST,
    PUT,
    DELETE,
};



@interface NetworkManager : NSObject
+ (nonnull instancetype)sharedInstance;

- (void)sendAuthorization:(NSString *)key;
#pragma mark 常用网络请求方式
/**
 常用网络请求方式
 
 @param requestMethod 请求方试
 @param apiPath 方法的链接
 @param parameters 参数
 @param progress 进度
 @param success 成功
 @param failure 失败
 */
- (void)sendRequestMethod:(HTTPMethod)requestMethod
                                             apiPath:(nonnull NSString *)apiPath
                                          parameters:(nullable id)parameters
                                            progress:(nullable void (^)(NSProgress * _Nullable progress))progress
                                             success:(nullable void(^) (BOOL isSuccess, id _Nullable responseObject))success
                                             failure:(nullable void(^) (NSString * _Nullable errorMessage))failure ;

- (void)sendRequestMethod:(HTTPMethod)requestMethod
              showLoading:(BOOL)isShow
                  apiPath:(nonnull NSString *)apiPath
               parameters:(nullable id)parameters
                 progress:(nullable void (^)(NSProgress * _Nullable progress))progress
                  success:(nullable void(^) (BOOL isSuccess, id _Nullable responseObject))success
                  failure:(nullable void(^) (NSString * _Nullable errorMessage))failure ;
#pragma mark POST 上传图片
/**
 上传图片
 
 @param apiPath 方法的链接
 @param parameters 参数
 @param imageArray 图片
 @param width 图片宽度
 @param progress 进度
 @param success 成功
 @param failure 失败
 */
- (void)sendPOSTRequestWithApiPath:(nonnull NSString *)apiPath
                                             parameters:(nullable id)parameters
                                             imageArray:(NSArray *_Nullable)imageArray
                                            targetWidth:(CGFloat )width
                                               progress:(nullable void (^)(NSProgress * _Nullable progress))progress
                                                success:(nullable void(^) (BOOL isSuccess, id _Nullable responseObject))success
                                                failure:(nullable void(^) (NSString *_Nullable error))failure ;

@end

然后我处理网络数据的使用的是YYModel,为什么用这个,因为这个转换快,使用方便,网上有对比其他几款model转换,YYModel处理速度是最快的,而且容错率也是最高的,反正我用了之后感觉非常方便。
以下是用到的常用的转换函数,需要写在.m文件中。

+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper{
    return @{@"Id" : @"id",
             };
}
+ (nullable NSDictionary<NSString *, id> *)modelContainerPropertyGenericClass{
    return @{@"richTextList" : [DAHttpRichTextModel class],
             };
}

说一下路由跳转,路由跳转参考了git上面 DCURLRouter,十分方便也不需要引入头文件了

#pragma mark --------  push控制器 --------
+ (void)pushURLString:(NSString *)urlString animated:(BOOL)animated;
+ (void)pushURLString:(NSString *)urlString query:(NSDictionary *)query animated:(BOOL)animated;
+ (void)pushURLString:(NSString *)urlString query:(NSDictionary *)query callBack:(RouterCallBack)callBack animated:(BOOL)animated;
#pragma mark --------  present控制器 --------
+ (void)presentURLString:(NSString *)urlString animated:(BOOL)animated completion:(void (^ __nullable)(void))completion;
+ (void)presentURLString:(NSString *)urlString query:(NSDictionary *)query animated:(BOOL)animated completion:(void (^ __nullable)(void))completion;

#pragma mark --------  pop控制器 --------
/** pop掉一层控制器 */
+ (void)popViewControllerAnimated:(BOOL)animated;
/** pop掉两层控制器 */
+ (void)popTwiceViewControllerAnimated:(BOOL)animated;
/** pop掉times层控制器 */
+ (void)popViewControllerWithTimes:(NSUInteger)times animated:(BOOL)animated;
/** pop到根层控制器 */
+ (void)popToRootViewControllerAnimated:(BOOL)animated;

#pragma mark --------  dismiss控制器 --------
/** dismiss掉1层控制器 */
+ (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion;
/** dismiss掉2层控制器 */
+ (void)dismissTwiceViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion;
/** dismiss掉times层控制器 */
+ (void)dismissViewControllerWithTimes:(NSUInteger)times animated: (BOOL)flag completion: (void (^ __nullable)(void))completion;
/** dismiss到根层控制器 */
+ (void)dismissToRootViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion;

+ (void)setRootController:(UIViewController *)viewController;
- (UINavigationController*)currentNavigationViewController;
- (UIViewController*)currentViewController;

还写了一个viewController的分类,作用是运用runtime传参,*注意使用这个方法的时候参数不能使用驼峰,因为这个首字母大写的方法会把后面的字符都转成小写。

- (void)setParameters:(NSDictionary *)parameters {
    objc_setAssociatedObject(self, &parametersKey, parameters, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    NSArray *props = [self getAllProperties];
    //    NSLog(@"所有属性 = %@",props);
    [parameters enumerateKeysAndObjectsUsingBlock:^(NSString *key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if ([props containsObject:key]) {
            SEL setSel = [self creatSetterWithPropertyName:key];
            //            NSLog(@"所有方法 = %@", [self getAllMethods]);
            [self performSelectorOnMainThread:setSel withObject:obj waitUntilDone:[NSThread isMainThread]];
        }
    }];
}

- (NSDictionary *)parameters {
    return objc_getAssociatedObject(self, &parametersKey);
}

- (void)setCallback:(RouterCallBack)callback {
    objc_setAssociatedObject(self, &callBackKey, callback, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (RouterCallBack)callback {
    return objc_getAssociatedObject(self, &callBackKey);
}



//获取所有属性的字符串形式
- (NSArray *)getAllProperties
{
    u_int count;
    objc_property_t *properties  = class_copyPropertyList([self class], &count);
    NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++)
    {
        const char* propertyName =property_getName(properties[i]);
        [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
    }
    free(properties);
//    NSLog(@"所有属性值 %@",propertiesArray);
    
    return propertiesArray;
}

- (NSArray*)getAllMethods {
    unsigned int methCount = 0;
    Method *meths = class_copyMethodList([self class], &methCount);
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:methCount];
    for(int i = 0; i < methCount; i++) {
        
        Method meth = meths[i];
        
        SEL sel = method_getName(meth);
        
        const char *name = sel_getName(sel);
        
        [array addObject:[NSString stringWithCString:name encoding:NSUTF8StringEncoding]];
    }
    
    free(meths);
    return array;
}

#pragma mark -- 通过字符串来创建该字符串的Setter方法,并返回
- (SEL) creatSetterWithPropertyName: (NSString *) propertyName{
    //1.首字母大写
    propertyName = propertyName.capitalizedString;
    //2.拼接上set关键字
    propertyName = [NSString stringWithFormat:@"set%@:", propertyName];
    //3.返回set方法
//    NSLog(@"所有set方法 %@",propertyName);
    
    return NSSelectorFromString(propertyName);
}

分享一下工具类中的内容

@interface DAUITool : NSObject

+ (void) registerTableView:(UITableView *)tableView name:(NSString *)cellName;
+ (void)setExtraCellLineHidden: (UITableView *)tableView;
+ (void)registerCollectionView:(UICollectionView *)collectionView name:(NSString *)cellName;


+ (void)setContentInsetAdjustment:(UIScrollView *)scrollView viewController:(UIViewController *)vc;
+ (CGFloat)getContentHeight:(NSString*)content width:(CGFloat)width fontSize:(CGFloat)size;


/**
 设置阴影

 @param view view
 */
+ (void)setShadow:(UIView *)view;

/**
 设置渐变色

 @param view 当前view
 @param isUpToBottom YES设置颜色从上往下 NO 从左往右还是
 */
+ (void)setChangeColor:(UIView *)view direction:(BOOL)isUpToBottom;

/**
 毛玻璃

 @param view view
 */
+ (void)setGlassBackground:(UIView *)view;
@end

我的颜色都放在了color分类中

1677581-3bfedba3a231e90f.jpg

1529480889782.jpg

我的Base文件夹中存放都是常用的控件

 

1677581-4d09f394149623f5.jpg

1529481041900.jpg

 

AppDelegate可以用分类,就显的整洁很多

 

1677581-3b1a028f9c0fd0d7.jpg

1529481223458.jpg

 

1677581-07b389ceb48a3b63.jpg

1529481219378.jpg

最后一点:大图片直接拖到工程里,因为imageNamed:会缓存。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值