{"id":53055,"date":"2026-09-01T16:08:59","date_gmt":"2026-09-01T20:08:59","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=53055"},"modified":"2026-09-01T16:08:59","modified_gmt":"2026-09-01T20:08:59","slug":"swift-6-4-warning-for-weak-captures-within-nested-closures","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2026\/09\/01\/swift-6-4-warning-for-weak-captures-within-nested-closures\/","title":{"rendered":"Swift 6.4: Warning for Weak Captures Within Nested Closures"},"content":{"rendered":"<p><a href=\"https:\/\/www.swiftbysundell.com\/articles\/warning-for-nested-weak-self-closure-captures\/\">John Sundell<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.swiftbysundell.com\/articles\/warning-for-nested-weak-self-closure-captures\/\">\n<p>New in Swift 6.4 [&#8230;] is a warning which is emitted when a nested closure performs a <code>weak<\/code> capture while the parent closure implicitly captures that object as a <em>strong reference<\/em>.<\/p><p>At first glance, that new diagnostic could be a bit puzzling, since it might seem unnecessary to have to specify such a weak capture twice (or, alternatively, move it to the parent closure&rsquo;s capture list), but it turns out that it&rsquo;s an incredibly useful new warning that can help us avoid a quite common kind of memory management bug.<\/p>\n<p>[&#8230;]<\/p>\n<p>Finally, in situations where capturing <code>self<\/code> strongly doesn&rsquo;t actually lead to a retain cycle, we can silence the new warning for nested <code>weak<\/code> captures by explicitly adding <code>self<\/code> to the outer closure&rsquo;s capture list, such as when creating a <code>Task<\/code> within the following example[&#8230;]<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2026\/06\/24\/swift-6-4\/\">Swift 6.4<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2026\/06\/09\/xcode-27-announced\/\">Xcode 27 Announced<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/08\/17\/how-to-automate-memory-leak-detection-with-xctest\/\">How to Automate Memory Leak Detection With XCTest<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/08\/04\/avoiding-implicit-retain-cycles-when-using-swift-function-references\/\">Avoiding Implicit Retain Cycles When Using Swift Function References<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/05\/18\/weak-self-closure-rules-of-thumb\/\">Weak Self: Closure Rules of Thumb<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/08\/04\/swift-guard-capture-specifier-pitch\/\">Swift &ldquo;guard&rdquo; Capture Specifier Pitch<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/07\/13\/implicit-capturing-of-self-in-swift-5-3\/\">Implicit Capturing of Self in Swift 5.3<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/03\/23\/dealing-with-weak-in-closure-based-delegation\/\">Dealing With Weak in Closure-based Delegation<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/04\/05\/stop-the-weak-strong-swift-dance\/\">Stop the weak-strong Swift Dance<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>John Sundell: New in Swift 6.4 [&#8230;] is a warning which is emitted when a nested closure performs a weak capture while the parent closure implicitly captures that object as a strong reference.At first glance, that new diagnostic could be a bit puzzling, since it might seem unnecessary to have to specify such a weak [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2026-09-01T20:09:02Z","apple_news_api_id":"907537bf-7aa5-4201-8800-c1bced9b5417","apple_news_api_modified_at":"2026-09-01T20:09:02Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AkHU3v3qlQgGIAMG87ZtUFw","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[55,46,571,71,901],"class_list":["post-53055","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-languagedesign","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/53055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=53055"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/53055\/revisions"}],"predecessor-version":[{"id":53056,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/53055\/revisions\/53056"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=53055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=53055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=53055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}