Updating to Swift 4.2 with Xcode 10 using related sources – onlinecode
In this post we will give you information about Updating to Swift 4.2 with Xcode 10 using related sources – onlinecode. Hear we will give you detail about Updating to Swift 4.2 with Xcode 10 using related sources – onlinecodeAnd how to use it also give you demo for it if it is necessary.
Swift 4.2 is a major release and shipped with Xcode 10. It comes with a lot of code improvements for which the best way to start is to watch the WWDC 2018: What’s New in Swift session. Some of the improvements are easy to implement in your existing code.
Replace strongSelf with self
SE-0079 makes it possible to upgrade self from a weak to a strong reference by allowing the use of optional binding. If you have a lot of unwraps like this:
guard let strongSelf = self else { return }
Then you can go for a find an replace to replace strongSelf
with self
.
Replacing // TODO: with the new #warning
You might have added a custom run script to show warnings for // TODO:
like explained in severalblog posts. Just like before in Objective-C, we can now replace those by simple #warning statements pointing out that we need to update that piece of code.
Iterating over all cases of an enum
Before Swift 4.2 it was hard to get all cases of an enum. Several solutions made it possible from which you might have implemented one of these in your project.
SE-0194 introduces a new CaseIterable
protocol to derive a Collection of all Enum Cases with a new static var allCases
.
enum CompassDirection: CaseIterable {
case north, south, east, west
}
print("There are (CompassDirection.allCases.count) directions.")
Generating random values
arc4random_uniform
is common and was used before Swift 4.2 to generate random values. With SE-0202 Random Unification it’s now a lot easier to generate a random integer:
Int.random(in: 1..<5)
a random boolean:
Bool.random()
a shuffled array:
let shuffledNumbers = [0, 1, 2, 3, 4].shuffled()
or to get a random element:
let bingoNumbers = [0, 1, 2, 3, 4, 5]
bingoNumbers.randomElement()
If your project uses randomness anywhere you might want to check whether these newly introduced methods can improve your existing code.
More changes in Swift 4.2
The above examples are just a piece of all the introduced changes. Ted Kremenek gives us a detailed overview of all the changes in his blog post Swift 4.2 Released!.
Apple’s guide in migrating
Apple makes it easy for you to migrate, especially when migrating from an older Swift version. Check out Migrating to Swift 4.2 or use the built-in migration tool which you can find under Edit -> Convert -> To Current Swift Syntax
.
Getting more out of Xcode 10
If you like to get the most out of Xcode 10, check out Enabling newly added opt-in features in Xcode 10 and Build performance analysing in Xcode 10
<!– Disable cross link to Medium
Also published on Medium.
–>
Hope this code and post will helped you for implement Updating to Swift 4.2 with Xcode 10 using related sources – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs