gpt4 book ai didi

ios - 当数据为 NULL 时,访问链表的数据元素时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 05:44:26 25 4
gpt4 key购买 nike

我正在使用以下代码遍历 objective-c 中的链表

   const MSList *calls = linphone_core_get_calls(LC);
if (calls == NULL)
{
[self dismissCtrl];
//how to check current is which screen is on
// while ((currentView == CallView.compositeViewDescription) ||
// (currentView == CallIncomingView.compositeViewDescription) ||
// (currentView == CallOutgoingView.compositeViewDescription)) {
// [self popCurrentView];
// }

} else {
linphone_call_resume((LinphoneCall *)calls->data);
while (calls)
{

if(calls->data != NULL && calls->data != nil && calls->data != (__bridge void *)((id)[NSNull null]))
{
//crash
if (linphone_call_get_state((LinphoneCall *)calls->data) == LinphoneCallIncomingReceived ||
linphone_call_get_state((LinphoneCall *)calls->data) == LinphoneCallIncomingEarlyMedia) {
[self displayIncomingCall:(LinphoneCall *)calls->data];
break;
}
}
calls = calls->next;



}

当整个列表不为空但其数据、前一个或下一个值为 NULL 时,应用程序崩溃。我添加了代码来检查数据是否为 ​​NULL,但如果它(数据)为 NULL,那么我将无法访问它,并且在这种情况下应用程序本身就会崩溃。如何防止这种情况发生?我已附上应用程序崩溃位置以及列表当时包含的值的屏幕截图。 enter image description here

最佳答案

if (calls == NULL) {
[self dismissCtrl];
//how to check current is which screen is on
// while ((currentView == CallView.compositeViewDescription) ||
// (currentView == CallIncomingView.compositeViewDescription) ||
// (currentView == CallOutgoingView.compositeViewDescription)) {
// [self popCurrentView];
// }

}
else
{

size_t count = bctbx_list_size(calls);

linphone_call_resume((LinphoneCall *)calls->data);


int i = 0;

while (calls )
{


if(i < count)
{



if ( calls->data == (__bridge void *)((id)[NSNull null]) || calls->data == NULL || calls->data == nil )
{

return;
}


LinphoneCall *objCall = (LinphoneCall *)calls->data;



LinphoneCallState state = (objCall != NULL) ? linphone_call_get_state(call) : 0;



if (state == LinphoneCallIncomingReceived ||
state == LinphoneCallIncomingEarlyMedia)
{
[self displayIncomingCall:(LinphoneCall *)calls->data];
break;
}

//calls ? calls->data : NULL
// calls = calls ? calls->next : NULL;
calls = calls->next;
i = i+1;
}
else
{
break;
}
}

关于ios - 当数据为 NULL 时,访问链表的数据元素时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56274086/

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