我正在開發一個iOS 4應用程序。NSMutableDictionary轉換爲__ NSCFString
我有一個的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的類上創建的(我的應用程序的主視圖控制器)。從該視圖控制器I示出了另一之一:在所定義的ViewController
- (IBAction)oneCardCliked:(id)sender
{
oneViewController = [[OneCardViewController alloc] init];
oneViewController.cardHelper = cardHelper;
[self presentModalViewController:oneViewController animated:YES];
}
CardHelper:
@interface ViewController : UIViewController {
...
CardHelper* cardHelper;
...
}
...
我通過cardHelper到OneCardViewController使用那裏。
但是,在OneCardViewController
我試圖從cards
一個card
,我拿到卡已轉換從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];
你知道爲什麼嗎?