0
我觉得我已经阅读了许多(简单)例子,这些例子完全符合我所要做的。我似乎无法得到这个工作。我需要再次注意我的代码,并且我周围没有任何人,所以如果这看起来很简单,请原谅我......代码编译没有问题。谢谢!ObjC:在循环中构造一个可变字典
@implementation Engine
- (id) initWithInventory: (NSString *) path {
if (self = [super init]) {
NSString *contents = [NSString stringWithContentsOfFile:@"ingredientList.csv" encoding:NSASCIIStringEncoding error:nil];
NSLog(@"%@",contents); // This yields the contents of the file appropriately
NSArray *lines = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSRange ingredientRange = {0,96}; // This is done because I want to omit the last element of the array... the 97th is an empty string caused by the end of file newline character. I know it's bad coding...
NSEnumerator *enumerator = [[lines subarrayWithRange:ingredientRange] objectEnumerator];
NSString *curString;
NSArray *ingredientElements;
NSRange activeEffectRange = {1,4}; // Element 0 will be the key, elements 1-4 are the array to be stored.
while (curString = [enumerator nextObject]) {
ingredientElements = [curString componentsSeparatedByString:@","];
Ingredient *theIngredient = [[Ingredient alloc] initWithName:[ingredientElements objectAtIndex:0] andActiveEffects:[ingredientElements subarrayWithRange:activeEffectRange]];
NSLog(@"%@",[theIngredient ingredientName]);
NSLog(@"%@",[theIngredient activeEffects]); //These both print out correctly.
NSString *theName = [theIngredient ingredientName];
[allIngredients setObject:theIngredient forKey:theName];
NSLog(@"%@",[allIngredients objectForKey:[theIngredient ingredientName]]); // ***This yields (null)***
}
}
return self;
}
编辑:我要补充一点,allIngredients
是正在启动的类的实例变量,所以它被正确定义为NSMutableDictionary
:
@interface Engine : NSObject {
NSMutableDictionary *allIngredients;
}
- (id) initWithInventory: (NSString *) path;
@end
对不起,延迟接受。 – 2012-09-15 23:41:15