开发者

How to split objective c function definition?

开发者 https://www.devze.com 2022-12-13 17:09 出处:网络
I have a really long objective c function definition, and would like to split it into multiple lines to make the function more readable. Let\'s say I have this definition:

I have a really long objective c function definition, and would like to split it into multiple lines to make the function more readable. Let's say I have this definition:

-(id) initWithBsType:(NSInteger)buysell AccountCode:(NSString *):c_acc_code password:(NSString *)password exchangeCode:(NSString *)ecode productCode:(NSString *)product orderType:(NSString *)otype price:(NSString *)price qty:(NSString *):qty reference:(NSString *)ref enablePriceWarn:(BOOL)enablepw enableApprvWarn:(BOOL)enableaw orderValidity:(NSString *)validity;

What should I insert to split it into 3-4 lines?

(I'm creating an object that can b开发者_如何学Pythone serialized into xml using libxml, so I need to be able to assign this many params to the object on creation.)


Alternatively, in Xcode's configuration under "Indentation" you can turn on "Line Wrapping" to enable soft wrapping and you'll never have to worry about this again.


In typical Objective-C style, methods that span multiple lines are usually aligned by colon to make them more readable:

-(id) initWithBsType:(NSInteger)buysell
         AccountCode:(NSString *)c_acc_code
            password:(NSString *)password
        exchangeCode:(NSString *)ecode
         productCode:(NSString *)product
           orderType:(NSString *)otype
               price:(NSString *)price
                 qty:(NSString *)qty
           reference:(NSString *)ref
     enablePriceWarn:(BOOL)enablepw
     enableApprvWarn:(BOOL)enableaw
       orderValidity:(NSString *)validity;


Seeing the long parameter list makes me wonder how the calling function looks like... probably quite long.

I'd recommend using structs and maybe split the reading of the XML into more methods, e.g.:

typedef struct {
    /* ... */
} Order;

// extend the xml-reader for clean seperation, 
// seperate into multiple methods if too big
-(BOOL) readOrder:(Order*);

Now your initializer needs to take only one parameter:

-(id) initWithOrder:(Order*)order;
0

精彩评论

暂无评论...
验证码 换一张
取 消