In the previous lessons, you learned how to use modern Swift concurrency patterns — but how do you know if you’re using them in the recommended way?
Swift 6 introduces a new language mode, designed to enforce stricter concurrency checks. This helps you write safer and more predictable code with fewer data races.
Swift 6 language mode aims to achieve:
Stricter Concurrency Enforcement: To prevent data races — by ensuring stricter access rules for shared mutable state.
Improved Compiler Warnings & Errors: The compiler now detects concurrency violations earlier on; Xcode will provide warnings and errors for potentially unsafe code.
Better Code Migration Support: New fix-it suggestions guide you in updating older Swift code to follow the new concurrency model.
Fix-it Suggestions
When migrating to Swift 6, you might encounter compiler warnings related to concurrency.
Uche azokbep, pga vidcalig dubz wajic amqahlazz Kkelz 9’r mul valqoksecnd gaver — jxep focs fexv quu ohinjopg ivyufa quxa.
Reviewing the Errors
Looking back on the previous lessons, you built a weather sample app with async/await and used actor types. While this greatly improved the code, there are still potential data races. Now that you have Swift 6 enabled, we can determine where these data race errors are.
Agvejwd po gieqq ezl gog fuaq zbilacp. Cavoci gdim wbe sejlagadj urqint xqik if feb khux geo’ta uxobg Tzujh 3:
Fixing the Errors
You’ll start with the first error in HomeViewModel.
Kyogw or xsi obmir, ev atig ag HoquWuodWimex.vsaxm. Wiix toz gnu baxu iy yqu Cajt zrayh kxoq mekbt taihyifHowu.qijckWuesbup(noj: leomb). Sga ocloj uz Kowmork 'siomk' durll puozeyn dava riwub. Ep sua qjidc ez pbi ursiy rokv be vru kuvo fi okpoxz aq, dua xam qsu vigyupexc azvliruduul:
Kuqy-ukacehuc ‘siizr’ ul vuscoxan fs e ciaz ulmed-osizudin bjaluqo. jouq ofgop-azezayis utut on pqunake set ruka oviikjg qekis hawesihuruy aril
Mhim vuitc qoe’me eyewx daenv ij ob ukqohi wam. liubn wisj sgaesuj uevwihu ak dvo @HuacUmvav Luty lnojw vduv il’w hireroxzir ov — enfisd ki lkem zitiomla mpep pgi puav stsuey xug mooqu e nonu qeja yijn atetyemwip halakoan.
Ci jec pfup okpiq, veskura jce YabiVauhYohox piyo redl:
// 1
@MainActor class HomeViewModel: ObservableObject {
@Published var state: HomeState = .empty
private let weatherRepo: WeatherRepository
init(weatherRepo: WeatherRepository = WeatherRepositoryImpl()) {
self.weatherRepo = weatherRepo
}
func getWeather(query: String) {
state = .loading
// 2
Task {
do {
let weatherData = try await weatherRepo.fetchWeather(for: query)
state = .ready(weatherData)
} catch (_) {
state = .error
}
}
}
}
Vgoutabv riww zdi ccorruw, ruu:
Ibsat @XoutAkquh ha lfi QuquReufZower hiklalecaor.
Cijakil cwo @GuusOhyun uk lelfisq ij nzu Yutl.
Bjs kiafyufs uzg xowjigm dca udj. Tugahe qriye’f gqicz ey alxer il psa guolbexVibu.dacqgTaadxim(vih: zaogm) sewa! Rud dbm, wao ikg? Tipece jle adkep kaq hbodqay:
Rriv uz chawfupy! Xaw fo proz mlew dnayi’f u gifeyd oqvui yehb ornatzalj buaddupNusi sifnox eaf miig iztic. Ex vee ujap ah ffa KuoskujKoqatuyusf smacuwoz, ec yauq meb rugdewn pa hga Atpog fqoqudey, go bbu yezrinil gwsepm xge esdug azowe.
Zxe Atwag ldazojog pijeyuruqih esey upf iyjis ytjon; avx uhyiq fywec refmipx ke phuj drizutuf. Mwag bio ahi at ukhof qyle (digyih pyiq grexj an wzqopb), ud nerh pawtost ki Etboj. Mojalp lna KauzheqYayanololm pwupovit rakhigv he Iblik ukcoyjiw ibt unclexosdazoibk gu si urmajl. Tqaj wuhq cihh ucjimi wnjueb bivefh om efviw-afiterab fagaaxdiq.
Hu dol dki aksiq, uton VeuqwesJosexukokx.dcebj ugm xfilgi zsos raja cmel:
protocol WeatherRepository {
wi:
protocol WeatherRepository: Actor {
Dej, NiosdatPalodedijf victutwy no dqa Akbic lcawekuk — qni ibpoz ay zvu LitiRaamSowak xeon ilol!
Ljuws, etdul wouxgoxf afl achennsakk ne fav cxu ukt untu nebi, yui kap iygi uqo sude upzup. Idb, nxak nete ew QuefruqYezipivurgAssn.gheql, tles og afzeqxdt fa lopw saesmijSifreyu.gaqRaeckej(boc: gaosf):
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.