NSDictionary 丢失其内容(不包括错误访问)
所以我有一个 uiviewcontroller。它具有 NSMutableArray 的属性,并具有非原子的、合成的保留属性。
在 viewDidLoad 中,我使用以下命令对其进行初始化,并向导航栏添加一个按钮。
test = [NSDictionary dictionaryWithObjectsAndKeys:
[[SearchField alloc] initWithName:@"Subject" :Text], kSubjectKey,
[[SearchField alloc] initWithName:@"Source publication" :Text], kSourceKey,
[[SearchField alloc] initWithName:@"Keyword" :Text], kKeywordKey,
[[SearchField alloc] initWithName:@"Author" :Text], kAuthorKey,
[[SearchField alloc] initWithName:@"Color" :Bool], kColorKey,
nil ];
NSLog([NSString stringWithFormat:@"lol %d", [test count]]);
第一个日志在通过调试器运行时运行良好。但是,我已将以下代码绑定到按钮:
-(void)search:(id)sender{
NSLog([NSString stringWithFormat:@"lol %d", [test count]]);
当此代码执行时,日志行会因异常访问而崩溃。在 viewDidLoad 和按下按钮之间 NSDictionary 没有发生任何事情,那么为什么会发生这种情况呢?更重要的是,我该如何解决它? :)
干杯
编辑 我想也许是我的SearchField类在做事,所以我用简单的字符串替换它们,问题仍然出现。
So i have a uiviewcontroller. It has a property of an NSMutableArray with a nonatomic, retain property synthesized.
In the viewDidLoad I init it with the following, and also add a button to the nav bar.
test = [NSDictionary dictionaryWithObjectsAndKeys:
[[SearchField alloc] initWithName:@"Subject" :Text], kSubjectKey,
[[SearchField alloc] initWithName:@"Source publication" :Text], kSourceKey,
[[SearchField alloc] initWithName:@"Keyword" :Text], kKeywordKey,
[[SearchField alloc] initWithName:@"Author" :Text], kAuthorKey,
[[SearchField alloc] initWithName:@"Color" :Bool], kColorKey,
nil ];
NSLog([NSString stringWithFormat:@"lol %d", [test count]]);
The first log, when running through the debugger runs fine. However, I have tied the following code to the button:
-(void)search:(id)sender{
NSLog([NSString stringWithFormat:@"lol %d", [test count]]);
When this code executes the log line crashes with exc bad access. Nothing is happening to the NSDictionary between the viewDidLoad and the button press, so why is this happening? And more importantly, how do I fix it? :)
Cheers
Edit
I thought maybe it was my SearchField class was doing things, so i replaced them with simple strings, the problem still occurs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您直接访问成员变量 - 您需要通过访问器 - 即:
这将确保您的对象保留字典。
You're directly accessing the member variable - you need to go through the accessor - ie:
This will make sure your object retains the dictionary.
通过将
test
设置为视图控制器标头的属性,确保它是一个类变量:@property (nonatomic, keep) NSDictionary *test;
在视图控制器的
-dealloc
方法中释放它:[test release], test = nil;
设置其值如下:
self.test = [NSDictionarydictionaryWithObjectsAndKeys:...];
访问其
count
属性,如下所示:NSLog(@"计数:%u", test.count);
或者:
NSLog(@"计数:%u", [测试计数]);
请注意,
-count
返回一个无符号整数,因此您应该使用%u
来格式化其值。Make sure
test
is a class variable, by setting it up as a property of your view controller's header:@property (nonatomic, retain) NSDictionary *test;
Release it in your view controller's
-dealloc
method:[test release], test = nil;
Set its value as follows:
self.test = [NSDictionary dictionaryWithObjectsAndKeys:...];
Access its
count
property as follows:NSLog(@"Count: %u", test.count);
Or:
NSLog(@"Count: %u", [test count]);
Note that
-count
returns an unsigned integer, so you should use%u
to format its value.您正在创建的字典是自动发布的。您正在使用一种便捷方法 (
dictionaryWithObjectsAndKeys:
),它返回一个新的自动释放对象。自动释放的对象在创建后一段时间被释放(并释放),这就是它在程序后期崩溃的原因。
您需要保留返回的字典,或者使用 alloc/init 方法创建一个新字典。
无论哪种方式,您都需要记住在使用完字典后释放它。
您应该阅读 Objective-C 的基本内存管理规则和约定。
The dictionary you're creating is autoreleased. You're using a convenience method (
dictionaryWithObjectsAndKeys:
) which returns a new autoreleased object.The autoreleased object is being released (and deallocated) some time after its creation, which is why it's crashing later in the program.
You need to retain the returned dictionary, or create a new dictionary using the alloc/init method.
Either way, you need to remember to release the dictionary when you're done with it.
You should read up on the basic memory management rules and conventions for Objective-C.