<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">I don't know why the limit exists. Technical or on purpose. I hope someone inside can answer this.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Sep 12, 2016 at 11:55 PM, Daniel Dunbar via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On Sep 11, 2016, at 6:12 PM, Tanner Nelson via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
><br>
> Hey Swift Users,<br>
><br>
> I was wondering how you all work around not being able to add stored properties in extensions (especially protocol extensions).<br>
><br>
> I ran into an issue recently where I needed an internal stored variable for a protocol, but I didn't want the conformer to worry about implementing the variable.<br>
><br>
> I ended up using something like this to achieve the effect.<br>
><br>
> extension MyProtocol {<br>
> private var address: String {<br>
> mutating get {<br>
> var id = ""<br>
> withUnsafePointer(to: &self) { id = "\($0)"}<br>
> return id<br>
<br>
</span>BTW, you can write `return withUnsafePointer(to: &self) { "\($0)" }` instead.<br>
<span class=""><br>
> }<br>
> }<br>
><br>
> var myStoredVar: Bool {<br>
> mutating get {<br>
> return _storage[address] ?? false<br>
> }<br>
> set {<br>
> _storage[address] = newValue<br>
> }<br>
> }<br>
> }<br>
><br>
> Obviously not ideal, but I don't see another way to achieve this besides subclassing (which has its own problems for my situation).<br>
><br>
> Wondering if anyone has run into this and come up with a better solution or other type of work around.<br>
<br>
</span>I don't have a better answer to this part.<br>
<br>
This particular solution only works if it is ok for `_storage[address]` to potentially be reused by a new instance, though (if the old instance is deallocated and the new one is allocated in its place).<br>
<br>
- Daniel<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> Thanks!<br>
> Tanner<br>
> ______________________________<wbr>_________________<br>
> swift-users mailing list<br>
> <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br>
______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
</div></div></blockquote></div><br></div>