我正在尝试将 GIPHY API 合并到我的项目中。
在我的 AppDelegate 中,我将 API 密钥设置为公共 beta 密钥(只是为了测试它):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SwiftyGiphyAPI.shared.apiKey = "dc6zaTOxFJmzC"
// Override point for customization after application launch.
return true
}
这是来自公共课程SwiftyGiphyAPI
:
public static let publicBetaKey = "dc6zaTOxFJmzC"
/// Access the Giphy API through the shared singleton.
public static let shared: SwiftyGiphyAPI = SwiftyGiphyAPI()
/// Before you can use SwiftyGiphy, you need to set your API key.
public var apiKey: String? {
didSet {
if apiKey == SwiftyGiphyAPI.publicBetaKey
{
print("****************************************************************************************************************************")
print("* *")
print("* IMPORTANT: You seem to be using Giphy's public beta key. Please change this to a production key before shipping. *")
print("* Apply for one here: http://api.giphy.com/submit *")
print("* *")
print("****************************************************************************************************************************")
print("")
}
}
}
当控制器打开时,趋势部分应该显示趋势 GIF,这是该功能的开始:
func getTrending(limit: Int = 25, rating: SwiftyGiphyAPIContentRating = .pg13, offset: Int? = nil, completion: GiphyMultipleGIFResponseBlock?)
{
guard apiKey != nil || !isUsingDefaultAPIBase else {
print("ATTENTION: You need to set your Giphy API key before using SwiftyGiphy.")
completion?(networkError(description: NSLocalizedString("You need to set your Giphy API key before using SwiftyGiphy.", comment: "You need to set your Giphy API key before using SwiftyGiphy.")), nil)
return
}
我收到了打印声明You need to set your Giphy API key before using SwiftyGiphy.
我认为这是因为SwiftyGiphyAPI.shared.apiKey
它仍然是可选的,所以它被读取SwiftyGiphyAPI
为 nil。我尝试了一些可选链接,但也许我做错了。
我确定我缺少一个简单的修复方法。
此外,如果有帮助,这里是 GIPHY 文档的链接:GIPHY Docs
在此先感谢您的帮助!