ios - 索引 0 超出空数组的范围' *** 第一次抛出调用堆栈 :
<p><p>我收到如下错误,请帮助我提前谢谢,这是我让空数组的索引 0 超出范围的错误 *** 首先抛出调用堆栈:--->>
以下是我的代码:</p>
<pre><code>-(void)retriveContactsFromAddressBook
{
//CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
if (addressBook!=nil)
{
NSArray *allContacts=(__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i=0;
for ( i = 0; i<; i++)
{
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts;
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName=(__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
if (i==0) {
self.FirstName=;
if (lastName==nil) {
self.LastName=[init];
} else {
self.LastName=;
}
} else {
;
if (lastName==nil) {
;
} else {
;
}
}
NSString *full=,];
if (i==0) {
self.fullName=;
} else {
;
}
}
}
else
{
NSLog(@"Error Address book empty");
}
}
</code></pre>
<p>我收到如下错误---->>></p>
<pre><code>>Terminating app due to uncaught exception 'NSRangeException', reason: '*** -: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x2ce265f7 0x3a560c77 0x2cd3a157 0x126419 0x125edb 0x125cb7 0x302ce46d 0x30378325 0x3037824d 0x303777d1 0x3037750b 0x30377271 0x30377209 0x302cb97f 0x2fcf6f65 0x2fcf2951 0x2fcf27d9 0x2fcf21c7 0x2fcf1fd1 0x2fcebedd 0x2cded015 0x2cdea6f9 0x2cdeaafb 0x2cd37b31 0x2cd37943 0x340f0051 0x3032d6f1 0x1e1ea9 0x3aafcaaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code>-(void)retriveContactsFromAddressBook
{
//CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
self.LastName=[init];
self.FirstName=[init];
self.fullName=;
if (addressBook!=nil)
{
NSArray *allContacts=(__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i=0;
for ( i = 0; i<; i++)
{
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts;
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName=(__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
if (firstName == nil) {
;
} else {
;
}
if (lastName==nil) {
;
} else {
;
}
NSString *full=,];
;
}
}
else
{
NSLog(@"Error Address book empty");
}
}
</code></pre>
<p>这样使用..</p>
<pre><code>self.LastName=[init];
</code></pre>
<p>这一行是问题.. 如果 lastname 为 nil 你没有插入任何对象</p>
<pre><code>NSString *full=,];
</code></pre>
<p>但是在这一行 <code></code> 您尝试从有问题的空数组中检索对象...</p></p>
<p style="font-size: 20px;">关于ios - 索引 0 超出空数组的范围' *** 第一次抛出调用堆栈 :,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28044625/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28044625/
</a>
</p>
页:
[1]