2011-12-14 20 views
1

我正在開發一個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,我拿到卡已轉換從NSMutableDictionaryNSCFString

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]; 

你知道爲什麼嗎?

回答

2

它沒有被轉換,它正在處理和內存被用於其他事情。

在你的init方法,你不保留這些對象:

userLanguage = [NSString stringWithString:language]; 
cards = [NSMutableDictionary dictionaryWithCapacity: kNumCards]; 

嘗試

userLanguage = [language copy]; 
cards = [NSMutableDictionary alloc] initWithCapacity:kNumCards]; 

代替。

1
cards = [NSMutableDictionary dictionaryWithCapacity: kNumCards]; 

這會創建一個自動釋放對象。當你再次調用它時,它已經被釋放並被分配給其他的東西,在這種情況下是一個字符串。

您必須保留字典或創建一個非自動釋放一個:

cards = [[NSMutableDictionary alloc] initWithCapacity:kNumCards]; 
2

你沒有保留的對象,他們被釋放和指針沒有指向一個被重新使用的新對象相同的記憶。

考慮爲您的項目使用ARC(自動保留計數)。使用ARC,編譯器負責保留計數,因此您不必實際不允許這樣做。有一個重構將轉換當前的項目。

你的編程生活應該更加高興與ARC。 :-)

相關問題