应用程序崩溃与NSInvalidArgumentException

时间:2014-10-06 18:07:15

标签: ios objective-c

我试图在地图视图中添加注释。每个注释代表一条推文。我从twitter api获取每条推文的坐标。应用程序在加载地图后立即崩溃。而且我不确定为什么。

编辑:这是相关代码的当前版本。它运行正常,直到它发送tweetLocation为null的推文。然后它崩溃了。

STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"key" consumerSecret:@"secret"];

    [twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {

        [twitter getSearchTweetsWithQuery:@"apple" geocode:geoCode lang:nil locale:nil resultType:@"recent" count:@"10" until:nil sinceID:nil maxID:nil includeEntities:nil callback:nil successBlock:^(NSDictionary *searchMetadata, NSArray *statuses) {

            tweetsArray = [NSMutableArray arrayWithArray:statuses];
            NSLog(@"tweetsArray: %@", tweetsArray);

            for (int i = 0; i < [tweetsArray count]; i++) {
                NSDictionary *t = tweetsArray[i];
                tweetAnnotation = [[MKPointAnnotation alloc]init];

                NSArray *tweetLocation = [[t valueForKey:@"geo"]valueForKey:@"coordinates"];
                NSLog(@"Tweet Location: %@", tweetLocation);

                if ([tweetLocation count] == 0) {
                    continue;
                }
                else {
                    double lat = [[tweetLocation objectAtIndex:0]doubleValue];
                    double longitude = [[tweetLocation objectAtIndex:1]doubleValue];
                    locationCoord.latitude = lat;
                    locationCoord.longitude = longitude;

                    twitterName = [t valueForKeyPath:@"user.screen_name"];
                    NSLog(@"Twitter name: %@", twitterName);
                    tweetAnnotation.title = twitterName;

                    tweetAnnotation.coordinate = locationCoord;
                    [tweetLocationsArray addObject:tweetAnnotation];
                }
            }

            [tweetMap addAnnotations:tweetLocationsArray];

        } errorBlock:^(NSError *error) {

            NSLog(@"%@", error.debugDescription);

        }];

    } errorBlock:^(NSError *error) {

        NSLog(@"%@", error.debugDescription);

    }];

编辑:这是控制台中的当前错误:

2014-10-07 08:00:36.278 Tweets By Location[5244:382886] Tweet Location: (
    "37.33158914",
    "-122.03076495"
)
2014-10-07 08:00:36.278 Tweets By Location[5244:382886] Twitter name: tofuhunter
2014-10-07 08:00:36.278 Tweets By Location[5244:382886] Tweet Location: (
    "37.33161957",
    "-122.03080173"
)
2014-10-07 08:00:36.278 Tweets By Location[5244:382886] Twitter name: treynesss
2014-10-07 08:00:36.278 Tweets By Location[5244:382886] Tweet Location: (
    "37.33161957",
    "-122.03080173"
)
2014-10-07 08:00:36.279 Tweets By Location[5244:382886] Twitter name: mattlarowe
2014-10-07 08:00:36.279 Tweets By Location[5244:382886] Tweet Location: <null>
2014-10-07 08:00:36.279 Tweets By Location[5244:382886] -[NSNull count]: unrecognized selector sent to instance 0x10b0accb0
2014-10-07 08:00:36.281 Tweets By Location[5244:382886] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull count]: unrecognized selector sent to instance 0x10b0accb0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010ae043f5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010aa9dbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010ae0b50d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010ad637fc ___forwarding___ + 988
    4   CoreFoundation                      0x000000010ad63398 _CF_forwarding_prep_0 + 120
    5   Tweets By Location                  0x000000010a08876e __32-[MapViewController viewDidLoad]_block_invoke_2 + 542
    6   Tweets By Location                  0x000000010a029feb __147-[STTwitterAPI getSearchTweetsWithQuery:geocode:lang:locale:resultType:count:until:sinceID:maxID:includeEntities:callback:successBlock:errorBlock:]_block_invoke + 203
    7   Tweets By Location                  0x000000010a01cd1a __99-[STTwitterAPI getResource:baseURLString:parameters:downloadProgressBlock:successBlock:errorBlock:]_block_invoke117 + 186
    8   Tweets By Location                  0x000000010a0751d8 __95-[STTwitterAppOnly getResource:baseURLString:parameters:progressBlock:successBlock:errorBlock:]_block_invoke104 + 184
    9   Tweets By Location                  0x000000010a065a32 __158+[STHTTPRequest(STTwitter) twitterRequestWithURLString:stTwitterUploadProgressBlock:stTwitterDownloadProgressBlock:stTwitterSuccessBlock:stTwitterErrorBlock:]_block_invoke33 + 722
    10  Tweets By Location                  0x000000010a071538 -[STHTTPRequest connectionDidFinishLoading:] + 472
    11  CFNetwork                           0x00000001105212cc __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 69
    12  CFNetwork                           0x0000000110521270 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 199
    13  CFNetwork                           0x00000001105213d7 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 48
    14  CFNetwork                           0x00000001103f25f7 ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 107
    15  CFNetwork                           0x00000001104be871 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 273
    16  CFNetwork                           0x00000001103ddb36 _ZN19RunloopBlockContext13_invoke_blockEPKvPv + 72
    17  CoreFoundation                      0x000000010ad0c714 CFArrayApplyFunction + 68
    18  CFNetwork                           0x00000001103dd9f7 _ZN19RunloopBlockContext7performEv + 133
    19  CFNetwork                           0x00000001103dd836 _ZN17MultiplexerSource7performEv + 256
    20  CFNetwork                           0x00000001103dd64c _ZN17MultiplexerSource8_performEPv + 72
    21  CoreFoundation                      0x000000010ad39ad1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    22  CoreFoundation                      0x000000010ad2f99d __CFRunLoopDoSources0 + 269
    23  CoreFoundation                      0x000000010ad2efd4 __CFRunLoopRun + 868
    24  CoreFoundation                      0x000000010ad2ea06 CFRunLoopRunSpecific + 470
    25  GraphicsServices                    0x000000010cf2f9f0 GSEventRunModal + 161
    26  UIKit                               0x000000010b277550 UIApplicationMain + 1282
    27  Tweets By Location                  0x000000010a089e43 main + 115
    28  libdyld.dylib                       0x000000010cb78145 start + 1
    29  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

3 个答案:

答案 0 :(得分:1)

崩溃日志中的这一行:

2014-10-06 14:18:08.622 Tweets By Location[3612:174001] -[__NSCFArray length]: unrecognized selector sent to instance 0x7fa983d03510

表示您正在向数组发送length,但数组没有名为length的方法。

原因是这段代码:

NSString *tweetLocation = [[t valueForKey:@"geo"]valueForKey:@"coordinates"];
NSLog(@"Tweet Location: %@", tweetLocation);

if ([tweetLocation length] == 0) {
    continue;
}

您假设geo.coordinates处的任何内容都是字符串,但服务器返回一个数组。

要修复它,请查看服务器应返回的规范,并调整代码以符合规范。如果服务器可能返回该字段的数组或字符串,则可以使用isKindOfClass:测试在运行时返回的数据。

答案 1 :(得分:1)

元素"coordinates"不是字符串。

根据文档,您正在遍历的对象是Tweet对象: https://dev.twitter.com/overview/api/tweets

coordinates部分与此示例类似:

  "coordinates":
  [
    -75.14310264,
    40.05701649
  ]

您应该将coordinates解析为表示(经度,纬度)对的数组。

请注意,coordinates可能为空(与[NSNull null]相比),因此您也应该考虑到这一点。

答案 2 :(得分:0)

我弄清楚它崩溃的原因。我必须检查坐标JSON数组是否等于NSNull。

if ([tweetLocation isEqual:[NSNull null]]) {
                    continue;
                }
                else {
                    double lat = [[tweetLocation objectAtIndex:0]doubleValue];
                    double longitude = [[tweetLocation objectAtIndex:1]doubleValue];
                    locationCoord.latitude = lat;
                    locationCoord.longitude = longitude;
                    NSLog(@"Lat: %.8f, Long: %.8f", lat, longitude);

                    twitterName = [t valueForKeyPath:@"user.screen_name"];
                    NSLog(@"Twitter name: %@", twitterName);
                    tweetAnnotation.title = twitterName;

                    tweetAnnotation.coordinate = locationCoord;
                    [tweetLocationsArray addObject:tweetAnnotation];
                }