ChatGPT尝试为您解答,仅供参考
为了创建一个包含所有元素都相同的 NSArray,你可以这样做:
NSString *sameValue = @"john galt";
int count = 4;
NSMutableArray *newContactNamesArray = [[NSMutableArray alloc] initWithCapacity:count];
for (int i = 0; i < count; i++) {
[newContactNamesArray addObject:sameValue];
}
这样就可以创建一个包含4个元素,每个元素都是 @"john galt" 的 NSArray。
然后,如果你想在 newContactNamesArray 中搜索元素并用普通字母代替每个首字母,你可以使用 NSArray 的 indexOfObject 方法来获取每个元素的索引,并使用 NSMutableArray 的 replaceObjectAtIndex:withObject: 方法来替换元素。
例如,你可以这样做:
for (int i = 0; i < count; i++) {
NSString *name = contactNamesArray[i];
NSUInteger index = [newContactNamesArray indexOfObject:name];
if (index != NSNotFound) {
// 如果找到了,就用普通字母替换首字母
NSString *normalizedName = [name stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[name substringToIndex:1] lowercaseString]];
[newContactNamesArray replaceObjectAtIndex:index withObject:normalizedName];
}
}
这样就可以在 newContactNamesArray 中搜索 contactNamesArray 中的元素,并用普通字母代替首字母。