My Five Favorite Things About Swift

Jiakan Wang
Tue May 11 2021
IOS-APP-DEVELOPMENT SWIFT-PROGRAMMING SWIFT
CC Attribution: https://www.flickr.com/photos/evarinaldiphotography/6966830273/

Statsig strives to empower all developers to ship features faster, so naturally one of the first milestones for us was to provide an SDK for iOS development. I started doing iOS development at Facebook, which only used Objective-C for its iOS apps. But given Swift’s growing popularity (Apple’s own adoption has been growing quickly), we decided to offer and write our SDK natively in Swift. So I set off to learn Swift from scratch while working on the SDK, and I ended up loving it! Here are my five favorite things about Swift so far:

1. Swift is much more readable

I started off reading some open source projects, and I was able to understand everything without any prior knowledge of the language. The syntax is much closer to a language like JavaScript or Java than it is to Objective-C, which is great news for new iOS developers. For example, instead of concatenating strings like this:

NSString *firstMsg = @”Concatenating strings in Objective-C “;
NSString *secondMsg = @”is hard”;
NSString *fullMsg = [NSString stringWithFormat:@”%@%@”, firstMsg, secondMsg];

we can now do this:

let firstMsg = “Concatenating strings in Swift “
let secondMsg = “is EAZY”
let fullMsg = firstMsg + secondMsg

2. Swift supports modern language features

There are many handy features that I wish existed in Objective-C. While writing the SDK, I found out that Swift supports many features that Objective-C did not. For example:

  • optional parameters and labels
  • (true) generics
  • optional chaining

These allowed me to not only write cleaner code myself, but also to pass the benefits onto our users because I am able to create more user-friendly function signatures in our SDK.

3. No more header files!

Needless to say, this is awesome! I’m also pleasantly surprised by how little I missed them — in fact I didn’t even realize it until I was almost done with the core SDK features…

4. Some nice quirks that I didn’t know I wanted

Swift introduced some nice quirks that I found strange at first but soon started using everywhere. Let’s say you are writing a function that does something with a vehicle’s model year (vehicle.model.year) while everything can be nil, you can use the new keyword and comma-separated if statements to get the edge cases out of the way first like this:

guard let vehicle = vehicle,
let model = vehicle.model,
let modelYear = model.year else {
// if vehicle, model, or modelYear is nil, we return nil here
return nil
}
// do something with modelYear now that it is not nil for sure

And I especially love this one line of code you can use to capture weak self in a closure:

guard let self = self else { return }

5. Easy to port to Objective-C

There are lots of applications written in Objective-C still, so we need to provide an SDK for Objective-C users as well. Fortunately, Swift provides an easy way for us to simply write a wrapper on top of the public classes and functions we need to expose to Objective-C code. For a class like this in Swift:

public class Statsig {
public static func start(sdkKey: String) {
...
}
}

You can just create a new class like this:

@objc(Statsig)
public class ObjcStatsig: NSObject {
@objc public static func start(withSDKKey: String) {
Statsig.start(sdkKey: withSDKKey)
}
}

Once the framework is imported, your Objective-C code will be able to call [Statsig startWithSDKKey:@"my_sdk_key"];! Note that @objc implies which class, member, or function to be converted, and I'm renaming the class name from ObjcStatsig to Statsig when exposed to Objective-C with the (Statsig) after @objc. I also named the parameter "withSDKKey" to follow Objective-C naming convention, but if you don't, Xcode will automatically prepend a "with" prefix to your first parameters in the bridging headers. Pretty neat, isn't it?

Unfortunately, this took me quite a while to figure out: even though it’s painless to implement, the documentation online for this is pretty lacking. If you want to see more of how we pull this off, check out our GitHub repo. Or, if you’re looking for a feature flag/experimentation framework for your iOS App, then feel free to integrate Statsig in your Objective-C or Swift codebase!

What do YOU love (or hate) about Swift?


Try Statsig Today

Explore Statsig’s smart feature gates with built-in A/B tests, or create an account instantly and start optimizing your web and mobile applications. You can also schedule a live demo or chat with us to design a custom package for your business.

MORE POSTS

Recently published

My Summer as a Statsig Intern

RIA RAJAN

This summer I had the pleasure of joining Statsig as their first ever product design intern. This was my first college internship, and I was so excited to get some design experience. I had just finished my freshman year in college and was still working on...

Read more

Long-live the 95% Confidence Interval

TIMOTHY CHAN

The 95% confidence interval currently dominates online and scientific experimentation; it always has. Yet it’s validity and usefulness is often questioned. It’s called too conservative by some [1], and too permissive by others. It’s deemed arbitrary...

Read more

Realtime Product Observability with Apache Druid

JASON WANG

Statsig’s Journey with Druid This is the text version of the story that we shared at Druid Summit Seattle 2022. Every feature we build at Statsig serves a common goal — to help you better know about your product, and empower you to make good decisions for...

Read more

Quant vs. Qual

MARGARET-ANN SEGER

💡 How to decide between leaning on data vs. research when diagnosing and solving product problems Four heuristics I’ve found helpful when deciding between data vs. research to diagnose + solve a problem. Earth image credit of Moncast Drawing. As a PM, data...

Read more

The Importance of Default Values

TORE

Have you ever sent an email to the wrong person? Well I have. At work. From a generic support email address. To a group of our top customers. Facepalm. In March of 2018, I was working on the games team at Facebook. You may remember that month as a tumultuous...

Read more
ANNOUNCEMENT

CUPED on Statsig

CRAIG

Run experiments with more speed and accuracy We’re pleased to announce the rollout of CUPED for all our customers. Statsig will now automatically use CUPED to reduce variance and bias on experiments’ key metrics. This gives you access to a powerful experiment...

Read more

We use cookies to ensure you get the best experience on our website.

Privacy Policy