Swift报错:所引用的类中不存在对应方法
Hey Jeffrey, let's work through that "referenced class has no such method" error you're seeing in your Swift project with the Spotify SDK. Based on your code snippet, here are the most likely fixes to try:
1. Make Sure You're Importing the Spotify SDK Module
Your code references SPTAuth, SPTAudioStreamingController, and other Spotify-specific classes, but I don't see the required import statement at the top. In Swift, you need to explicitly import the SDK's module to access its classes and methods:
// Add this line to your imports import SpotifyiOS
If you're using CocoaPods to integrate the SDK, double-check your Podfile includes the dependency:
pod 'SpotifyiOS'
Run pod install again, and make sure you're opening your project via the .xcworkspace file (not the .xcodeproj).
2. Verify API Compatibility with Your Spotify SDK Version
Spotify's iOS SDK has changed some method names across versions. For example, the old SPTAuth.defaultInstance() method has been replaced with SPTAuth.shared in newer releases:
- If you're using a recent SDK version, update your auth initialization to:
var auth = SPTAuth.shared
- If you're stuck on an older version, confirm that
defaultInstance()is indeed a valid method for that release (old SDKs did support this, but outdated versions might have compatibility issues with newer Xcode/Swift versions).
3. Clear Xcode's Cache and Rebuild
Xcode's cached build files can sometimes cause false "method not found" errors, especially after adding a new SDK. Try these steps:
- Press
Cmd + Shift + Kto clean the Build Folder - Close Xcode, then delete the
DerivedDatafolder for your project (you can find it at~/Library/Developer/Xcode/DerivedData/[YourProjectName]-[RandomString]) - Reopen your project and rebuild from scratch
4. Check Class Initialization and Optional Safety
If you're calling methods on an uninitialized optional variable (like your player property), Xcode might throw a misleading "method doesn't exist" error instead of pointing out the nil value. Make sure you properly initialize the SPTAudioStreamingController before using it:
// Example initialization for the player do { player = try SPTAudioStreamingController.sharedInstance() player?.playbackDelegate = self player?.delegate = self } catch { print("Failed to initialize player: \(error.localizedDescription)") }
内容的提问来源于stack exchange,提问作者Jeffrey Bloom

