我正在改变NSArray的结果:我将没有国家/地区代码前缀的电话号码替换为具有国家/地区代码前缀的电话号码。然而,在某些情况下,可能存在一个人有两个电话记录的情况。我需要将这两个结合在一起,在我添加了前缀之后,我需要将它放入一个数组中,这样我就得到了一个类似于@[ @[@"0634343", @"4390493"], @[@"490209"]];
等的数组。
目前我只是创建一个如下所示的数组:
@[@"0634343", @"4390493", @"490209"];
这是我正在使用的代码:
self.prefixSearchNumbers = [NSMutableArray array];
for (NSArray *phoneNumbers in _phoneSearchResult) {
NSMutableArray *formattedPhoneNumbers = [NSMutableArray array];
for (NSString *phoneNumber in phoneNumbers) {
NSString *firstChar = [phoneNumber substringToIndex:1];
if (([firstChar isEqualToString:@"0"] && prefix != nil) || (firstChar == 0 && prefix!=nil)) {
NSString *phoneNumber = [phoneNumber stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:prefix]; // trows warning: varibale 'phoneNumber' is uninitialized when used within its own initialization
NSLog(@"phoneNumber: %@", phoneNumber); //returns phoneNumber withous prefix in Array;
}
[formattedPhoneNumbers addObject:phoneNumber];
NSLog(@"formattedPhoneNumbers: %@", formattedPhoneNumbers);
}
[self.prefixSearchNumbers addObject:formattedPhoneNumbers];
name = [self.modifiedNameArray objectAtIndex:indexPath.row];
}
如何使用数组创建数组?
答案 0 :(得分:0)
您是否尝试按索引克隆对结果进行分组?
self.prefixSearchNumbers = [NSMutableArray array];
for (NSArray *phoneNumbers in _phoneSearchResult) {
NSMutableArray *formattedPhoneNumbers = [NSMutableArray array];
for (NSString *phoneNumber in phoneNumbers) {
NSString *firstChar = [phoneNumber substringToIndex:1];
if (([firstChar isEqualToString:@"0"] && prefix != nil) || (firstChar == 0 && prefix!=nil)) {
phoneNumber = [phoneNumber stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:prefix];
}
[formattedPhoneNumbers addObject:phoneNumber];
}
[self.prefixSearchNumbers addObject:formattedPhoneNumbers];
}