NsMutable阵列不添加对象iOS
问题描述:
我正在从用户手机访问联系人。一切都很完美,但是当我将它添加到NSMutableArray
中时,则不会输入任何内容。在我点击阵列时显示0X16bed870的断点。我的代码如下。NsMutable阵列不添加对象iOS
-(void)mycontact
{
NSMutableArray * contactNumbersArray = [[NSMutableArray alloc] init];
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted)
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"This app previously was refused permissions to contacts; Please go to settings and grant permission to this app so it can use contacts" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:TRUE completion:nil];
return;
}
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES) {
//keys with fetching properties
NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
NSString *containerId = store.defaultContainerIdentifier;
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
NSError *error;
NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error) {
NSLog(@"error fetching contacts %@", error);
} else {
NSString *phone;
NSString *fullName;
NSString *firstName;
NSString *lastName;
UIImage *profileImage;
for (CNContact *contact in cnContacts) {
// copy data to my custom Contacts class.
firstName = contact.givenName;
lastName = contact.familyName;
if (lastName == nil) {
fullName=[NSString stringWithFormat:@"%@",firstName];
} else if (firstName == nil) {
fullName=[NSString stringWithFormat:@"%@",lastName];
} else {
fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
}
UIImage *image = [UIImage imageWithData:contact.imageData];
if (image != nil) {
profileImage = image;
}else{
profileImage = [UIImage imageNamed:@"person-icon.png"];
}
for (CNLabeledValue *label in contact.phoneNumbers) {
phone = [label.value stringValue];
if ([phone length] > 0) {
[contactNumbersArray addObject:phone];
}
}
NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
}
dispatch_async(dispatch_get_main_queue(), ^{
for(int j=0;j<[contactNumbersArray count];j++)
{
NSString *newString = [[contactNumbersArray[j] componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:@""];
contactNumbersArray[j]=newString;
}
});
}
}
}];
}
答
您是通过同一阵列contactNumbersArray循环中,你 要更新的值。
+0
他不需要再添加它,他正在编辑它 – ddb
+0
@ddb执行。我想删除 - ,(,)符号,所以我正在编辑它。 –
'NSMutableArray'不会是'nil'会吗(常见错误)?你指的是哪一个数组?此外,为什么你使用'dispatch_async(dispatch_get_main_queue()'?你似乎没有访问UI ... – Droppy
对我来说,这是不清楚的问题:你在谈论哪个数组?contactNumbersArray?你在哪里放置断点来检查其内容 – ddb
@ddb 是的,我在谈论contactNumbersArray我已经把破发点上 电话= [label.value stringValue的];。手机 其示值但不执行后增加的contactNumbersArray阵列 –