<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Yes, not all animals have names.<div class=""><br class=""></div><div class="">Defining a protocol sounds reasonable.</div><div class=""><br class=""></div><div class="">In my case, the Animal, Dog and Cat are all subclasses of Core Data's NSManagedObject, having to define a protocol and make them conform to that is a bit involved. </div><div class=""><br class=""></div><div class="">But I think that seems to be the only sensible way.</div><div class=""><br class=""></div><div class="">Thanks.</div><div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On 9 Jun 2017, at 7:28 PM, Jens Persson <<a href="mailto:jens@bitcycle.com" class="">jens@bitcycle.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">I'm not sure what you are trying to achieve.<div class="">Why can't you just do:</div><div class="">label.text = animal.nam</div><div class="">?</div><div class="">Is it because not all Animals have names?</div><div class="">If so, you could perhaps use something like this:<br class=""></div><div class=""><br class=""></div><div class=""><div class="">class Animal {}</div><div class="">protocol Named {</div><div class=""> var name: String { get }</div><div class="">}</div><div class="">class Dog: Animal, Named {</div><div class=""> let name = "Dog"</div><div class="">}</div><div class="">class Cat: Animal, Named {</div><div class=""> var name: String {</div><div class=""> return "Cat"</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">let animals: [Animal] = [Cat(), Dog()]</div><div class=""><br class=""></div><div class="">for animal in animals {</div><div class=""> if let namedAnimal = animal as? Named {</div><div class=""> print(namedAnimal.name)</div><div class=""> }</div><div class="">}</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Fri, Jun 9, 2017 at 12:34 PM, Glen Huang via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi, I wonder what's the correct way to express something like this in swift:<br class="">
<br class="">
Say I have a superclass with two subclasses:<br class="">
<br class="">
class Animal {}<br class="">
class Dog: Animal {<br class="">
let name = "Dog"<br class="">
}<br class="">
class Cat: Animal {<br class="">
var name: String {<br class="">
return "Cat"<br class="">
}<br class="">
}<br class="">
<br class="">
And I want to display the name with a switch:<br class="">
<br class="">
switch animal {<br class="">
case let a as Dog, let a as Cat:<br class="">
label.text = <a href="http://a.name/" rel="noreferrer" target="_blank" class="">a.name</a><br class="">
default:<br class="">
break<br class="">
}<br class="">
<br class="">
Swift currently won't allow me to use multiple value-binding patterns like this. I wonder what's the right way to express it? Must I repeat the statements?<br class="">
<br class="">
switch animal {<br class="">
case let a as Dog:<br class="">
label.text = <a href="http://a.name/" rel="noreferrer" target="_blank" class="">a.name</a><br class="">
case let a as Cat:<br class="">
label.text = <a href="http://a.name/" rel="noreferrer" target="_blank" class="">a.name</a><br class="">
default:<br class="">
break<br class="">
}<br class="">
<br class="">
I also tried fallthrough, but swift won't let me fall through to a value-binding pattern:<br class="">
<br class="">
switch animal {<br class="">
case let a as Dog:<br class="">
fallthrough // error<br class="">
case let a as Cat:<br class="">
label.text = <a href="http://a.name/" rel="noreferrer" target="_blank" class="">a.name</a><br class="">
default:<br class="">
break<br class="">
}<br class="">
<br class="">
I'm at my wits end. Could someone shed some light on this? Thanks.<br class="">
______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
</blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></body></html>