【问题标题】:*** -[CFString rangeOfString:options:]: message sent to deallocated instance 0xbe253f0*** -[CFString rangeOfString:options:]: 消息发送到已释放实例 0xbe253f0
【发布时间】:2010-10-02 15:59:13
【问题描述】:

我创建了一个带有搜索栏的 UITableView。首次访问 TableView 并搜索一切正常。当视图卸载并且您返回搜索选项并尝试再次搜索时,就会出现问题。

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.listContent = [[NSMutableArray alloc] initWithCapacity:20];
        self.filteredListContent = [NSMutableArray arrayWithCapacity:[listContent count]];
        ...
    }

    - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
        [self.filteredListContent removeAllObjects]; // First clear the filtered array.

        /*
         Search the main list for buildings whose name match searchText; add items that match to the filtered array.
         */


        for (NSArray*rows in self.listContent)
        {
            for (NSDictionary *row  in rows)
            {
                NSString *locationName =  [row objectForKey:@"Name"];

                // TODO: BUG - *** -[CFString rangeOfString:options:]: message sent to deallocated instance 0xbe253f0
                NSRange titleResultsRange = [locationName rangeOfString:searchText options:NSCaseInsensitiveSearch];

                if (titleResultsRange.length > 0) 
                {
                    [filteredListContent addObject:row];
                }
            }
        }
    }


    -(void) locationsReceived:(NSData *)data {

        NSString *jsonData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        // Create a dictionary from the JSON string
        NSArray *items = [[[jsonData JSONValue] objectForKey:@"ResultSet"] objectForKey:@"Location"];

        // Create a dictionary from the JSON string
        [listContent release];
        listContent = nil;
        self.listContent = [[NSMutableArray alloc] initWithCapacity:20];

        for (NSDictionary *section in  sectionTitles) {

            NSMutableArray *rows = [[NSMutableArray alloc] initWithCapacity:20];

            [self.listContent addObject:rows];

            for (NSDictionary *item in items) {

                NSInteger sectionCampus = [[section objectForKey:@"CampusID"] intValue];
                NSInteger rowCampus = [[item objectForKey:@"CampusID"] intValue];

                if (sectionCampus == rowCampus ) {
                    //NSDictionary *newDic = [[NSDictionary alloc] initWithDictionary:item copyItems:YES];
                    //[rows addObject:newDic ];
                    //[newDic release];
                    [rows addObject:item ];

                } else {
                    break;
                }
            }
        }
    }


- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    /*
     Update the filtered array based on the search text and scope.
     */

    [self.filteredListContent removeAllObjects]; // First clear the filtered array.

    /*
     Search the main list for buildings whose name matches searchText; add items that match to the filtered array.
     */
    for (NSArray*rows in listContent)
    {
        for (NSDictionary *row  in rows)
        {
            NSString *locationName =  [row objectForKey:@"Name"];

            // TODO: BUG - *** -[CFString rangeOfString:options:]: message sent to deallocated instance 0xbe253f0
            NSRange titleResultsRange = [locationName rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (titleResultsRange.length > 0) 
            {
                [filteredListContent addObject:row];
            }
        }
    }
}

【问题讨论】:

  • 感谢格式化部分代码,下次请格式化所有代码
  • 您是否正在向某处的listContent 发送发布消息?以及如何用值填充该数组?
  • 我在 dealloc self.listContent = nil; 中执行以下操作
  • 请回答我的第二个问题。
  • 我添加了填充数组的代码。谢谢。

标签: iphone ios4


【解决方案1】:

我宁愿清空-(void) locationsReceived:(NSData *)data 中的现有listContent 而不是分配一个新的。

【讨论】:

  • 我在 -(void) locationsReceived:(NSData *)data 中将代码更改为 [self.listContent removeAllObjects],但仍然出现同样的错误。
  • 我只能猜测:也许[jsonData JSONValue] 没有保留它返回的值?也许您的其他代码正在做一些奇怪的事情,而您的摘录中没有。
  • 我认为问题是当我将搜索结果过滤到filteredListContent 列表中时,我正在将listContent 数组中对字典对象的引用添加到filteredListContent 数组中。但我认为问题可能是我正在调用 [self.filteredListContent removeAllObjects],这可能是从 listContent 数组中删除字典对象。我将尝试深度复制字典对象,看看是否能解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 2011-06-16
相关资源
最近更新 更多