YouTube API上的EXC_BAD_INSTRUCTION

时间:2018-08-26 10:11:16

标签: ios swift xcode

从YouTube的api获取播放列表视频,如下所示:

这是视频课:

https://app.quicktype.io?share=aNLIT2lKl4ecggFJMRhY

解析如下:

 let youtubeData = try JSONDecoder().decode(Video.self, from: data!)
            for item in youtubeData.items{
                print(item)
                self.titles.append(item.snippet.title)
                self.ids.append(item.contentDetails.videoID)
                self.images.append((item.snippet.thumbnails.standard?.url)!)
            }

,链接为:

            let link: String = "https://www.googleapis.com/youtube/v3/playlistItems?key=AIzaSyCZD5QOrfmfKKeu2FbiTXzhEEr96Ky6tew&playlistId=PLbJRbIvdAVXRdkBSpA25hugmJy_rOKOYu&part=snippet,contentDetails&maxResults=20"

这是邮递员中得到的:

    {
"kind": "youtube#playlistItemListResponse",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/rADsLSpprHJE-fPnym42pRX8eww\"",
"pageInfo": {
    "totalResults": 4,
    "resultsPerPage": 50
},
"items": [
    {
        "kind": "youtube#playlistItem",
        "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/6yrUGyNtxoJ1dmT3Y8sRBJLBeXE\"",
        "id": "UExiSlJiSXZkQVZYUmRrQlNwQTI1aHVnbUp5X3JPS09ZdS41NkI0NEY2RDEwNTU3Q0M2",
        "snippet": {
            "publishedAt": "2018-08-17T02:56:10.000Z",
            "channelId": "UCqXAWrjSgLb0EPa_icPt-pw",
            "title": "Interview",
            "description": "",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/CjNr5Gpa4TE/default.jpg",
                    "width": 120,
                    "height": 90
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/CjNr5Gpa4TE/mqdefault.jpg",
                    "width": 320,
                    "height": 180
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/CjNr5Gpa4TE/hqdefault.jpg",
                    "width": 480,
                    "height": 360
                },
                "standard": {
                    "url": "https://i.ytimg.com/vi/CjNr5Gpa4TE/sddefault.jpg",
                    "width": 640,
                    "height": 480
                },
                "maxres": {
                    "url": "https://i.ytimg.com/vi/CjNr5Gpa4TE/maxresdefault.jpg",
                    "width": 1280,
                    "height": 720
                }
            },
            "channelTitle": "الدوري السعودي للمحترفين Saudi Pro League",
            "playlistId": "PLbJRbIvdAVXRdkBSpA25hugmJy_rOKOYu",
            "position": 0,
            "resourceId": {
                "kind": "youtube#video",
                "videoId": "CjNr5Gpa4TE"
            }
        },
        "contentDetails": {
            "videoId": "CjNr5Gpa4TE",
            "videoPublishedAt": "2018-07-28T19:04:42.000Z"
        }
    },
    {
        "kind": "youtube#playlistItem",
        "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/9bldgOrkQOmEy3P6x_T4pJiqAss\"",
        "id": "UExiSlJiSXZkQVZYUmRrQlNwQTI1aHVnbUp5X3JPS09ZdS4yODlGNEE0NkRGMEEzMEQy",
        "snippet": {
            "publishedAt": "2018-08-17T02:59:16.000Z",
            "channelId": "UCqXAWrjSgLb0EPa_icPt-pw",
            "title": "ورشة عمل مراقبي ومنسقي المباريات - اليوم الختامي",
            "description": "",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/t-sLCUA68R0/default.jpg",
                    "width": 120,
                    "height": 90
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/t-sLCUA68R0/mqdefault.jpg",
                    "width": 320,
                    "height": 180
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/t-sLCUA68R0/hqdefault.jpg",
                    "width": 480,
                    "height": 360
                }
            },
            "channelTitle": "الدوري السعودي للمحترفين Saudi Pro League",
            "playlistId": "PLbJRbIvdAVXRdkBSpA25hugmJy_rOKOYu",
            "position": 1,
            "resourceId": {
                "kind": "youtube#video",
                "videoId": "t-sLCUA68R0"
            }
        },
        "contentDetails": {
            "videoId": "t-sLCUA68R0",
            "videoPublishedAt": "2018-07-28T17:01:42.000Z"
        }
    },
    {
        "kind": "youtube#playlistItem",
        "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/Eue448TE2XiiTrzi8vuJICCFqwU\"",
        "id": "UExiSlJiSXZkQVZYUmRrQlNwQTI1aHVnbUp5X3JPS09ZdS4wMTcyMDhGQUE4NTIzM0Y5",
        "snippet": {
            "publishedAt": "2018-08-17T02:59:24.000Z",
            "channelId": "UCqXAWrjSgLb0EPa_icPt-pw",
            "title": "ورشة عمل مراقبي ومنسقي المباريات - اليوم الأول",
            "description": "ملخص اليوم الأول من ورشة عمل مراقبي ومنسقي المباريات والمنسقين الأمنيين والإعلاميين والتي تقيمها الرابطة بالتعاون مع الاتحاد السعودي والاتحاد الآسيوي لكرة القدم",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/tnUz7fLwEBM/default.jpg",
                    "width": 120,
                    "height": 90
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/tnUz7fLwEBM/mqdefault.jpg",
                    "width": 320,
                    "height": 180
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/tnUz7fLwEBM/hqdefault.jpg",
                    "width": 480,
                    "height": 360
                },
                "standard": {
                    "url": "https://i.ytimg.com/vi/tnUz7fLwEBM/sddefault.jpg",
                    "width": 640,
                    "height": 480
                },
                "maxres": {
                    "url": "https://i.ytimg.com/vi/tnUz7fLwEBM/maxresdefault.jpg",
                    "width": 1280,
                    "height": 720
                }
            },
            "channelTitle": "الدوري السعودي للمحترفين Saudi Pro League",
            "playlistId": "PLbJRbIvdAVXRdkBSpA25hugmJy_rOKOYu",
            "position": 2,
            "resourceId": {
                "kind": "youtube#video",
                "videoId": "tnUz7fLwEBM"
            }
        },
        "contentDetails": {
            "videoId": "tnUz7fLwEBM",
            "videoPublishedAt": "2018-07-27T17:34:19.000Z"
        }
    },
    {
        "kind": "youtube#playlistItem",
        "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/AjA_MynNqIpjpuSN8kJmW7zgDlk\"",
        "id": "UExiSlJiSXZkQVZYUmRrQlNwQTI1aHVnbUp5X3JPS09ZdS41MjE1MkI0OTQ2QzJGNzNG",
        "snippet": {
            "publishedAt": "2018-08-17T02:59:33.000Z",
            "channelId": "UCqXAWrjSgLb0EPa_icPt-pw",
            "title": "حديث سلمان النمشان عن ورشة عمل مهام مراقبي ومنسقي المباريات والمنسقين الأمنيين والإعلاميين",
            "description": "سلمان بن نمشان رئيس لجنة مراقبي ومنسقي المباريات في رابطة الدوري السعودي للمحترفين يتحدث عن أهمية ورشة عمل مراقبي ومنسقي المباريات والمنسقين الأمنيين والإعلاميين والتي تقيمها الرابطة بالتعاون مع الاتحاد السعودي والاتحاد الآسيوي لكرة القدم",
            "thumbnails": {
                "default": {
                    "url": "https://i.ytimg.com/vi/wIIGgdEa9DU/default.jpg",
                    "width": 120,
                    "height": 90
                },
                "medium": {
                    "url": "https://i.ytimg.com/vi/wIIGgdEa9DU/mqdefault.jpg",
                    "width": 320,
                    "height": 180
                },
                "high": {
                    "url": "https://i.ytimg.com/vi/wIIGgdEa9DU/hqdefault.jpg",
                    "width": 480,
                    "height": 360
                },
                "standard": {
                    "url": "https://i.ytimg.com/vi/wIIGgdEa9DU/sddefault.jpg",
                    "width": 640,
                    "height": 480
                },
                "maxres": {
                    "url": "https://i.ytimg.com/vi/wIIGgdEa9DU/maxresdefault.jpg",
                    "width": 1280,
                    "height": 720
                }
            },
            "channelTitle": "الدوري السعودي للمحترفين Saudi Pro League",
            "playlistId": "PLbJRbIvdAVXRdkBSpA25hugmJy_rOKOYu",
            "position": 3,
            "resourceId": {
                "kind": "youtube#video",
                "videoId": "wIIGgdEa9DU"
            }
        },
        "contentDetails": {
            "videoId": "wIIGgdEa9DU",
            "videoPublishedAt": "2018-07-27T17:24:59.000Z"
        }
    }
]
}

但出现此错误:

  

线程2:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

在此行:

enter image description here

当播放列表ID为:

  

PLSmSEc8Z6liq82OteppRm-koytegaPX2C

..它工作正常!

出什么问题了?以及如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在您的响应项目ID中

"kind": "youtube#playlistItem",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/9bldgOrkQOmEy3P6x_T4pJiqAss\""

没有缩略图> standrad-> URL路径值(没有 standrad 键)