我有一系列名字,想要通过A& A来计算它们。 B前缀。 例如,对于此用户名,我希望我的 NSMutableDictionary * cellNum 返回以每个字母开头的名称计数,例如:“A”:“2”& “B”: “1”
@interface ...
{
NSArray *users;
NSMutableDictionary *cellNum;
}
@implementation ...
{
users = @[@"Ali",@"Armita",@"Babak"];
for (int i=0;i<users.count;i++)
{
if ( [users[i] hasPrefix:@"A"] )
{
cellNum[@"a"] = @([cellNum[@"a"] intValue]+1);
}
else
{
cellNum[@"b"] = @([cellNum[@"b"] intValue]+1);
}
}
}
答案 0 :(得分:2)
尝试使用此代码:
NSMutableDictionary *cellNum = [NSMutableDictionary dictionary];
[cellNum setObject:@(0) forKey:@"a"];
[cellNum setObject:@(0) forKey:@"b"];
NSArray* users = @[@"Ali",@"Armita",@"Babak"];
for (int i=0;i<users.count;i++)
{
if ( [users[i] hasPrefix:@"A"] ) {
cellNum[@"a"] = @([cellNum[@"a"] intValue]+1);
}
else {
cellNum[@"b"] = @([cellNum[@"b"] intValue]+1);
}
}
这肯定会给你想要的结果:{a = 2; b = 1; }