获取[__ NSCFString count]的NSMutableArray:发送到实例的无法识别的选择器

时间:2012-09-08 01:51:44

标签: objective-c ios xcode uitableview nsmutablearray

我真的需要一些帮助...

当我尝试使用NSMutableArray获取节的单元格数时,我得到[__NSCFString count]:无法识别的选择器发送到实例0x6d567e0

当我使用“rows = [[searchProfile objectAtIndex:section] count];”

时会发生这种情况

以下是代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"1");
    // _________ Return the number of sections ________
    int sections =  0;

    if (tableView == self.tableView)
    {
        sections = 2;
    }
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        sections = [searchedProfile count];
    }
    return sections;
}


/* ------------------------------------------ Set The Number Of Cells for Each Section ------------------------------- */


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"2");

    // _________  Return the number of rows in the section. ________
    int rows = 0;

    if (tableView == self.tableView)
    {
        if (section == 0) {
            rows = userProfile.count;}
        else {
            rows = profiles.count;}
    }

    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        rows = [[searchedProfile objectAtIndex:section]count];
    }
    return rows;
}

将字符串插入“searchingProfile”数组的方法:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    searchedKeys = [NSMutableArray array];
    searchedProfile = [NSMutableArray array];
    searchedData = [NSMutableDictionary dictionary];

    [searchedProfile removeAllObjects];
    [searchedData removeAllObjects];
    [searchedKeys removeAllObjects];


        NSDictionary *profile;

        for (profile in profiles)
        {
            NSString *birthname;
            NSString *currentname;
            NSString *photopath;
            NSDate *birthdate;

            for (birthname in profile)
            {
                birthname = [profile objectForKey:@"birthname"];
                currentname = [profile objectForKey:@"currentname"];
                photopath = [profile objectForKey:@"profilepic"];
                birthdate = [profile objectForKey:@"birthdate"];
                NSRange nameRange = [birthname rangeOfString:searchString options:NSCaseInsensitiveSearch];

                if (nameRange.location != NSNotFound)
                {
                    [searchedProfile addObject:birthname];
                    [searchedData setObject:birthname forKey:@"birthname"];
                    [searchedData setObject:currentname forKey:@"currentname"];
                    [searchedData setObject:photopath forKey:@"profilepic"];
                    [searchedData setObject:birthdate forKey:@"birthdate"];
                    if ([birthname isEqualToString:[profile objectForKey:@"birthname"]])
                        break;
                }
            }
        }
    NSLog(@"SEARCHED PROFILES: %@", searchedProfile);
    NSLog(@"SEARCHED DATA:%@", searchedData);
    NSLog(@"SEARCHED KEYS:%@", searchedKeys);
    NSLog(@"count test: %d", searchedProfile.count);
    return YES;
}

谢谢!

3 个答案:

答案 0 :(得分:4)

看起来你正在NSSrray上使用count方法在NSString上,这是不允许的。您将NSStrings存储在数组searchedProfile中,看起来您想要找出字符串的长度。所以不要这样:

    rows = [[searchedProfile objectAtIndex:section] count];

这样做:

    rows = [[searchedProfile objectAtIndex:section] length];

答案 1 :(得分:0)

[searchingProfile objectAtIndex:section] 将返回字符串,因为在searchingProfile中的对象是字符串,因为这是你在

之前添加对象的方式
 [searchedProfile addObject:birthname];

count 不是为NSString定义的属性。无论如何,似乎你想要返回对象的数量(即存储在searchingProfile数组中的birthnames的数量)作为表中的行数,试试这个

rows = [searchedProfile count];

答案 2 :(得分:0)

保留“searchingProfile”数组:searchingProfile = [[NSArray array] retain];