gpt4 book ai didi

ios - NSRange :rangeOfString throws an exception in AFHTTPRequestOperation's success block

转载 作者:行者123 更新时间:2023-11-28 20:08:38 25 4
gpt4 key购买 nike

我正在尝试获取 < > 标签之间的字符串。当在 AFHTTPRequestOperation 的成功 block 中调用“rangeOfString”方法时,应用程序抛出异常。当我从成功 block 中调用 rangeOfString 方法时,它可以正常工作。

...
...
NSString *Url = [NSString stringWithFormat:@"%@%@", BaseURLString, @"?q=services/session/token"];
[manager GET:Url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"CSRF Token: %@", responseObject);
//save CSRFToken to keychain
NSString *response = (NSString *) responseObject;
//below line throws a NSInvalidArgument exception.
NSRange range1 = [response rangeOfString:@"<"];
NSLog(@"range = %d", range1);
dispatch_semaphore_signal(semaphore);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
dispatch_semaphore_signal(semaphore);

}];

出现以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData rangeOfString:]: unrecognized selector sent to instance 0xd12ba50'
*** First throw call stack:
(
0 CoreFoundation 0x01e325e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01bb58b6 objc_exception_throw + 44
2 CoreFoundation 0x01ecf903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x01e2290b ___forwarding___ + 1019
4 CoreFoundation 0x01e224ee _CF_forwarding_prep_0 + 14
5 vd 0x00003287 __58-[VDLoginHTTPClient getCSRFTokenWithUserName:andPassword:]_block_invoke + 167

这可能是什么问题?问候...

最佳答案

您是在 NSData 而不是 NSString 上调用此消息。

您正在将 responseObject 转换为 NSString* - 这是错误的 - 可能您那里有 NSData - 检查 responseObject 的类型并将其正确转换为 NSString * - 而不是通过 (NSString*) 强制转换它。

如果你有 NSData,使用它:

NSString* responseString = [[[NSString alloc] initWithData:responseObject
encoding:NSUTF8StringEncoding] autorelease];

代替:

NSString *response = (NSString *) responseObject;

关于ios - NSRange :rangeOfString throws an exception in AFHTTPRequestOperation's success block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21143591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com