gpt4 book ai didi

ios - NSMutableDictionary 转换成 __ NSCFString

转载 作者:行者123 更新时间:2023-12-01 18:01:49 29 4
gpt4 key购买 nike

我正在开发一个 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(我的应用程序的主视图 Controller )的类上创建的。从这个 View Controller 我展示了另一个:
- (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来自 cards ,我知道卡片已从 NSMutableDictionary 转换至 NSCFString .

OneCardViewController 接口(interface):
@interface OneCardViewController : UIViewController {
CardHelper* cardHelper;
...
}

@property (nonatomic, retain) CardHelper* cardHelper;

我在这里得到异常(exception):
- (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];

你知道为什么吗?

最佳答案

它没有被转换,它正在被释放,内存被用于其他用途。

在您的 init 方法中,您没有保留这些对象:

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

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

反而。

关于ios - NSMutableDictionary 转换成 __ NSCFString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8503337/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com