-1

这是我的主要内容:

  #import <UIKit/UIKit.h>

   #import "SimpleAppAppDelegate.h"
  #import "NewsFeed.h"
   #import "SimpleAppViewController.h"


        int main(int argc, char *argv[])
       {
        @autoreleasepool {
    return UIApplicationMain(argc, argv, nil,     NSStringFromClass([SimpleAppAppDelegate                              
    class]));    
    }
    }

这是它在命令中显示的错误消息:

NewsFeedAppTylerCrady[3837:f803]  
2012-10-11 11:15:21.226 NewsFeedAppTylerCrady[3837:f803] Processing Item: 
2012-10-11 11:15:21.227 NewsFeedAppTylerCrady[3837:f803] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFConstantString substringWithRange:]: Range or index out of bounds'
*** First throw call stack:
(0x13bc052 0x154dd0a 0x1364a78 0x13649e9 0x1389394 0x330f 0x30e8 0x2da8 0x2012 0xd964e 0x39a73 0x39ce2 0x39ea8 0x40d9a 0x11be6 0x128a6 0x21743 0x221f8 0x15aa9 0x12a6fa9 0x13901c5 0x12f5022 0x12f390a 0x12f2db4 0x12f2ccb 0x122a7 0x13a9b 0x3ad8 0x1c35)
terminate called throwing an exceptionCurrent language:  auto; currently objective-c
(gdb) 

我拥有的与此消息相对应的唯一方法是:

       -(NSString *) getTitleFromItem:(NSString *)item {
           NSRange titleOpen = [item rangeOfString:@"<title>"];
           NSRange titleClose = [item rangeOfString:@"</title>"];
           NSRange titleRange;
             titleRange.location = titleOpen.location+titleOpen.length;
       titleRange.length = titleClose.location - titleRange.location;
           return [item substringWithRange:titleRange];
       }

      -(NSString *) getDescriptionFromItem:(NSString *)item {
         NSRange descOpen = [item rangeOfString:@"<description>"];
          NSRange descClose = [item rangeOfString:@"</description>"];
          if(descOpen.location==NSNotFound)
              return @"No description available...";
         NSRange descRange;
         descRange.location = descOpen.location + descOpen.length;
          descRange.length = descClose.location - descRange.location;
          return [item substringWithRange:descRange];
      }
4

2 回答 2

3

在您的堆栈跟踪中:

...原因:'-[__NSCFConstantString substringWithRange:]:范围或索引超出范围'

看一下titleRangeand descRange,看起来它们可能超出了您的字符串范围。

于 2012-10-11T17:20:39.543 回答
2

在您的两个函数中添加一个检查

     -(NSString *) getTitleFromItem:(NSString *)item {
           NSRange titleOpen = [item rangeOfString:@"<title>"];
           NSRange titleClose = [item rangeOfString:@"</title>"];
           NSRange titleRange;
           titleRange.location = titleOpen.location+titleOpen.length;
           titleRange.length = titleClose.location - titleRange.location;
           if(titleRange.length <= item.length) // Check
                return [item substringWithRange:titleRange];
           else 
                return nil;
       }

另一个也一样。

于 2012-10-11T17:47:47.447 回答