gpt4 book ai didi

iphone - Xcode,只检索带有字母 A 的地址簿姓氏(依此类推)

转载 作者:行者123 更新时间:2023-11-29 13:37:23 26 4
gpt4 key购买 nike

我在检索地址簿的姓氏时遇到问题。我只想按字母表中的每个字母检索姓氏。

这是我到目前为止的代码

ABAddressBookRef addressBook = ABAddressBookCreate();
totalPeople = (__bridge_transfer NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

NSString *aString = @"A";

for(int i =0;i<[totalPeople count];i++){
ABRecordRef thisPerson = (__bridge ABRecordRef)
[totalPeople objectAtIndex:i];
lastName = (__bridge_transfer NSString *) ABRecordCopyValue(thisPerson, kABPersonLastNameProperty);
}

我不知道之后该怎么办,谢谢你看这个。

现在是这样

 ABAddressBookRef addressBook = ABAddressBookCreate();
totalPeople = (__bridge_transfer NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

NSString *aString = @"A";

for(int i =0;i<[totalPeople count];i++){
ABRecordRef thisPerson = (__bridge ABRecordRef)
[totalPeople objectAtIndex:i];
lastName = (__bridge_transfer NSString *) ABRecordCopyValue(thisPerson, kABPersonLastNameProperty);

NSString *firstLetterOfCopiedName = [lastName substringWithRange: NSMakeRange(0,1)];
if ([firstLetterOfCopiedName compare: aString options: NSCaseInsensitiveSearch] == NSOrderedSame) {
//This person's last name matches the string aString
aArray = [[NSArray alloc]initWithObjects:lastName, nil];
}

}

它只会将一个名称添加到数组中,我应该怎么做才能将其全部添加。抱歉,我是 ios 开发的新手!

最佳答案

您可以使用类似这样的方法并将结果存储在数组中或返回结果。 (未测试)

NSString *firstLetterOfCopiedName = [lastName substringWithRange: NSMakeRange(0,1)];
if ([firstLetterOfCopiedName compare: aString options: NSCaseInsensitiveSearch] == NSOrderedSame) {
//This person's last name matches the string aString
}

你需要在循环外分配数组(否则它只会包含一个对象),数组也必须是一个 NSMutableArray(所以它可以被修改)。这是一个例子:

ABAddressBookRef addressBook = ABAddressBookCreate();
totalPeople = (__bridge_transfer NSMutableArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);

NSString *aString = @"A";

//This is the resulting array
NSMutableArray *resultArray = [[NSMutableArray alloc] init];

for(int i =0;i<[totalPeople count];i++){
ABRecordRef thisPerson = (__bridge ABRecordRef)
[totalPeople objectAtIndex:i];
lastName = (__bridge_transfer NSString *) ABRecordCopyValue(thisPerson, kABPersonLastNameProperty);

NSString *firstLetterOfCopiedName = [lastName substringWithRange: NSMakeRange(0,1)];
if ([firstLetterOfCopiedName compare: aString options: NSCaseInsensitiveSearch] == NSOrderedSame) {
//This person's last name matches the string aString
[resultArray addObject: lastName];
}

}

//print contents of array
for(NSString *lastName in resultArray) {
NSLog(@"Last Name: %@", lastName);
}

关于iphone - Xcode,只检索带有字母 A 的地址簿姓氏(依此类推),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10220898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com