为什么我的 NSmutableDictionary 变成了 NSCFString?
我已经分配了一本字典并准备好放入我的程序的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您分配的 NSMutableDictionary 立即被 HBLoadDictionary 的返回值替换。我假设 HBLoadDictionary 返回一个自动释放的对象,您不会在任何地方保留该对象。加载字典后不久,它就会被释放,因此 dict 指向已释放的内存。此外,您分配的第一个 NSMutableDictionary 已泄漏。
您可以通过替换为来修复它
作为
旁注,在 Objective-C 方法中初始化全局变量是不好的做法。尽管您可能不会有多个应用程序委托,并且其 application:didFinishLaunchingWithOptions: 方法不会被多次调用,但这可能会在其他情况下导致内存泄漏。最好只拥有一个返回静态变量的类方法。
The problem is that the
NSMutableDictionary
you allocated is immediately replaced by the return value fromHBLoadDictionary
. I would assume thatHBLoadDictionary
returns an autoreleased object, which you're not retaining anywhere. Shortly after you load the dictionary, it's deallocated sodict
points to freed memory. Also, the first NSMutableDictionary that you allocated is leaked.You can fix it by replacing
with
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.