致电 NSDictionary 后我得到一些不相关的答案

发布于 2024-12-12 01:13:30 字数 1397 浏览 0 评论 0原文

我尝试了下面的代码:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


NSString * str1 = @"program";

NSArray * alphabets = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil];
NSMutableDictionary * alphaToNum = [NSMutableDictionary dictionary];
NSMutableDictionary * numToAlpha = [NSMutableDictionary dictionary];

NSInteger index =0;
int shifter = 5;

for(NSString * character in alphabets){
    index++;
    [alphaToNum setObject:[NSNumber numberWithInteger:index] forKey:character];
    [numToAlpha setObject:character forKey:[NSNumber numberWithInteger:index]];
}

NSRange * rng = {0,0};
NSLog(@"-------");
//NSLog(@"%@" , [str1 substringWithRange:NSMakeRange(1, 1)]);

for(int i=0;i<[str1 length];i++)
{   
    NSString * ss = [str1 substringWithRange:NSMakeRange(i, 1)];
    NSInteger  i = [alphaToNum valueForKey:ss];

    i = (i + 5);
            NSLog(@"%d",i); //last code
}

这是我得到的最后一个代码

2011-10-24 13:20:08.289 cipher[22732:a0f] 1103808

2011-10-24 13:20:08.290 cipher[22732:a0f] 1101168

2011-10-24 13:20:08.290 密码[22732:a0f] 1103200

2011-10-24 13:20:08.290 密码[22732:a0f] 1103616

2011-10-24 13:20:08.291 cipher[22732:a0f] 1103584

甚至有时是

“EXC_BAD_ACCESS”。

这是完全错误的!

I tried below code :

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


NSString * str1 = @"program";

NSArray * alphabets = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil];
NSMutableDictionary * alphaToNum = [NSMutableDictionary dictionary];
NSMutableDictionary * numToAlpha = [NSMutableDictionary dictionary];

NSInteger index =0;
int shifter = 5;

for(NSString * character in alphabets){
    index++;
    [alphaToNum setObject:[NSNumber numberWithInteger:index] forKey:character];
    [numToAlpha setObject:character forKey:[NSNumber numberWithInteger:index]];
}

NSRange * rng = {0,0};
NSLog(@"-------");
//NSLog(@"%@" , [str1 substringWithRange:NSMakeRange(1, 1)]);

for(int i=0;i<[str1 length];i++)
{   
    NSString * ss = [str1 substringWithRange:NSMakeRange(i, 1)];
    NSInteger  i = [alphaToNum valueForKey:ss];

    i = (i + 5);
            NSLog(@"%d",i); //last code
}

and this is what I get for the last code

2011-10-24 13:20:08.289 cipher[22732:a0f] 1103808

2011-10-24 13:20:08.290 cipher[22732:a0f] 1101168

2011-10-24 13:20:08.290 cipher[22732:a0f] 1103200

2011-10-24 13:20:08.290 cipher[22732:a0f] 1103616

2011-10-24 13:20:08.291 cipher[22732:a0f] 1103584

or even sometimes

“EXC_BAD_ACCESS”.

which is totally wrong!

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

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

发布评论

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

评论(1

拿命拼未来 2024-12-19 01:13:30
NSInteger  i = [alphaToNum valueForKey:ss];

这将返回一个 NSNumber 对象,而不是一个 NSInteger。

如下所示,

NSInteger  i = [[alphaToNum valueForKey:ss] integerValue];

关于崩溃,我们只能在查看崩溃日志后才能回答。那会更有帮助。

NSInteger  i = [alphaToNum valueForKey:ss];

This will return an NSNumber object, not an NSInteger.

Make that line as follows,

NSInteger  i = [[alphaToNum valueForKey:ss] integerValue];

And regarding the crash, we can answer only after looking at the crash log. That would be more helpful.

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