xcode NSMutableArray 获取 [__NSCFString count]:无法识别的选择器发送到实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12327411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSMutableArray getting [__NSCFString count]: unrecognized selector sent to instance
提问by Razvan
I really need some help...
我真的需要一些帮助...
When I try to get the number of cells for section using a NSMutableArray, I get [__NSCFString count]: unrecognized selector sent to instance 0x6d567e0
当我尝试使用 NSMutableArray 获取部分的单元格数时,我得到 [__NSCFString count]: unrecognized selector sent to instance 0x6d567e0
This happens when I use "rows = [[searchProfile objectAtIndex:section] count];"
当我使用“rows = [[searchProfile objectAtIndex:section] count];”时会发生这种情况
Here is the code:
这是代码:
- (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;
}
And the method that inserts strings into "searchedProfile" array:
以及将字符串插入“searchedProfile”数组的方法:
- (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;
}
Thank you !
谢谢 !
回答by pasawaya
It looks like you're using the count
method for NSArray's on an NSString, which is not allowed. You store NSStrings in the array searchedProfile
, and it looks like you want to find out the length of a string. So instead of this:
看起来您在count
NSString 上使用NSArray 的方法,这是不允许的。您将 NSStrings 存储在数组中searchedProfile
,看起来您想找出字符串的长度。所以而不是这个:
rows = [[searchedProfile objectAtIndex:section] count];
Do this:
做这个:
rows = [[searchedProfile objectAtIndex:section] length];
回答by Neo
[searchedProfile objectAtIndex:section]will return string as the object in searchedProfile is string because this is how u added object before
[searchedProfile objectAtIndex:section]将返回字符串,因为 searchedProfile 中的对象是字符串,因为这是您之前添加对象的方式
[searchedProfile addObject:birthname];
and countisnt the property defined for NSString. Anyways, it seems you want to return number of objects(i.e. number of birthnames stored in the searchedProfile array ) as the number of rows in the table, try this
并且count不是为 NSString 定义的属性。无论如何,您似乎想返回对象数(即存储在 searchedProfile 数组中的出生姓名数)作为表中的行数,试试这个
rows = [searchedProfile count];
回答by Raghunandan R
Retain "searchedProfile" array by doing: searchedProfile = [[NSArray array] retain];
通过执行以下操作保留“searchedProfile”数组:searchedProfile = [[NSArray array] retain];