How Switching to Swift Boosted Our Mobile App’s Performance and Stability
This article explains why the Taobao mobile team migrated the product review list from Objective‑C to Swift, outlines Swift’s three main advantages—speed, safety, and expressiveness—shows concrete design‑pattern implementations, discusses the challenges faced during migration, and quantifies the resulting gains in code size, review efficiency, and crash reduction.
After a month‑and‑a‑half of technical refactoring and several months of iteration, the new Taobao product‑review list ran stably during the 2021 Double‑11 event, delivering clear business improvements and valuable technical insights such as a lightweight development framework based on DinamicX and event‑chain orchestration, as well as a migration from Objective‑C to Swift.
Why Choose Swift?
Swift is seen as the future language for Apple platforms, backed by strong community support (73 WWDC sessions covering syntax, design, performance, and toolchains) and a robust type system that prevents many runtime crashes.
Three Core Advantages of Swift
Faster : Optimized system libraries reduce memory allocation, reference‑count overhead, and method dispatch costs.
Safer : A strong type system eliminates unsafe pointer usage; optional types force developers to handle nil values explicitly, reducing crashes.
More Expressive : Less code is needed to represent the same logic—Swift Evolution has added over 330 proposals to improve expressiveness, shrinking code size by 30‑50% compared with Objective‑C.
Builder Pattern Example (Objective‑C)
// OCDemoModelBuilder.h
@interface OCDemoModelBuilder : NSObject
@property (nonatomic, copy, nonnull) NSString *a;
@property (nonatomic, copy, nonnull) NSString *b;
@property (nonatomic, copy, nonnull) NSString *c;
@property (nonatomic, copy, nonnull) NSString *d;
@property (nonatomic, copy, nonnull) NSString *e;
@property (nonatomic, copy, nonnull) NSString *f;
@property (nonatomic, copy, nonnull) NSString *g;
@property (nonatomic, copy, nonnull) NSString *h;
@end
// OCDemoModelBuilder.m
@implementation OCDemoModelBuilder
- (instancetype)init {
if (self = [super init]) {
_a = @"a"; _b = @"b"; _c = @"c"; _d = @"d";
_e = @"e"; _f = @"f"; _g = @"g"; _h = @"h";
}
return self;
}
@end
// OCDemoModel.h
@interface OCDemoModel : NSObject
@property (nonatomic, readonly, nonnull) NSString *a;
@property (nonatomic, readonly, nonnull) NSString *b;
@property (nonatomic, readonly, nonnull) NSString *c;
@property (nonatomic, readonly, nonnull) NSString *d;
@property (nonatomic, readonly, nonnull) NSString *e;
@property (nonatomic, readonly, nonnull) NSString *f;
@property (nonatomic, readonly, nonnull) NSString *g;
@property (nonatomic, readonly, nonnull) NSString *h;
- (instancetype)initWithBuilder:(void(^)(OCDemoModelBuilder *builder))builderBlock;
@end
// OCDemoModel.m
@implementation OCDemoModel
- (instancetype)initWithBuilder:(void(^)(OCDemoModelBuilder *builder))builderBlock {
if (self = [super init]) {
OCDemoModelBuilder *builder = [[OCDemoModelBuilder alloc] init];
if (builderBlock) { builderBlock(builder); }
_a = builder.a; _b = builder.b; _c = builder.c; _d = builder.d;
_e = builder.e; _f = builder.f; _g = builder.g; _h = builder.h;
}
return self;
}
@end
// Usage
OCDemoModel *ret = [[OCDemoModel alloc] initWithBuilder:^(OCDemoModelBuilder * _Nonnull builder) {
builder.b = @"b1";
}];Swift Struct Simplifies Builder
struct SwiftDemoModel {
var a = "a"
var b = "b"
var c = "c"
var d = "d"
var e = "e"
var f = "f"
var g = "g"
var h = "h"
}
// Usage
let ret = SwiftDemoModel(b: "b1")State Pattern Example
/// Executable.h
@protocol Executable <NSObject>
- (nullable NSDictionary *)toFormattedData;
@end
/// OCDemoExecutedResult.h
@interface OCDemoExecutedResult : NSObject <Executable>
+ (OCDemoNoneResult *)none;
+ (OCDemoFinishedResult *)finishedWithData:(nullable NSDictionary *)data type:(nullable NSString *)type;
+ (OCDemoFailureResult *)failureWithErrorCode:(nonnull NSString *)errorCode errorMsg:(nonnull NSString *)errorMsg userInfo:(nullable NSDictionary *)userInfo;
@endFacade Pattern Example
@protocol JSONDecodable <NSObject>
@end
@protocol JSONEncodable <NSObject>
@end
@protocol XMLDecodable <NSObject>
@end
@protocol XMLEncodable <NSObject>
@end
- (void)decodeJSON:(id<JSONCodable>)json xml:(id<XMLCodable>)xml {
// implementation
}Challenges Encountered
API not adapted for Swift : Auto‑generated Swift bridges often violate API Design Guidelines, leading to unsafe optional handling.
Unsafe APIs : Missing optional annotations cause implicit‑unwrap crashes.
Destructive inheritance : Subclassing NSArray and returning nil breaks Swift’s expectations.
Clang module build errors : Mixed‑module dependencies require careful module flag configuration.
Dependency update lag : Some internal libraries lacked module support, requiring version bumps.
Module name clashes : Modules whose name matches a class (e.g., STDPop) cause ambiguous imports.
Solutions
We added Swift‑friendly extensions, created wrapper classes (e.g., DemoEventHandlerBox) to isolate unsafe Objective‑C APIs, enforced module definitions, and updated dependencies to module‑compatible versions. We also introduced a middle‑layer to bridge Swift and Objective‑C without forcing all OC modules to enable Clang modules.
Benefits
Code size reduced by 30‑50%, leading to faster development cycles.
Cross‑team code review effort decreased because Swift and Kotlin share similar syntax.
Crash rate dropped dramatically (online crash rate ~0.00008% for a pure‑Swift app).
Feature iteration time shortened by ~25% compared with an OC/Java stack.
Conclusion and Outlook
Adopting Swift for the product‑review list proved beneficial for performance, safety, and developer productivity. Future work includes establishing Swift coding standards, expanding Swift layers in core SDKs, and leveraging Apple’s new tools (WidgetKit, DocC) to further improve the ecosystem.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
