Category, protocol, one big class? What should i use here?

I am trying to implement a general class of numbers. Basically, my application should receive a string from the user and analyze it to determine if its imperial (foot fraction or any combination of them) or its metric (m cm mm or any combination). My idea was to make metric and imperial classes, both of which have a more general Number type. The user interface then creates a Number object, passing in the line for parsing, and the number determines whether it is metric or imperial. I see how easy it is to work with the 1 class of large numbers, which does this, but it seems to me that I should separate the metric and imperial classes from the standard interface (Number), since they will do much more, and then just parse the string.

So, if I do Number *num = [[Number alloc] initWithString:someString];, I get a subclass of Number that refers to Imperial or Metric. This is really a place where I know something doesn't make sense, but I'm not sure how to do it.

What would be a good way to handle this?

+5
source share
2 answers

What do you think is wrong with your proposal?

What you want to do and how to solve it exactly matches the Cocoa concept, called class clusters, described in the Cocoa Fundamentals Guide . Apple uses this pattern throughout Foundation and AppKit.

+4
source

NSFormatter, . , , .

. , "" "LinegthInInches" "LengthInMetres" (). , , , , - . , , , . , NSNumber, .

, , , . NSNumberFormatter , , , , .

+2

All Articles