Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 4向数组中追加数据时出错_Swift_Xcode - Fatal编程技术网

Swift 4向数组中追加数据时出错

Swift 4向数组中追加数据时出错,swift,xcode,Swift,Xcode,在我的应用程序中,我有一组数组,这些数组附加了来自API的数据。方法如下: let noOfCountries = APIData.data.count while self.countries < noOfCountries{ self.idArray.append(APIData.data[self.countries].id!) self.countryIntroArray.append(APIDa

在我的应用程序中,我有一组数组,这些数组附加了来自API的数据。方法如下:

   let noOfCountries = APIData.data.count
            while self.countries < noOfCountries{
                self.idArray.append(APIData.data[self.countries].id!)
            self.countryIntroArray.append(APIData.data[self.countries].countryIntroTab!)
            self.countryNameArray.append(APIData.data[self.countries].countryName!)

            self.flagArray.append(APIData.data[self.countries].countryFlag!)

                self.countryEventsArray.append(APIData.data[self.countries].countryEventsTab!)// App crashes here with this error: Unexpectedly found nil while unwrapping an Optional value

                self.countries += 1
            }
结构设置如下:

let id: Int?
let countryName: String?
let countryFlag: String?
let countryIntroTab: String?
let countryEventsTab: String?

我做错了什么?如果我从self.countryEventsArray中删除感叹号,应用程序将根本无法运行。

如果没有任何数据存在,则不应将api数据追加到数组中 使用相同的代码进行检查

if let myData = APIData.data[self.countries].countryEventsTab {
   self.countryEventsArray.append(myData)
}

检查countryEventsTab是否为
nil
if let myData = APIData.data[self.countries].countryEventsTab {
   self.countryEventsArray.append(myData)
}