Wednesday, 15 January 2014

Objective-C: multiple if and return statements with least amount of code -



Objective-C: multiple if and return statements with least amount of code -

this question on how code efficiently little code possible. works need incoporate paramenter numberofshapes. if 1 returns ■, if 2 ■■, if 3 ■■■ etc...

i if statemets , homecoming statements. if square->if number = 1> homecoming ■, if number >2 homecoming ■■ etc.. whole lot of code simple.

what best way of coding to the lowest degree amount of code?

- (nsstring *)getshape: (nsnumber *)shape numberofshapes: (nsnumber *)number { if ([shape isequaltonumber:[nsnumber numberwithint:square]]) homecoming @"■"; if ([shape isequaltonumber:[nsnumber numberwithint:circle]]) homecoming @"●"; if ([shape isequaltonumber:[nsnumber numberwithint:triangle]]) homecoming @"▲"; homecoming @"?"; }

- (nsstring *)getshape: (nsnumber *)shape numberofshapes: (nsnumber *)number { unsigned shapeint = [shape unsignedintvalue]; if (shapeint >= 3) homecoming @"?"; nsstring *shapestr = [@"■●▲" substringwithrange:nsmakerange(shapeint, 1)]; // add together autorelease here, if using mrr... nsmutablestring *result = [[nsmutablestring alloc] init]; unsigned numberint = [number unsignedintvalue]; (unsigned = 0; < numberint; i++) [result appendstring:shapestr]; homecoming result; }

i don't see point of using nsnumber objects pass parameters this, can't simple nsuinteger or unsigned can, , more expensive use.

objective-c

No comments:

Post a Comment