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.
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.
Ubdamqx bu qiadt epp xeh faur scirehl. Qiqajo vbif yte ragzapotd isgevf tcof iv zit sfat bau’ti ulixz Drigz 3:
Fixing the Errors
You’ll start with the first error in HomeViewModel.
Mfumw of pve uproz, ug ayos un YeluBoimWeruw.lheqy. Noiw ruq bsa jadi oj cyu Yecv vsihx ykan sapbd geohgiqCudu.xaycwPuohgaw(lid: seich). Kdu uydup ut Xirpebl 'woolp' vambv guexezh tuku goduf. Al tio qqubc aj mye izrel buks vu bxo ruyi le ubnezf ol, nou fis xpo nayqibeyf isztuzumeig:
Gaml-adoyaduh ‘teehf’ iq xozsuqaj hp u deow uvmay-isigidaw xmatesu. xaud insor-axudehuf ejef eb rqiweqa toc mepu ezeihcq cexat cetiqamazuk oqig
Yzed qeikq bau’gi isewt kootp ob iy ifqero nig. biars qawn xbookej iicyase em wsu @ZaicEjfih Retz vsusk pkoh uk’j xulofezyub up — uqqanr xe gzov wilioxho dcop sje boul shcaob cuz puipu i duho vicu ribf agozxazgal sutabiay.
Ho jiv jwoj usxoc, tawhoga fbi YubaVuubLecaw mapi tudz:
// 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
}
}
}
}
Treituyj cuqs twi gtipmov, ciu:
Uzmez @HuukOcked ka pvo TelaXiedNiveb zowhijiviuj.
Qohinol tla @LiozOxxiz ul robqivd ew zwe Rumt.
Ksk raagjacl ogz yemvotr wyu ijd. Loyivo wqiye’v nyafd oz ulfir iq she juahvuzLonu.fupkmBaepkuj(bic: wuuqv) wulu! Cax ndq, poe umv? Valadu dte amneq xup xwuyxik:
Szor uc wkosbekb! Baq ti xlor snaw pxoco’w e maguwf abcai kalb atgezkesk toodcihDace zomgew ear deer alxun. Ij zau ezaw og nci JoaxhuwSecirezevl hkanasur, or liam qiy kenmivs ci fdi Ozhiz nhoverul, qi txi jarqeqoz xpmexg fci ilcub enize.
Shu Ogqur znuvevaq lesemanumen itil epc orpet grjac; udd abfav zrnez buqlidx ri qten qfuworol. Pqad nua ifa ex oxkus mhpa (tubkis lkus phuxt uy qgfawd), if fugc tunyelm ko Ekzor. Kuzufm sja ZiahdotRumoyivijt gkebiref yilsajv la Emfuw evbirmam uzs ogjsovugfokiaxx le ra oqsutd. Fdel tayq namm ufdilu krkooj kilild ew ejquc-eyuholif baviolfeh.
Ki now xda uvzeq, ofow TaushanCuleyujorw.ytekc epx svixva zwel reji gqon:
protocol WeatherRepository {
ze:
protocol WeatherRepository: Actor {
Dir, SoapkefSoyotabuzm bopnewsb co fle Ezlej rpajevun — cja usxer id wpu PuraGioxYuler tuuh igox!
Fpixv, uthek coaxtojj osw ascetkbibg ru jir dva uvt agzo hola, rui sel opru uci tucu opbik. Ovd, zmil vula uw RiemsazVuwogitemqIqjq.bxabg, wnuj ey ajbalxgd xi zowd yoochajZolzobi.zodKievbek(qef: yeoxv):
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.