开发者

is there a shorter way to do this?

surely, there is a shorter way to do this?

NSString *filePathA = [[NSBundle mainBundle] pathForResource:@"A" ofType:@"txt"];
NSString *myTextA= [NSString stringWithContentsOfFile:filePathA encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresA = [myTextA componentsSeparatedByString:@"-"];




NSString *filePathB = [[NSBundle mainBundle] pathForResource:@"B" ofType:@"txt"];
NSString *myTextB= [NSString stringWithContentsOfFile:filePathB encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresB = [myTextB componentsSeparatedByString:@"-"];

NSString *filePathC = [[NSBundle mainBundle] pathForResource:@"C" ofType:@"txt"];
NSString *myTextC= [NSString stringWithContentsOfFile:filePathC encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresC = [myTextC componentsSeparatedByString:@"-"];

NSString *filePathD = [[NSBundle mainBundle] pathForResource:@"D" ofType:@"txt"];
NSString *myTextD= [NSString stringWithContentsOfFile:filePathD encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresD = [myTextD componentsSeparatedByString:@"-"];

NSString *filePathE = [[NSBundle mainBundle] pathForResource:@"E" ofType:@"txt"];
NSString *myTextE= [NSString stringWithContentsOfFile:filePathE encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresE = [myTextE componentsSeparatedByString:@"-"];

NSString *filePathF = [[NSBundle mainBundle] pathForResource:@"F" ofType:@"txt"];
NSString *myTextF= [NSString stringWithContentsOfFile:filePathF encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresF = [myTextF componentsSeparatedByString:@"-"];

NSString *filePathG = [[NSBundle mainBundle] pathForResource:@"G" ofType:@"txt"];
NSString *myTextG= [NSString stringWithContentsOfFile:filePathG encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresG = [myTextG componentsSeparatedByString:@"-"];

NSString *filePathH = [[NSBundle mainBundle] pathForResource:@"H" ofType:@"txt"];
NSString *myTextH= [NSString stringWithContentsOfFile:filePathH encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresH = [myTextH componentsSeparatedByString:@"-"];

NSString *filePathI = [[NSBundle mainBundle] pathForResource:@"I" ofType:@"txt"];
NSString *myTextI= [NSString stringWithContentsOfFile:filePathI encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresI = [myTextI componentsSeparatedByString:@"-"];

NSString *filePathJ = [[NSBundle mainBundle] pathForResource:@"J" ofType:@"txt"];
NSString *myTextJ= [NSString stringWithContentsOfFile:filePathJ encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresJ = [myTextJ componentsSeparatedByString:@"-"];

NSString *filePathK = [[NSBundle mainBundle] pathForResource:@"K" ofType:@"txt"];
NSString *myTextK= [NSString stringWithContentsOfFile:filePathK encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresK = [myTextK componentsSeparatedByString:@"-"];

NSString *filePathL = [[NSBundle mainBundle] pathForResource:@"L" ofType:@"txt"];
NSString *myTextL= [NSString stringWithContentsOfFile:filePathL encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresL = [myTextL componentsSeparatedByString:@"-"];

NSString *filePathM = [[NSBundle mainBundle] pathForResource:@"M" ofType:@"txt"];
NSString *myTextM= [NSString stringWithContentsOfFile:filePathM encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresM = [myTextM componentsSeparatedByString:@"-"];

NSString *filePathN = [[NSBundle mainBundle] pathForResource:@"N" ofType:@"txt"];
NSString *myTextN = [NSString stringWithContentsOfFile:filePathN encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresN = [myTextN componentsSeparatedByString:@"-"];

NSString *filePathO = [[NSBundle mainBundle] pathForResource:@"O" ofType:@"txt"];
NSString *myTextO= [NSString stringWithContentsOfFile:filePathO encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresO = [myTextO componentsSeparatedByString:@"-"];

NSString *filePathP = [[NSBundle mainBundle] pathForResource:@"P" ofType:@"txt"];
NSString *myTextP= [NSString stringWithContentsOfFile:filePathP encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresP = [myTextP componentsSeparatedByString:@"-"];

NSString *filePathQ = [[NSBundle mainBundle] pathForResource:@"Q" ofType:@"txt"];
NSString *myTextQ= [NSString stringWithContentsOfFile:filePathQ encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresQ = [myTextQ componentsSeparatedByString:@"-"];

NSString *filePathR = [[NSBundle mainBundle] pathForResource:@"R" ofType:@"txt"];
NSString *myTextR= [NSString stringWithContentsOfFile:filePathR encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresR = [myTextR componentsSeparatedByString:@"-"];

NSString *filePathS = [[NSBundle mainBundle] pathForResource:@"S" ofType:@"txt"];
NSString *myTextS= [NSString stringWithContentsOfFile:filePathS encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresS = [myTextS componentsSeparatedByString:@"-"];

NSString *filePathT = [[NSBundle mainBundle] pathForResource:@"T" ofType:@"txt"];
NSString *myTextT= [NSString stringWithContentsOfFile:filePathT encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresT = [myTextT componentsSeparatedByString:@"-"];

NSString *filePathU = [[NSBundle mainBundle] pathForResource:@"U" ofType:@"txt"];
NSString *myTextU= [NSString stringWithContentsOfFile:filePathU encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresU = [myTextU componentsSeparatedByString:@"-"];

NSString *filePathV = [[NSBundle mainBundle] pathForResource:@"V" ofType:@"txt"];
NSString *myTextV= [NSString stringWithContentsOfFile:filePathV encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresV = [myTextV componentsSeparatedByString:@"-"];

NSString *filePathW = [[NSBundle mainBundle] pathForResource:@"W" ofType:@"txt"];
NSString *myTextW= [NSString stringWithContentsOfFile:filePathW encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresW = [myTextW componentsSeparatedByString:@"-"];
//none with "X"
//NSString *filePathX = [[NSBundle mainBundle] pathForResource:@"X" ofType:@"txt"];
//NSString *myTextX=开发者_运维问答 [NSString stringWithContentsOfFile:filePathX encoding:NSUTF8StringEncoding error:nil];
//NSArray *arrayStructuresX = [myTextX componentsSeparatedByString:@"-"];

NSString *filePathY = [[NSBundle mainBundle] pathForResource:@"Y" ofType:@"txt"];
NSString *myTextY= [NSString stringWithContentsOfFile:filePathY encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresY = [myTextY componentsSeparatedByString:@"-"];

NSString *filePathZ = [[NSBundle mainBundle] pathForResource:@"Z" ofType:@"txt"];
NSString *myTextZ= [NSString stringWithContentsOfFile:filePathZ encoding:NSUTF8StringEncoding error:nil];
NSArray *arrayStructuresZ = [myTextZ componentsSeparatedByString:@"-"];

any ideas? I was suggested to refract it, but i have no idea how to that!!

thanks

Sam

UPDATE:

[ is repetitive code like this actually bad? im just starting off? would this be considered a bad practice? or a 'don't' in the world of programming? ]


I would suggest creating an array and a dictionary: one to hold all of your filepaths, and one to hold your arrayStructures.

Create the filepaths array with all of the filenames that you want to load data from.

The arrayStructures dictionary should initially be an empty NSMutableDictionary with a capacity of 25 ( letters in the alphabet -1 ( not using x ) ).

Loop through the filepaths array and do the exact thing that you were doing for each iteration, except at the end of each iteration, you should call addObject: forKey: arrayStructures dictionary.

NSMutableDictionary *structures = [ [ NSMutableDictionary alloc ] initWithCapacity:25 ];
NSArray *filePaths = [ [NSArray alloc] initWithObjects: @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", /* no X */@"Y", @"Z" ]; // you could also just create a standalone text file and load it from there, i know this is ridiculous!

for( NSString *path in filePaths ) {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:path ofType:@"txt"];
    NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [ structures addObject: [myText componentsSeparatedByString:@"-"] forKey: path ];
}

Now loop through the dictionary....

for( NSString *filePath in structures ) {
    NSArray *structure = [ structure objectForKey: filePath ];
    //...
}


Smells like an excellent opportunity to write a loop.

You can see that your code is repetitive, so what you need to do is extract the similar component from each element as a loop, extract the different compoenent as an input or output array, and you are done.


If you really want all those local variables I think this looks like a perfect opportunity to use a macro (if I dare to say)

#define MAKEARRAY( _letter ) \
  NSString *filePath ## _letter = [[NSBundle mainBundle] pathForResource:@ #_letter ofType:@"txt"]; \
  NSString *myText ## _letter = [NSString stringWithContentsOfFile:filePath ## _letter encoding:NSUTF8StringEncoding error:nil]; \
  NSArray *arrayStructures ## _letter = [myText ## _letter componentsSeparatedByString:@"-"]

MAKEARRAY( A );
MAKEARRAY( B );
MAKEARRAY( C );
:
:
MAKEARRAY( W );
//MAKEARRAY( X ); none with X
MAKEARRAY( Y );
MAKEARRAY( Z );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜