【发布时间】:2012-02-04 07:37:10
【问题描述】:
我在头文件中声明了一个字符串,如下所示:
@property (nonatomic, retain) NSString *resultOfHash;
我这样调用我的 getHash 方法:
NSString *hash = [self getHash];
我的getHash方法是:
-(NSString *) getHash
{
//Get username form Keychain
KeychainItemWrapper *keyChain = [[KeychainItemWrapper alloc] initWithIdentifier:KeyChainName accessGroup:nil];
username = [keyChain objectForKey:(__bridge id)kSecAttrAccount];
//get token from NSUserDefauls
NSString *token = [[NSUserDefaults standardUserDefaults]objectForKey:@"Token"];
NSString *toHash = [[username stringByAppendingString:HashExtra] stringByAppendingString:token];
const char *s = [toHash cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
CC_SHA512(keyData.bytes, keyData.length, digest);
NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
//convert to string
resultOfHash = [out description];
//App crashed out above
// get rid of unwanted characters
resultOfHash = [resultOfHash stringByReplacingOccurrencesOfString:@" " withString:@""];
resultOfHash = [resultOfHash stringByReplacingOccurrencesOfString:@"<" withString:@""];
resultOfHash = [resultOfHash stringByReplacingOccurrencesOfString:@">" withString:@""];
//log to make sure it works
NSLog(@"hash is: %@", resultOfHash);
return resultOfHash;
}
我的代码在以下行崩溃:ResultOfHash = [out description];但我不知道为什么。
当我使用局部变量时,转换工作正常,但我无法从 getHash 方法返回局部变量。示例:
替换 ResultOfHash = [out description];
与
NSString *local = [out description];
return local;
并且转换工作正常,当我逐行调试时,调试器将转到我的方法的右括号,然后产生 EXC_BAD_ACCESS 错误。
我试过运行 NSZombie,但什么也没找到。
如果能帮助您解决这个问题,我们将不胜感激。
【问题讨论】:
-
你有没有合成字符串resultOfHash???
标签: iphone string exc-bad-access header-files