I've had ideas for something like this in the past but I've finally implemented one and I could definitely use some code review for it. Obviously the quality of the names generated will depend on what the whole thing is seeded with, but that's a separate question. My concerns are the quality and readability of the code, following Objective-C best practices, and possibly how to better structure it to add functionality to it.
I'm creating an NSString, so I did briefly try to subclass NSString before I realized that it was a lot more complicated than I thought, so I'm just using a string property on the object.
Here is the header:
#import <Foundation/Foundation.h>
@interface DTNameCreator : NSObject
//eventually this would take an enum as a type
-(id) initWithType:(int)characterType;
@property NSString *createdName;
@end
And the implementation:
#import "DTNameCreator.h"
@implementation DTNameCreator {
NSMutableArray *_prefixArray;
NSMutableArray *_middleString1Array;
NSMutableArray *_middleString2Array;
NSMutableArray *_middleString3Array;
NSMutableArray *_suffixStringArray;
NSString *_prefixString;
NSString *_middleString1;
NSString *_middleString2;
NSString *_middleString3;
NSString *_suffixString;
NSMutableArray *_generatorArray;
}
#pragma mark - Initialization
-(id) initWithType:(int)characterType {
self = [super init];
if (self) {
[self seedArraysForType:characterType];
[self createNameGeneratorStringsAndArray];
[self createName];
}
return self;
}
-(void) seedArraysForType:(int)characterType {
switch (characterType) {
case 0:
[self seedArraysForType0];
break;
case 1:
[self seedArraysForType1];
break;
default:
break;
}
}
-(void) seedArraysForType0 {
_prefixArray = [[NSMutableArray alloc]initWithObjects:@"Abu", @"Bra", @"Der", @"Eve", @"Far", @"Gar", @"Her", nil];
_middleString1Array = [[NSMutableArray alloc]initWithObjects:@"ne", @"we", @"re", @"ne", @"be", @"ge", @"te", nil];
_middleString2Array = _middleString1Array;
_middleString3Array = _middleString1Array;
_suffixStringArray = [[NSMutableArray alloc]initWithObjects:@"we", @"re",@"mu", @"ne", @"ge", @"or", @"ar", nil];
}
-(void) seedArraysForType1 {
_prefixArray = [[NSMutableArray alloc]initWithObjects:@"Wen", @"Pre", @"Mar", @"Nem", @"Car", @"Cat", @"Hel", nil];
_middleString1Array = [[NSMutableArray alloc]initWithObjects:@"ne", @"we", @"re", @"ne", @"be", @"ge", @"te", nil];
_middleString2Array = _middleString1Array;
_middleString3Array = _middleString1Array;
_suffixStringArray = [[NSMutableArray alloc]initWithObjects:@"pe", @"ra",@"mul", @"nem", @"ge", @"ara", @"era", nil];
}
-(void) createNameGeneratorStringsAndArray {
int randomNumber1 = arc4random() % _prefixArray.count;
int randomNumber2 = arc4random() % _middleString1Array.count;
int randomNumber3 = arc4random() % _middleString2Array.count;
int randomNumber4 = arc4random() % _middleString3Array.count;
int randomNumber5 = arc4random() % _suffixStringArray.count;
_prefixString = [[NSString alloc]initWithString:[_prefixArray objectAtIndex:randomNumber1]];
_middleString1 = [[NSString alloc]initWithString:[_middleString1Array objectAtIndex:randomNumber2]];
_middleString2 = [[NSString alloc]initWithString:[_middleString2Array objectAtIndex:randomNumber3]];
_middleString3 = [[NSString alloc]initWithString:[_middleString3Array objectAtIndex:randomNumber4]];
_suffixString = [[NSString alloc]initWithString:[_suffixStringArray objectAtIndex:randomNumber5]];
//only add the middle ones because we add the prefix and suffix manually
_generatorArray = [[NSMutableArray alloc]initWithObjects:_middleString1, _middleString2, _middleString3, nil];
}
#pragma mark - Name geneneration
-(void) createName {
NSMutableArray *localGeneratorArray = [[NSMutableArray alloc]init];
NSString *generatorString = [[NSString alloc]init];
//have to add the prefix manually first
[localGeneratorArray addObject:_prefixString];
//check if any of the middle pieces are already present in the name and add if not
BOOL isMiddlePresent = NO;
for (id obj in _generatorArray) {
for (int i = 0; i < localGeneratorArray.count; i++) {
if ([obj isEqualToString:[localGeneratorArray objectAtIndex:i]]) {
isMiddlePresent = YES;
}
}
if (!isMiddlePresent) {
[localGeneratorArray addObject:obj];
}
}
//check if the suffix is present and add it if not
BOOL isSuffixPresent = NO;
for (int i = 0; i < localGeneratorArray.count; i++) {
if ([_suffixString isEqualToString:[localGeneratorArray objectAtIndex:i]]) {
isSuffixPresent = YES;
}
}
if (!isSuffixPresent) {
[localGeneratorArray addObject:_suffixString];
}
//create the name string out of the contents of the array
for (int i = 0; i < localGeneratorArray.count; i++) {
generatorString = [generatorString stringByAppendingString:[localGeneratorArray objectAtIndex:i]];
}
self.createdName = generatorString;
}
@end
And a sample of the output:
2014-04-07 10:12:33.273 DwarfTowers01[21566:60b] Eveweor
2014-04-07 10:12:33.273 DwarfTowers01[21566:60b] Abuwetegere
2014-04-07 10:12:33.274 DwarfTowers01[21566:60b] Garnear
2014-04-07 10:12:33.274 DwarfTowers01[21566:60b] Fargewene
2014-04-07 10:12:33.275 DwarfTowers01[21566:60b] Derneteor
2014-04-07 10:12:33.275 DwarfTowers01[21566:60b] Evenewe
2014-04-07 10:12:33.276 DwarfTowers01[21566:60b] Farnewegemu
2014-04-07 10:12:33.276 DwarfTowers01[21566:60b] Dernege
2014-04-07 10:12:33.277 DwarfTowers01[21566:60b] Gargemu
2014-04-07 10:12:33.277 DwarfTowers01[21566:60b] Farbewenere