//获取共享地址簿的实例 ABAddressBookRef addressBook = ABAddressBookCreate();为什么我的nsmutablearray属性不保留对象?
//get all contacts in addressbook
CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numPeople = ABAddressBookGetPersonCount(addressBook);
self.contacts = [NSMutableArray array];
//modify data to be stored in an array
for (int i = 0; i < numPeople; i++) {
AddressBookPerson *person = [[AddressBookPerson alloc] init];
ABRecordRef ref = CFArrayGetValueAtIndex(allContacts, i);
NSString *firstName = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSString *lastName = (NSString *)ABRecordCopyValue(ref, kABPersonLastNameProperty);
ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(ref, kABPersonPhoneProperty);
NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
CFRelease(phoneNumberProperty);
NSString *contactFirstLast = [NSString stringWithFormat: @"%@, %@", firstName, lastName];
person.name = contactFirstLast;
person.phoneNumbers = phoneNumbers;
[self.contacts addObject:person];
//self.contacts = [NSArray arrayWithArray:listOfContacts];
[contactFirstLast release];
[person release];
}
我不知道为什么这个属性不保留。我甚至尝试过尝试变化,如:
self.contacts = [[NSMutableArray alloc] init];
它被分配,但我放置在里面的任何对象都不会被保留。我使用了一个不同的数组,它不是属性,对象保留得很好。但问题是我需要一个全局属性来包含值。
UPDATE:
我找到了解决问题的办法。我跑分析,并做了参考计数,我知道它没有内存泄漏。但问题仍然存在,这是因为财产的名称。
“contacts”是“可用于启动连接和管理传入连接请求的一组MIDI网络主机”的内置属性。
我给自己的房产命名了一个独特的名字,所有的问题都消失了。没有必要使用静态变量,我明白,我正在使用该变量进行测试,以查看我的对象发生了什么。静态变量的工作原因是因为它是唯一命名的。
可以回答你自己的问题,但请不要将它作为对别人现有答案的编辑。 –
我的道歉,请记住下次。 – Toret