我正在开发一个 iOS 4 应用程序。
我有一个具有 NSMutableDictionary 属性的类:
@interface CardHelper : NSObject <NSXMLParserDelegate>{
...
NSMutableDictionary* cards;
...
}
@property (nonatomic, readonly) NSMutableDictionary* cards;
- (id)initWithXMLFile:(NSString *)xmlFileName andLanguage:(NSString *)language;
...
我在这里创建了 NSMutableDictionary:
...
#define kNumCards 22
...
- (id)initWithXMLFile:(NSString *)xmlFileName andLanguage:(NSString *)language
{
if (self = [super init])
{
userLanguage = [NSString stringWithString:language];
cards = [NSMutableDictionary dictionaryWithCapacity: kNumCards];
[self parseXMLFile:[self GetResourcePathFor:xmlFileName OfType:@"xml"]];
return self;
}
else
return nil;
}
我在这里添加元素:
- (void) parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
NSLog(@"DidEndElement: %@", elementName);
if ([elementName isEqualToString:@"card"])
{
[cards setObject:currentCard forKey:currentCard.Number];
[currentCard release];
currentCard = nil;
return;
}
...
}
CardHelper 对象是在一个名为 ViewController(我的应用程序的主视图控制器)的类上创建的。从这个视图控制器我展示了另一个:
- (IBAction)oneCardCliked:(id)sender
{
oneViewController = [[OneCardViewController alloc] init];
oneViewController.cardHelper = cardHelper;
[self presentModalViewController:oneViewController animated:YES];
}
在 ViewController 中定义的 CardHelper:
@interface ViewController : UIViewController {
...
CardHelper* cardHelper;
...
}
...
我将 cardHelper 传递给 OneCardViewController 以在那里使用。
但是,在OneCardViewController
我尝试获取card
fromcards
时,我发现卡已从 转换NSMutableDictionary
为NSCFString
。
OneCardViewController 接口:
@interface OneCardViewController : UIViewController {
CardHelper* cardHelper;
...
}
@property (nonatomic, retain) CardHelper* cardHelper;
我在这里得到例外:
- (void) setUpTarotGame
{
int arcaneNumber;
arcaneNumber = [cardHelper GenerateArcaneNumber];
NSString* number = [NSString stringWithFormat:@"%d", arcaneNumber];
if (cardHelper == nil) {
NSLog(@"cardHelper nil");
return;
}
if (cardHelper.cards == nil)
{
NSLog(@"cards nil");
return;
}
else
NSLog(@"cards count = %d", [cardHelper.cards count]);
currentCard = [cardHelper.cards objectForKey:number];
[self setCardImageWithArcane:arcaneNumber];
}
在这一行中抛出异常:
currentCard = [cardHelper.cards objectForKey:number];
你知道为什么吗?