nsstring导致我的应用程序崩溃

时间:2015-07-14 10:55:59

标签: ios objective-c ios8 nsdictionary

我创建了一个包含post params的字典。

NSDictionary *post_params = [NSDictionary dictionaryWithObjectsAndKeys:
[[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString], @"advertisingId",
[[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleShortVersionString"], @"appVersion",
[[UIDevice currentDevice]systemVersion], @"deviceSystemVersion",
[[NSUserDefaults  standardUserDefaults] objectForKey:@"apnsToken"], @"apnsToken",
[[NSUserDefaults  standardUserDefaults] objectForKey:@"userId"], @"userId",nil];

使用以下代码将上述帖子参数附加到帖子请求中:

for (id key in params) {
    [body appendData:[[NSString stringWithString:[params objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
}

上述代码在send_post_request函数中执行。

我收到了此崩溃报告

0   CoreFoundation                  0x182a982d8 __exceptionPreprocess + 132 (NSException.m:162)
1   libobjc.A.dylib                 0x1947640e4 objc_exception_throw + 60 (objc-exception.mm:527)
2   CoreFoundation                  0x182a9f3a4 __methodDescriptionForSelector + 0 (NSObject.m:368)
3   CoreFoundation                  0x182a9c154 ___forwarding___ + 928 (NSForwarding.m:2878)
4   CoreFoundation                  0x18299eccc _CF_forwarding_prep_0 + 92 (NSForwarding.s:772)
5   Foundation                      0x1838cc864 -[NSPlaceholderString initWithString:] + 180 (NSString.m:1973)
6   Foundation                      0x1838d2e14 +[NSString stringWithString:] + 56 (NSString.m:152)
7   ZVSDV                           0x100084f70 +[ZVSDVutableURLRequest send_post_request:params] + 800 (ZVSDVutableURLRequest.m:79)

此报告显示应用程序在执行此行代码时崩溃:

[body appendData:[[NSString stringWithString:[params objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];

这可能是出于这个原因:

  1. [params objectForKey:key]返回nil值。但这不可能发生。因为NSDictionary不能保持零值。

  2. [params objectForKey:key]返回整数。我确信NSUserDefaults返回NSString个对象。其他参数对象应为NSString

  3. 可能是什么问题?

1 个答案:

答案 0 :(得分:2)

for (id key in params) { if (![[params objectForKey:key] isKindOfClass:[NSString class]]) { NSLog(@"the object for key '%@' is not a string. It's %@", key, NSStringFromClass([[params objectForKey:key] class])); } } 中的一个对象,而不是NSString。

运行此选项以结帐

SqlConnection