挂进NSDictionary

时间:2014-05-30 11:35:33

标签: ios objective-c

我正在尝试制作NSMutableDictionary并覆盖iOS应用中默认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)

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

4 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

您的代码在此行崩溃:

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

此处fp8NSDictionary对象,但您将其用作NSArray对象。您可以使用objectForKey:方法检索对象或此行以获取对象:

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

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

答案 2 :(得分:0)

NSDictionary没有objectAtIndex方法。 fp8NSDictionary,因为它被定义为" id"编译器不会抱怨它。好像你忘了" ["在您调用方法的字典定义之前。它应该是Status: 0x22a1b740> initWithDictionary:[{

答案 3 :(得分:0)

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

如果你改变了

-(id)initWithDictionary:(id)fp8 {

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

然后Xcode会给你一个警告。