NSMutableDictionary 的 setObject 函数崩溃

发布于 2024-12-23 12:02:46 字数 724 浏览 0 评论 0原文

这是我的代码:

NSLog(@"dictionaryPlayers=%@,%d",[dictionaryPlayers description],dictionaryPlayers.count);
[dictionaryPlayers setObject:@"test" forKey:@"test2"];

dictionaryPlayers 在此类的 init 函数中初始化:

-(id)init{
  ...
  dictionaryPlayers = [[NSMutableDictionary dictionaryWithCapacity:10]retain];
  ...
}

程序崩溃:

Thread 1:Program received signal: "SIGABRT".

在控制台中:

2011-12-27 17:01:21.744 [25454:207] dictionaryPlayers={
},0
2011-12-27 17:01:21.745 [25454:207] -[__NSCFConstantString tick]: unrecognized selector sent to instance 0x199bcc

通过 NSLog 输出,我认为dictionaryPlayers 初始化得很好。所以不知道为什么会崩溃...

Here is my code:

NSLog(@"dictionaryPlayers=%@,%d",[dictionaryPlayers description],dictionaryPlayers.count);
[dictionaryPlayers setObject:@"test" forKey:@"test2"];

dictionaryPlayers is inited in this class's init function:

-(id)init{
  ...
  dictionaryPlayers = [[NSMutableDictionary dictionaryWithCapacity:10]retain];
  ...
}

The program crashed:

Thread 1:Program received signal: "SIGABRT".

And in console:

2011-12-27 17:01:21.744 [25454:207] dictionaryPlayers={
},0
2011-12-27 17:01:21.745 [25454:207] -[__NSCFConstantString tick]: unrecognized selector sent to instance 0x199bcc

With the NSLog outputs, i think dictionaryPlayers is well inited. So I don't know why crashed...

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

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

发布评论

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

评论(4

萝莉病 2024-12-30 12:02:47

dictionaryPlayers = [NSMutableDictionary dictionaryWithCapacity:10]; 

从 init() 中删除,并

dictionaryPlayers = [[NSMutableDictionary alloc] init];

在日志语句上方添加。还是崩溃了....
然后我删除了

[dicTest setValue:@"Test" forKey:@"testKey"];

所以只剩下两行:

dictionaryPlayers = [[NSMutableDictionary alloc] init];
NSLog(@"dictionaryPlayers=%@,%d",[dictionaryPlayers description],dictionaryPlayers.count);

它没有崩溃。所以,看来 setValue 行确实是问题所在。

I removed

dictionaryPlayers = [NSMutableDictionary dictionaryWithCapacity:10]; 

from init(), and added

dictionaryPlayers = [[NSMutableDictionary alloc] init];

above my log statement. Still crash....
Then I removed

[dicTest setValue:@"Test" forKey:@"testKey"];

So there's only 2 lines left:

dictionaryPlayers = [[NSMutableDictionary alloc] init];
NSLog(@"dictionaryPlayers=%@,%d",[dictionaryPlayers description],dictionaryPlayers.count);

It didn't crash. So, it seems the setValue line really is the problem.

烙印 2024-12-30 12:02:47

您不需要在 init 语句中对对象调用保留。也只是为了咯咯地笑,尝试:

dictionaryPlayers = [[NSMutableDictionary alloc] init];

而不是在

dictionaryPlayers = [NSMutableDictionary dictionaryWithCapacity:10];

日志语句上方执行此操作(将其从 init 中取出)。

如果这有效,请将日志放入您的 init 方法中,并确保在将 KV 添加到字典的方法之前调用该方法,

我无法重现此行为。这是我的代码:
ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(nonatomic, retain) NSMutableDictionary *dictionaryPlayers;
@property (retain, nonatomic) IBOutlet UITextView *logTextView;
- (IBAction)logButtonPressed:(id)sender;
@end

ViewController.m:

#import "ViewController.h"

@implementation ViewController
@synthesize dictionaryPlayers;
@synthesize logTextView;


#pragma mark - My Methods
- (IBAction)logButtonPressed:(id)sender {
    logTextView.text = [NSString stringWithFormat:@"%@,%d",[dictionaryPlayers description],[dictionaryPlayers count]];
    NSLog(@"dictionaryPlayers=%@,%d",[dictionaryPlayers description],[dictionaryPlayers count]);
}

#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    dictionaryPlayers = [[NSMutableDictionary alloc] init];


    [dictionaryPlayers setValue:@"Test" forKey:@"testKey"];
    NSLog(@"dictionaryPlayers=%@,%d",[dictionaryPlayers description],[dictionaryPlayers count]);
}

没有问题。如果你这样做的话,就不会有问题。以下是我进一步排除故障所采取的步骤:

  1. 对playersDictionary 进行项目搜索,并确保没有其他东西接触该对象。
  2. 尝试清理项目
  3. 使用此结构创建一个新项目,看看会发生什么

You don't need to call retain on the object in your init statement. Also just for giggles try:

dictionaryPlayers = [[NSMutableDictionary alloc] init];

instead of

dictionaryPlayers = [NSMutableDictionary dictionaryWithCapacity:10];

And do this right above your log statement (take it out of the init).

If this works, put a log in your init method and make sure that is being called before your method that adds KV's to the dictionary

I cannot reproduce this behavior. Here is my code:
ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(nonatomic, retain) NSMutableDictionary *dictionaryPlayers;
@property (retain, nonatomic) IBOutlet UITextView *logTextView;
- (IBAction)logButtonPressed:(id)sender;
@end

ViewController.m:

#import "ViewController.h"

@implementation ViewController
@synthesize dictionaryPlayers;
@synthesize logTextView;


#pragma mark - My Methods
- (IBAction)logButtonPressed:(id)sender {
    logTextView.text = [NSString stringWithFormat:@"%@,%d",[dictionaryPlayers description],[dictionaryPlayers count]];
    NSLog(@"dictionaryPlayers=%@,%d",[dictionaryPlayers description],[dictionaryPlayers count]);
}

#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    dictionaryPlayers = [[NSMutableDictionary alloc] init];


    [dictionaryPlayers setValue:@"Test" forKey:@"testKey"];
    NSLog(@"dictionaryPlayers=%@,%d",[dictionaryPlayers description],[dictionaryPlayers count]);
}

No issues. If you are doing things this way, you cannot have problems. Here are the steps I would take to troubleshoot further:

  1. Do a project search for playersDictionary and make sure there isn't something else touching that object.
  2. Try cleaning the project
  3. Create a new project with this structure and see what happens
指尖上的星空 2024-12-30 12:02:46

您调用 tick: 的对象不再位于内存中并导致此崩溃。尝试看看为什么这个对象被释放。

The object on which you call tick: is not longer in memory and causes this crash. Try to see why this object is released.

荭秂 2024-12-30 12:02:46

看来这不是本地案例,所以你确定在上面综合了吗?并在标头中正确声明它?示例:
在标题中:

@property (nonatomic, strong) NSMutableDictionary *dictionaryPlayers;

在类中:

@synthesize dictionaryPlayers = _dictionaryPlayers;

It seems that this is not a local case, so did you make sure to synthesize it at the top? And declared it correctly in the header? Examples:
In header:

@property (nonatomic, strong) NSMutableDictionary *dictionaryPlayers;

in class:

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