赞
踩
毫无疑问,Swift是大势所趋,我认为,相比于OC,Swift更适用于未来
从 Chris Lattner 开始设计 Swift 到现在已经十年过去了,每年都有逐渐完善。现在已经成为了构成了一个特别玩完善的Apple开发生态,期间,WWDC对外输出的内容近百。
首先通过 TIOBE排行榜,我们就可以了解到,swift现在的受欢迎程度,一直都是上升趋势,现在已经排名第10名,而OC已经排在 20 名之外了。
从社区的语言排行榜来看,虽然乍一看,感觉还是 Swift 和 Objective-C 共存的大环境,但其实背后也反映出,Swift 已经被大部分开发者所接受了。
Swift再执行效率上边做了很多优化,比如:系统库有很多不需要引用计数的基础类型,
所谓的安全
不等于不发生Crash, 而是指任何的输入都有一个比较明确的表现定义, Swift 是一门类型安全的语言。鼓励程序员在代码中清楚明确值的类型。如果代码中使 用一个字符串 String,那么你不能错误地传递一个整型 Int 给它。因为 Swift 是类型安 全的,它会在代码编译的时候做类型检查,并且把所有不匹配的类型作为一个错误标记 出来。这样使得程序员在开发中尽可能早地发现和修正错误。
而 Objective-C 则不然,你声明一个 NSString 变量,仍然可以传一个 NSNumber 给 它,尽管编译器会抱怨,但是你仍然可以作为 NSNumber 来使用它。
由于我司最新代码使用了Swift,所以在平时的代码review中,对于这种类型错误可以节省人力,而且在后期的crash率,有很大的降低。
更具表达性就是说用更少的代码表达一段完整的逻辑, 整体来说,Swift的代码量比OC少了大概 30% - 50%。下方链接有很多增强Swift的表达式,可以参考。https://apple.github.io/swift-evolution/
Builder Pattern
我们定义了很多属性的复杂model时,不想这个model的属性在初始化之后被改变,我们就需要通过建造者模式来解决,代码如下:
// OCHMGModelBuilder.h
@interface OCHMGModelBuilder : NSObject
@property (nonatomic, nonnull) NSString *one;
@property (nonatomic, nonnull) NSString *two;
@property (nonatomic, nonnull) NSString *three;
@property (nonatomic, nonnull) NSString *four;
@property (nonatomic, nonnull) NSString *five;
@property (nonatomic, nonnull) NSString *six;
@property (nonatomic, nonnull) NSString *seven;
@property (nonatomic, nonnull) NSString *eight;
@property (nonatomic, nonnull) NSString *nine;
@property (nonatomic, nonnull) NSString *ten;
@end
// OCHMGModelBuilder.m
@implementation OCHMGModelBuilder
- (instancetype)init {
if (self = [super init]) {
_one = @"one";
_two = @"two";
_three = @"three";
_four = @"four";
_five = @"five";
_six = @"six";
_seven = @"seven";
_eight = @"eight";
_nine = @"nine";
_ten = @"ten";
}
return self;
}
@end
// OCHMGDemo.h
@interface OCHMGDemo : NSObject
@property (nonatomic, readonly, nonnull) NSString *one;
@property (nonatomic, readonly, nonnull) NSString *two;
@property (nonatomic, readonly, nonnull) NSString *three;
@property (nonatomic, readonly, nonnull) NSString *four;
@property (nonatomic, readonly, nonnull) NSString *five;
@property (nonatomic, readonly, nonnull) NSString *six;
@property (nonatomic, readonly, nonnull) NSString *seven;
@property (nonatomic, readonly, nonnull) NSString *eight;
@property (nonatomic, readonly, nonnull) NSString *nine;
@property (nonatomic, readonly, nonnull) NSString *ten;
- (instancetype)initWithBuilder:(void(^)(OCHMGModelBuilder *builder))builderBlock;
@end
// OCHMGDemo.m
@implementation OCHMGDemo
- (instancetype)initWithBuilder:(void (^)(OCHMGModelBuilder * _Nonnull))builderBlock {
if (self = [super init]) {
OCHMGModelBuilder *builder = [[OCHMGModelBuilder alloc] init];
if (builderBlock) {
builderBlock(builder);
}
_one = builder.one;
_two = builder.two;
_three = builder.three;
_four = builder.four;
_five = builder.five;
_six = builder.six;
_seven = builder.seven;
_eight = builder.eight;
_nine = builder.nine;
_ten = builder.ten;
}
return self;
}
@end
//ViewController.m 中使用
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
OCHMGDemo *demo = [[OCHMGDemo alloc] initWithBuilder:^(OCHMGModelBuilder * _Nonnull builder) {
builder.two = @"外面修改的Two";
}];
// demo = one、外面修改的Two、three、four、five、six...ten
}
@end
但是Swift的struct支持属性的默认初始值和初始化构造器,使得上方的构造器意义不大,如下:
struct SwiftHMGDemo {
var one = "one"
var two = "two"
var three = "three"
var four = "four"
var five = "five"
var six = "six"
var seven = "seven"
var eight = "eight"
var nine = "nine"
var ten = "ten"
}
// 使用
func use() {
let swiftDemo = SwiftHMGDemo(two: "外面修改的Two")
}
// swiftDemo = one、外面修改的Two、three、four、five、six...ten
Facade Pattern
当我们定义了一个方法需要传入的属性,该属性符合多个协议类型的时候,我们就用Facade Pattern来解决。
例如我们有四个协议 JSONDecodable
、JSONEncodable
、XMLDecodable
、XMLEncodable
以及一带有两个入参的方法,入参 1 为 json 要求同时满足 JSONDecodable
、JSONEncodable
两个协议,入参 2 为 xml 同时满足 XMLDecodable
、XMLEncodable
。当我们使用 OC 来解决问题时通常会这么写
@protocol JSONDecodable <NSObject>
@end
@protocol JSONEncodable <NSObject>
@end
@protocol XMLDecodable <NSObject>
@end
@protocol XMLEncodable <NSObject>
@end
@protocol JSONCodable <JSONDecodable, JSONEncodable>
@end
@protocol XMLCodable <XMLDecodable, XMLEncodable>
@end
- (void)decodeJSON:(id<JSONCodable>)json xml:(id<XMLCodable>)xml {
}
可以看出,我们是额外定义了两个协议 JSONCodable
,XMLCodable
来解决的问题,但是在Swift中,我们可以使用&来解决这个问题,不需要定义额外的类型:
protocol JSONDecodable {}
protocol JSONEncodable {}
protocol XMLDecodable {}
protocol XMLEncodable {}
func decode(json: JSONDecodable & JSONEncodable, xml: XMLDecodable & XMLEncodable) {
}
上方就是介绍了 为什么Swift更具表达式。
Swift代码量减少,投入的人力也会减少,整体会提效。
由于Android的Kotlin和iOS的Swift语法十分相似,那么在review代码的时候,安卓的和iOS的可以互相review,这样的话能够更加保证代码的质量。那么这种交叉review会给整个团队带来非常大的收益。
https://mp.weixin.qq.com/s?__biz=MzA5MTM1NTc2Ng
https://mp.weixin.qq.com/s?__biz=MzAxNDEwNjk5OQ
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。