【问题标题】:Hooking into a NSDictionary [duplicate]挂钩到 NSDictionary [重复]
【发布时间】:2014-05-30 11:35:32
【问题描述】:

我正在尝试在 iOS 应用中创建 NSMutableDictionary 并覆盖默认 NSDictionary 的部分内容。我使用的代码编译没有问题,但我显然遗漏了一些东西。

我有这个NSLog 的一个函数:

Status: 0x22a1b740> initWithDictionary:{
  ability =     (
                    {
                id = 1001;
            }
        );
        c = 10000;
        l = 1;
        p = 4;
        t = 5;
    }]

我尝试使用此代码创建NSMutableDictionary 以将键“c”更改为 20000;

%hook Status
-(id)initWithDictionary:(id)fp8 {
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
NSDictionary *oldDict = (NSDictionary *)[fp8 objectAtIndex:0];
[newDict addEntriesFromDictionary:oldDict];
[newDict setObject:@"20000" forKey:@"c"];
[fp8 replaceObjectAtIndex:0 withObject:newDict];
[newDict release];
}
return %orig;
}
%end

我收到此崩溃报告:

May 30 13:02:54 Fangs-iPad ReportCrash[2404]: -[__NSDictionaryI objectAtIndex:]: unrecognized selector sent to instance 0x22bd6910
May 30 13:02:54 Fangs-iPad ReportCrash[2404]: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI objectAtIndex:]: unrecognized selector sent to instance 0x22bd6910'
    *** First throw call stack:
    (0x322e73e7 0x3a181963 0x322eaf31 0x322e964d 0x32241208 0x557d98 0x56791c 0x16789d 0x16530f 0x1644dd 0x16851f 0x7af3f 0x340ff471 0x322bc941 0x322bac39 0x322baf93 0x3222e23d 0x3222e0c9 0x35de933b 0x3414a2b9 0x7425f 0x74218)

你知道我在这里做错了什么吗?非常感谢任何帮助:)

【问题讨论】:

  • 谷歌“无法识别的选择器”并研究几个“命中”。然后,如果你想不通,回来把你不明白的地方解释清楚。

标签: ios objective-c


【解决方案1】:

问题在于这一行:

NSDictionary *oldDict = (NSDictionary *)[fp8 objectAtIndex:0];

fp8 不是数组,它是NSDictionary

所以你需要这样的东西:

-(id)initWithDictionary:(id)fp8
{
   NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
   NSDictionary *oldDict = (NSDictionary *)[(NSArray *)[fp8 objectForKey:@"ability"] objectAtIndex:0];
   [newDict addEntriesFromDictionary:oldDict];
   [newDict setObject:@"20000" forKey:@"c"];
   [fp8 replaceObjectAtIndex:0 withObject:newDict];
   [newDict release];
}

【讨论】:

  • 非常感谢您的尝试,但似乎以同样的方式崩溃。
  • 字典名称显然是“代码”,它包含以下内容:ability = ( { id = 1001; } ); c = 10000; l = 1; p = 4; t = 5; }
【解决方案2】:

你的代码在这一行崩溃了:

NSDictionary *oldDict = (NSDictionary *)[fp8 objectAtIndex:0];

这里fp8 是一个NSDictionary 对象,但您将它用作NSArray 对象。您可以使用objectForKey: 方法检索对象或此行来获取对象:

NSDictionary *oldDict = (NSDictionary *)[[fp8 allValues] objectAtIndex:0];

否则看fp8对象的结构,因为你认为它应该是一个数组类型的对象,但实际上它是一个字典。

【讨论】:

  • -[NSDictionary allValues] 返回的对象顺序未定义,因此不能依赖。
【解决方案3】:

NSDictionary 没有objectAtIndex 方法。 fp8NSDictionary 并且因为它被定义为“id”编译器不会抱怨它。看来您在调用该方法的字典定义之前忘记了“[”。应该是Status: 0x22a1b740> initWithDictionary:[{

【讨论】:

  • [ 实际上在 [Status.那部分只是来自 NSlog,不是我的钩子的一部分
【解决方案4】:

您无法使用 objectAtIndex: 访问 NSDictionary 中的对象 - 请改用 objectForKey:

如果你改变了

-(id)initWithDictionary:(id)fp8 {

-(id)initWithDictionary:(NSDictionary *)fp8 {

那么 Xcode 会给你一个警告。

【讨论】:

  • 谢谢尼克。如果我这样做,它确实不起作用 -(id)initWithDictionary:(NSDictionary *)fp8 我实际上是先这样做的,但由于它没有编译,所以改为 id ,我猜想解决问题的坏举动......我基本上想改变字典键 c 通过使用编辑后的值创建 NSMutableDIctionary,但无法使其工作..
猜你喜欢
  • 1970-01-01
  • 2010-10-23
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
  • 2022-01-02
  • 2015-08-08
相关资源
最近更新 更多