常用的第三方闻道丶iOS(尝鲜版)环境集成

iOS开发经验总结(持续更新中)

2017-03-26  本文已影响276人  Bestmer

本文会持续记录自己在学习、工作中,接触的和iOS开发相关的各种技术。包括写代码时容易忽视的细节问题,项目中接触到实用技术以及优秀的三方框架。欢迎收藏,点赞,若有不足欢迎各位指出来一起探讨,共同进步。

Section One — Coding Tips

1.为私有方法名加前缀


2.建议书写枚举模仿苹果——在列出枚举内容的同时绑定了枚举数据类型NSUInteger,这样带来的好处是增强的类型检查和更好的代码可读性,示例:

typedef NS_ENUM(NSUInteger, GPSectionType) {
    GPSectionTypeNone              = 0,
    GPSectionTypeNews              = 1 ,
    GPSectionTypeInformation      = 2,
};

3.头文件 #import的顺序

#import <系统库>
#import <第三方库>
#import “其他类”
#import<UIKit/UIkit.h>
#import<Google/Analytics.h>
#import"GPCustomView.h

4.@Class的写法

// 建议的写法
@class UIView, UIImage;
// 不建议的写法
@class UIView;
@class UIImage;

5.声明const的字符串


6.方法尽量控制最多五十行


7.注释一定要写

/**
 *  @brief 直播间发送礼物
 *
 *  @param gift     礼物
 *  @param groupId  直播间id
 *  @param count    数量
 *  @param success  成功回调
 *  @param failure  失败回调
 */
 
 + (void)readySendGiftWithGift:(MoerLiveRoomGift*)gift
                      groupId:(NSString*)groupId
                        count:(NSInteger)count
                      success:(void(^)(Goods *goods))success
                      failure:(Failure)failure;                
/**
 *  @brief 根据字典信息实例化订单
 *
 *  @param dictionary 字典信息
 *
 *  @return 订单实例
 */
-(instancetype)initWithDictionary:(NSDictionary*)dictionary;

/**
 *  打赏对象
 */
@property (nonatomic, copy) NSString *targetId;

typedef NS_ENUM(NSInteger, MoerGoodsType) {
    /**
     *  无
     */
    MoerGoodsTypeNone,
    /**
     *  文章
     */
    MoerGoodsTypeArticle = 1,
    /**
     *  活动
     */
    MoerGoodsTypeActivity = 2
}

8.头文件引入其他类的时候使用@class


9.多使用pragma mark


10.控件的命名规范


11.对于#define宏命名

#define NS_AVAILABLE_IOS(_ios) CF_AVAILABLE_IOS(_ios)

12.对于局部的变量尽量的初始化

//正确的写法
int index = 0;
//错误的写法
int index;

13.block的命名规范

// 建议的写法
typedef void(DidUpdateViewCompletionHandle)(void)
// 不建议的写法
typedef void(DidUpdateViewCallBack)

14.在dealloc方法中移除通知

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

15.属性要尽量使用懒加载


16.多使用字面量


17.为第三方类添加分类添加前缀

@interface UIView (GP_Add)
- (void)gp_addCustomView:(CustomView *)customView;
@end;

18.数组和字典最好指定元素的类型

NSArray<NSString *> *names = [NSArray array];

Section Two — Work Experience

1.建议加载xib,xib名称用NSStringFromClass(),避免书写错误

// 推荐写法
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GPTableViewCell class]) bundle:nil] forCellReuseIdentifier:ID];

// 不推荐写法
[self.tableView registerNib:[UINib nibWithNibName:@"GPTableViewCell" bundle:nil] forCellReuseIdentifier:ID];

2.自定义cell的时候,将重用标识符同cell文件绑定在一起,而不是在控制器里定义一堆重用标识符

#import "GPBaseCell.h"

UIKIT_EXTERN NSString * const GPContentCelltCellIdentifier;

@interface GPContentCell : GPBaseCell

@end

#import "GPContentCell.h"

NSString * const "GPContentCell = @"GPContentCell";

@interface GPContentCell ()
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@end

@implementation MoerQAAskerContentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentLabel.textColor = [UIColor redColor];
}

3.使用XIB创建自定义View

- (instancetype)initWithXib;
- (instancetype)initWithXib {
    self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
    // 初始化设置调整UI样式
    ...
    return self;
}

4.善于利用autoresizingMask技术


5.拉伸图片

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(5_0); // create a resizable version of this image. the interior is tiled when drawn.
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode

6.使用SDWebImage给button设置图片

// 第一步:导入头文件
#import <SDWebImage/UIButton+WebCache.h>
// 第二步:调用方法并设置占位图片
[self.avatarButton sd_setImageWithURL:[NSURL URLWithString:model.avatarUrlString] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"PlaceholderImage"]];

7.使用SDWebImage加载gif图片

// 第一步:导入头文件
#import "UIImage+GIF.h"
// 第二步:调用方法
    UIImage *hoopImage = [UIImage sd_animatedGIFNamed:@"hula_hoop"];
    self.hoopImageView = [[UIImageView alloc] initWithImage:hoopImage];
    self.hoopImageView.frame = CGRectMake(10, 10, 100, 100);
    [self.view addSubview:self.hoopImageView];

8.使用dSYM文件查找线上程序的crash日志





9.block的注意事项

if (self.backBlock) {
    self.backBlock(self.textView.text)
}


10.不要把本地调试的代码提交到git上

#ifdef DEBUG
    isDebug = YES;
#else
    isDebug = NO;
#endif

11.使用自定义标签警告


12.使用runtime关联对象


13.使用contentMode让图片长得好看


14.控件字体颜色设置相同时使用IBOutletCollection


15.修改状态栏字体颜色

// 首先写一个属性
@property (nonatomic, assign) UIStatusBarStyle statusBarStyle;
// 根据具体需求来修改状态栏的样式
self.statusBarStyle = UIStatusBarStyleLightContent;// 白色
self.statusBarStyle = UIStatusBarStyleDefault; // 黑色
// 告诉系统更新状态栏样式
[self setNeedsStatusBarAppearanceUpdate];
// 重写该方法,将新的样式返回给系统
- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.statusBarStyle;
}

16.关闭刷新section的动画效果

// 虽然动画效果选择的是UITableViewRowAnimationNone
// 但实际上还是会动画效果
 [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:GPSectionTypeNews] withRowAnimation:UITableViewRowAnimationNone];
[UIView performWithoutAnimation:^{
            [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:GPSectionTypeNews] withRowAnimation:UITableViewRowAnimationNone];
        }];

17.善于利用round/ceil/floorf函数

Example:如何值是3.4的话,则
3.4 -- round 3.000000
    -- ceil 4.000000
    -- floor 3.00000

18.APP内拨打电话

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://010-88888888"]];

#import <UIKit/UIWebView.h>

UIWebView * callWebview = [[UIWebView alloc]init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel:010-88888888"]]];
[[UIApplication sharedApplication].keyWindow addSubview:callWebview];
区别一:第一种会先跳出程序到系统的打电话程序,第二种是一直都在自己的app中运行,没有出去过。
区别二:第一种触发直接到打电话界面,第二种会先弹出一个对话框,可以选择打不打电话,对话框如下。

19.善于利用UIStackView


20.iOS中常用动画详解


21.使用NSTextAttachment实现图文混排

//     
    NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ ", article.title]];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment.image = [UIImage imageNamed:@"icon"];
    CGFloat offsetY = -1.0;
    textAttachment.bounds = CGRectMake(0, offsetY, textAttachment.image.size.width, textAttachment.image.size.height);
    NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [titleString insertAttributedString:textAttachmentString];

22.TextKit的学习


23.支付相关


24.关于dispatch_semaphore的使用


25.NSStringFromSelector(_cmd)说明




26.iOS方法警告

__deprecated_msg("方法废弃,请使用...替换")
__attribute__((visibility("警告")));
- (void)test __deprecated_msg("方法废弃,请使用...替换") __attribute__((visibility("方法废弃")));


27.友盟推送踩的坑

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
                  stringByReplacingOccurrencesOfString: @">" withString: @""]
                 stringByReplacingOccurrencesOfString: @" " withString: @""]);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


28.配置Gitlab


29.过滤掉字符串前后的空格

 NSString* trimedURL = [stringURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

Section Three — Third Party

1.一款非常强大的文字编辑器


2.实现顶部弹窗的HUD


3.非常好用的一款加载弹窗


4.每一个网络请求封装成对象的网络库


5.让响应手势变得更简单


6.让每一个试图控制器拥有独立的导航栏


7.绘制图表、折线图的框架


8.一个比UISegmentedControl更好用的东西


9.轮播图必备


10.tableView算高神器


11.超级强大的开源框架


12.小抽屉展示


13.侧滑抽屉


14.夜间模式


15.图片浏览器


16.HTML标签解析


Section Four — Development Tool

1.捕捉网络请求 — Charles


2.切换host的工具 — Gas Mask


3.高效的编辑器 — Subline Text


4.模拟发送网络请求 — Postman


5.图形化Git工具 — SourceTree


6.分析他人APP界面 — Reveal


7.翻墙必备 — Shadowsocks


8.快捷键太多记不住? — Cheetsheet


Section Five — About Git

很多人做iOS开发,可能先接触方便好用的Sourcetree,记住了合并代码的那几个步骤,然后才接触git。为了能更好的理解Sourcetree做的每一步,很有必要学习博大精深的git,以下内容为本人恶补git知识时的笔记,感谢廖雪峰老师写的git教程。



版本库(Repository)


添加到远程仓库


查看分支

切换分支(创建新分支)


合并指定分支(dev)到当前分支(master)


删除分支


查看分支图


分支管理策略


Bug分支


Feature分支


多人协作


标签管理


忽略文件


使用rebase而不是merge


最后

学而时习之,不亦说乎。

上一篇 下一篇

猜你喜欢

热点阅读