3

I'm trying to figure out the point of printOn. I've looked through some classes that implement it, and it looks like it just helps to print the units for different datatypes. Is this accurate?

If so, could someone point me in the right direction of how to go about implementing this for my own class? I'm going to be doing arithmetic on potentially different unit sets, and would like to be able to have something like:

4 sec * 2 min = 8 sec min

The implementation to handle these units is complete, but errors keep getting thrown complaining that we have to implement our own printOn:

2 Answers 2

8

printOn is like toString in Java, it add to a stream a string representation of an Object. You can look here to get a useful example.

6

The printOn: method is actually intended to be used at development time to show enough information about the object to allow the developer to identify it. This prevents a lot of clicking and diving in inspectors and debuggers. It allows you to click on a variable in a list and see its value displayed in the text pane of the inspector or debugger.

It's not normally used for displaying strings to the end users. The problem there is that strings normally need to be internationalized and mapped into the appropriate language. If you want to display strings to the end user, there are better ways than using printOn:.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.