3

I'm using the Yelp API and pulling down a YLPBusiness. When I attempt to print() or dump() the YLPBusiness, I only receive the memory address in the console log.

If I print(YLPBusiness.name) however, I will receive the name. How can I fully print out all property values of the YLPBusiness object?

Screen shot of code and console log

Wade Sellers
  • 323
  • 4
  • 12

2 Answers2

7

You should override your class description property:

func description() -> String {
    return "Business name: \(self.name), address: \(self.address), etc."
}

where you print all properties of YLPBusiness as you desire.

You can fix your problem mentioned in comments by turning your method into property:

public override var description: String {
    return "Business name: \(self.name), address: \(self.address), etc."
}

It happened because Swift detects discrepancies between overloading and overriding in the Swift type system and the effective behavior seen via the Objective-C runtime.

KlimczakM
  • 12,576
  • 11
  • 64
  • 83
  • That will solve it! Thank you very much. You and Paulw11 both have the right ideas here. Thanks a bunch! – Wade Sellers Jun 24 '16 at 06:29
  • My pleasure to help. Consider accepting one answer, if it fits you needs. – KlimczakM Jun 24 '16 at 06:32
  • The only trick I have remaining is that the YelpAPI is in Objective-C still so when I subclass YLPBusiness and try to modify the description() func... I am receiving the error, "Method 'description()' with Objective-C selector 'description' conflicts with getter for 'description' from superclass 'NSObject' with the same Objective-C selector" – Wade Sellers Jun 24 '16 at 06:38
  • and if I try to add the "override" in front of it, I receive the error... "Method does not override any method from its superclass" – Wade Sellers Jun 24 '16 at 06:39
  • @Wade Sellers Please take a look at my updated answer, that should help you. – KlimczakM Jun 24 '16 at 06:43
  • That did it @KlimczakM Thank you so much for the help and explaining that. – Wade Sellers Jun 24 '16 at 06:50
3

When you print an object you are actually invoking the object's description method. It seems that the YLPBusiness class does not implement this method. You could create an extension to YLPBusiness that implemented the description method.

Paulw11
  • 108,386
  • 14
  • 159
  • 186