为什么我的 NSmutableDictionary 变成了 NSCFString?

发布于 2024-10-21 03:22:50 字数 1157 浏览 5 评论 0原文

我已经分配了一本字典并准备好放入我的程序的 appDelegate 中。

//appdel.m
    NSMutableDictionary *dict;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {    

    // Overide point for customization after application launch.

    dict = [[NSMutableDictionary alloc]init];


    dict = HBLoadDictionary(@"/dict.plist");

    // Add the tab bar controller's view to the window and display.
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

所以我希望这本字典存在于其他文件中,因此我将其设置为其他文件中的外部文件以供编辑和读取。

 //viewcontroller.m
    extern NSMutableDictionary *dict;

后来我决定为键设置一个对象。事件只是一个 EKevent。

NSString* str = [[NSString alloc] initWithFormat:@"%@", event.eventIdentifier];
NSString *eID  = [[NSString alloc]init];
eID = [data valueForKey:@"id"];
[dict setObject:str forKey:eID];

当我调用该函数时,我会得到这个

-[NSCFString setObject:forKey:]: unrecognized selector sent to instance

在某一时刻,我什至得到了一个 UIImage 而不是 NSCFString,这让我相信内存是一个问题,并且我处理得不好。为什么它甚至会改变这样的类型?导致它导致函数调用混乱......

I have a dictionary allocated and ready to go in the appDelegate of my program.

//appdel.m
    NSMutableDictionary *dict;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {    

    // Overide point for customization after application launch.

    dict = [[NSMutableDictionary alloc]init];


    dict = HBLoadDictionary(@"/dict.plist");

    // Add the tab bar controller's view to the window and display.
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

so I wanted this dictionary to exist in other files so I made it an external in other files to be edited and read from.

 //viewcontroller.m
    extern NSMutableDictionary *dict;

and later on I decide to set an object for a key. event is just an EKevent.

NSString* str = [[NSString alloc] initWithFormat:@"%@", event.eventIdentifier];
NSString *eID  = [[NSString alloc]init];
eID = [data valueForKey:@"id"];
[dict setObject:str forKey:eID];

when I make a call to the function I'll get this

-[NSCFString setObject:forKey:]: unrecognized selector sent to instance

At one point I even got a UIImage instead of a NSCFString which lead me to believe that memory is an issue and i'm not handling it right. Why is it even changing types like that? cause its causing function calls to mess up...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

比忠 2024-10-28 03:22:50

问题是您分配的 NSMutableDictionary 立即被 HBLoadDictionary 的返回值替换。我假设 HBLoadDictionary 返回一个自动释放的对象,您不会在任何地方保留该对象。加载字典后不久,它就会被释放,因此 dict 指向已释放的内存。此外,您分配的第一个 NSMutableDictionary 已泄漏。

您可以通过替换为来修复它

dict = [[NSMutableDictionary alloc]init];
dict = HBLoadDictionary(@"/dict.plist");

作为

dict = [HBLoadDictionary(@"/dict.plist") retain];

旁注,在 Objective-C 方法中初始化全局变量是不好的做法。尽管您可能不会有多个应用程序委托,并且其 application:didFinishLaunchingWithOptions: 方法不会被多次调用,但这可能会在其他情况下导致内存泄漏。最好只拥有一个返回静态变量的类方法。

The problem is that the NSMutableDictionary you allocated is immediately replaced by the return value from HBLoadDictionary. I would assume that HBLoadDictionary returns an autoreleased object, which you're not retaining anywhere. Shortly after you load the dictionary, it's deallocated so dict points to freed memory. Also, the first NSMutableDictionary that you allocated is leaked.

You can fix it by replacing

dict = [[NSMutableDictionary alloc]init];
dict = HBLoadDictionary(@"/dict.plist");

with

dict = [HBLoadDictionary(@"/dict.plist") retain];

As a side note, it's bad practice to initialize a global variable in an objective-c method. Although you probably won't have more than one application delegate, and its application:didFinishLaunchingWithOptions: method won't be called more than once, this could cause a memory leak in other situations. You're better off just having a class method that returns a static variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文