Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Intermediate Swift Memory Management in Swift Weak References

Weak can no longer be added to a protocol variable.

At 4:32, "weak" is added to a protocol variable, but this is no longer supported. My now current version of Xcode says:

"'weak' should not be applied to a property declaration in a protocol and will be disallowed in future versions"

Moving forward, how would this technique be used?

1 Answer

According to Swift Proposal SE-0186, the ability to add a weak property declaration in a protocol was essentially purposeless: You could add the weak keyword, but it wasn't enforced through compiler support, leading to potential unexpected behavior down the chain and failing to prevent against memory leaks, which was presumably the whole point in the first place.

Instead, for now, just know that you cannot declare a property as 'weak' in the protocol itself (just remove it), and instead you'll need to simply add the keyword where needed. :thumbsup:


Note: Some have argued — instead of simply removing the ability to add the 'weak' declaration to properties in a protocol — that compiler support for protocol conformance for this should be implemented (I agree). However, while the reviewing managers at Apple have acknowledged this alternative solution, and said it may be addressed as a part of a larger overhaul later, they decided it was out of scope at the time and didn't fit with other changes that had been made to fix similar issues.