<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">I’m trying to call this method to implement KVO in Swift:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">func</span> willChangeValue&lt;Value&gt;(for keyPath: <span style="color: #703daa" class="">KeyPath</span>&lt;<span style="color: #703daa" class="">Self</span>, <span style="color: #703daa" class="">Value</span>&gt;, withSetMutation mutation: <span style="color: #703daa" class="">NSKeyValueSetMutationKind</span>, using set: <span style="color: #703daa" class="">Set</span>&lt;<span style="color: #703daa" class="">Value</span>&gt;) -&gt; <span style="color: #703daa" class="">Void</span></div><br class=""></div><div class="">That method is defined in the standard library here:</div><div class=""><a href="https://github.com/apple/swift/blob/a550042391dcdc0a766a7d67f42d06fc0e92e90c/stdlib/public/SDK/Foundation/NSObject.swift#L210" class="">https://github.com/apple/swift/blob/a550042391dcdc0a766a7d67f42d06fc0e92e90c/stdlib/public/SDK/Foundation/NSObject.swift#L210</a></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">This is my code:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">class</span> Person: <span style="color: #703daa" class="">NSObject</span> {</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">@objc</span> <span style="color: #ba2da2" class="">dynamic</span> <span style="color: #ba2da2" class="">var</span> nicknames: <span style="color: #703daa" class="">Set</span>&lt;<span style="color: #703daa" class="">String</span>&gt; = []</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #ba2da2" class="">func</span> addNickname(<span style="color: #ba2da2" class="">_</span> nickname: <span style="color: #703daa" class="">String</span>) {</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ba2da2" class="">var</span> keyPath: <span style="color: #703daa" class="">KeyPath</span>&lt;<span style="color: #4f8187" class="">Person</span>, <span style="color: #703daa" class="">String</span>&gt; = ... <span style="color: #008400" class="">// ???</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3e1e81" class="">willChangeValue</span>(for: keyPath, withSetMutation: .<span style="color: #3e1e81" class="">union</span>, using: [nickname])</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #4f8187" class="">nicknames</span>.<span style="color: #3e1e81" class="">insert</span>(nickname)</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3e1e81" class="">didChangeValue</span>(for: keyPath, withSetMutation: .<span style="color: #3e1e81" class="">union</span>, using: [nickname])</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">}</div></div><div class=""><br class=""></div><div class="">The explicit type for that keyPath constant in the addNickname() method is exactly what the declaration of&nbsp;willChangeValue(for:, withSetMutation:, using:) wants and the compiler is happy with the way I wrote it here. It’s obvious that the KeyPath’s first generic type argument is the Person class itself and the second generic type argument is the type of whatever is inside the set. But I can’t find a way to actually write a keyPath that references a collection’s Element type.</div><div class=""><br class=""></div><div class="">Writing the property name itself yields a keyPath to the type of the property, which is Set&lt;String&gt;, obviously:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class=""><span style="color: #000000" class="">keyPath = \Person.nicknames </span>// ReferenceWritableKeyPath&lt;Person, Set&lt;String&gt;&gt;</div></div><div class=""><br class=""></div><div class="">I got creative and tried things like the following, but that only results in compiler errors telling me it’s ambiguous without more context or that a KeyPath must begin with a type:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">keyPath = \<span style="color: rgb(79, 129, 135);" class="">Person</span>.nicknames.<span style="color: rgb(186, 45, 162);" class="">Self</span>.Element</div></div><div class=""><br class=""></div><div class="">What do I have to write to actually get String instead of Set&lt;String&gt; there?</div><div class=""><br class=""></div><div class="">Ideally that would also work with an NSMutableSet, not just Set&lt;String&gt; because that’s what I actually need.</div><div class=""><br class=""></div><div class="">Thanks in advance,</div><div class=""><br class=""></div><div class="">Marco</div></body></html>