<div dir="ltr">An addendum: if something represents a specific “thing” that can’t be duplicated without consequence, often that means it should be stored in one specific place in your code, not made into a class. <br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 30, 2017 at 10:10 PM, Brent Royal-Gordon 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"><div style="word-wrap:break-word"><span class=""><div><blockquote type="cite"><div>On Jun 29, 2017, at 10:40 AM, Vitor Navarro via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_-4410130522752024117Apple-interchange-newline"><div><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">Do you guys have any guideline regarding usage here?</span></div></blockquote></div><div><br></div></span><div>Here's my suggestion: Use a class when the object represents a specific *thing* that can't be duplicated without consequence; use a struct (or enum) when the instance represents some abstract data with no concrete existence. Some examples using framework types:</div><div><br></div><div>* A piece of text is just some data; duplicating it doesn't do anything except maybe use more memory. So `String` should be a struct.</div><div><br></div><div>* A label is a particular thing that exists at a particular place on screen; if you duplicated it, you'd end up with two labels on the screen, or with an off-screen copy of a label that wouldn't have any effect when you mutated it. So `UILabel` should be a class.</div><div><br></div><div>* A URL is just some data; if you construct two URLs with the same contents, they are completely interchangeable. So `URL` should be a struct.</div><div><br></div><div>* A connection to a web server to retrieve the contents of a URL is a particular thing; if you duplicated it, you would either establish another connection, or the two instances would interfere with each other (e.g. canceling one would cancel the other). So `URLSessionTask` and `NSURLConnection` are classes.</div><div><br></div><div>Sometimes the same problem, approached in slightly different ways, would allow you to use either one. For instance, a database record is a particular *thing* and should probably be a class, but copy the values of the fields (perhaps omitting the ID) out of it and suddenly you have a plausible struct. As a *general* rule, it's usually better to use structs where possible because it's easier to reason about their behavior—mutations in one function don't suddenly pop up in a totally unrelated function—but sometimes a particular type works very easily as a class and very awkwardly as a struct. Ultimately, it's a judgement call.</div><div><br></div><div>The other point I will make is this: "Protocol-oriented programming" is new and exciting and often very useful, but it's a tool, not a religion. If subclassing works well for your use case, then subclass away.</div><span class="HOEnZb"><font color="#888888"><br><div>
<span class="m_-4410130522752024117Apple-style-span" style="border-collapse:separate;font-variant-ligatures:normal;font-variant-east-asian:normal;line-height:normal;border-spacing:0px"><div><div style="font-size:12px">-- </div><div style="font-size:12px">Brent Royal-Gordon</div><div style="font-size:12px">Architechies</div></div></span>
</div>
<br></font></span></div><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></blockquote></div><br></div>