Do I have to make two classes?

Discussion in 'iOS Programming' started by mikezang, Aug 18, 2010.

  1. macrumors 6502a

    May 22, 2010
    Tokyo, Japan
    I want to read some data and they have such structures:
    @interface StockSplit : NSObject {
    	NSString		*code;
    	NSString		*name;
    	NSString		*market;
    	int			unit;
    	bool			N225;
    	bool			OBS;
    	NSMutableArray	*split
    The split is a array as below:
    struct {
    	NSDate	*date;
    	float		rate;
    } SPLIT;
    Do I have to make split as a single class, can I put it into class StockSplit or any else simple method?
  2. macrumors regular

    Oct 18, 2007
    You can do anything you want, but...

    You don't HAVE to do anything - you can make the entire array text strings and then interpret the even number elements as one kind of thing and the odd elements as another thing if you like.

    However, the right question to be asking is one of yourself: How is the array going to be used?

    • Will you only be reading them? seems unlikely - how do they get populated?
    • Will something be editing them too? goes nicely with the above question
    • Do you expect adding new SPLITs into the array, deleting SPLITs from the array, and modifying the values of existing array elements?

    As for the question you asked, I'd make them a separate class, and the array an array (or other collection) of those objects, so it can have its own semantics. Consider the case if you want to add or change the content of a split - you will then have to go around your code and update it.

    My personal approach is to hide all the data members behind accessors - if you want to read XXX, use a getXXX: method; if you want to set XXX, use a setXXX: method. For the array, you can have addSplit: and removeSplit: along with getSplit: and setSplit: and getAllSplits: methods.

    Yes, it gets complicated, which is why I started with the question instead of an answer.
  3. thread starter macrumors 6502a

    May 22, 2010
    Tokyo, Japan
    Thanks for your question and answer.
    In fact, there is a text file in format as below:
    code,name,market,unit,[n225,] [obs,] split
    here split in format as below:
    S:yyyymmdd:rate [,S:yyyymmdd:rate]
    some data like this, total about 5000 lines.
    I found when I put them to class, it is very slow, so I am not sure if I have to use class, because use String Array is faster than class, do you think so?

Share This Page