Swift 声称我引用的类中没有方法
Posted
技术标签:
【中文标题】Swift 声称我引用的类中没有方法【英文标题】:Swift is claiming there is no method in the class I am referencing 【发布时间】:2018-09-21 17:21:21 【问题描述】:代码如下:
import UIKit
import SafariServices
import AVFoundation
import AWSAuthCore
class ViewController: UIViewController, SPTAudiostreamingPlaybackDelegate, SPTAudioStreamingDelegate
// Variables
var auth = SPTAuth.defaultInstance()!
var session:SPTSession!
// Initialzed in either updateAfterFirstLogin: (if first time login) or in viewDidLoad (when there is a check for a session object in User Defaults
var player: SPTAudioStreamingController?
var loginUrl: URL?
// Outlets
@IBOutlet weak var loginSpotify: UIButton!
@IBOutlet weak var testLabel: UILabel!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setup()
NotificationCenter.default.addObserver(self, selector: #selector(self.updateAfterFirstLogin()))
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func setup ()
// insert redirect your url and client ID below
let redirectURL = "splitter-app://callback" // put your redirect URL here
let clientID = "client ID goes here" // put your client ID here
auth.redirectURL = URL(string: redirectURL)
auth.clientID = "clientID goes here"
auth.requestedScopes = [SPTAuthStreamingScope, SPTAuthPlaylistReadPrivateScope, SPTAuthPlaylistModifyPublicScope, SPTAuthPlaylistModifyPrivateScope]
loginUrl = auth.spotifyWebAuthenticationURL()
print("test")
func initializePlayer(authSession:SPTSession)
if self.player == nil
self.player = SPTAudioStreamingController.sharedInstance()
self.player!.playbackDelegate = self
self.player!.delegate = self
try! player!.start(withClientId: auth.clientID)
self.player!.login(withAccessToken: authSession.accessToken)
func updateAfterFirstLogin ()
loginSpotify.isHidden = true
let userDefaults = UserDefaults.standard
if let sessionObj:AnyObject = userDefaults.object(forKey: "SpotifySession") as AnyObject?
let sessionDataObj = sessionObj as! Data
let firstTimeSession = NSKeyedUnarchiver.unarchiveObject(with: sessionDataObj) as! SPTSession
self.session = firstTimeSession
initializePlayer(authSession: session)
func initializaPlayer(authSession:SPTSession)
if self.player == nil
self.player = SPTAudioStreamingController.sharedInstance()
self.player!.playbackDelegate = self
self.player!.delegate = self
try! player?.start(withClientId: auth.clientID)
self.player!.login(withAccessToken: authSession.accessToken)
func audioStreamingDidLogin(_ audioStreaming: SPTAudioStreamingController!)
// after a user authenticates a session, the SPTAudioStreamingController is then initialized and this method called
print("logged in")
self.player?.playSpotifyURI("spotify:track:58s6EuEYJdlb0kO7awm3Vp", startingWith: 0, startingWithPosition: 0, callback: (error) in
if (error != nil)
print("playing!")
)
错误在第 31 行。Xcode 说:“ViewController”类型的值没有成员“updateAfterFirstLogin”,但是 updateAfterFirstLogin 在类中并且应该能够被引用。为什么我不能引用它? NotificationCenter类不能带void函数是不是有问题?
感谢 *** 社区阅读我的问题!
【问题讨论】:
这可能是问题中的拼写错误,但我认为您缺少initializePlayer
函数上的右括号。
您发布之前确保每个 都以 结束
这是因为updateAfterFirstLogin
在initializePlayer
内部。正确格式化您的代码,您将看到它。 updateAfterFirstLogin
函数在 initializePlayer
之外无法访问。
@hardikparmar 为什么不将其添加为答案
@KevinPeterson 当然。
【参考方案1】:
这是因为 updateAfterFirstLogin
在内部 initializePlayer
。正确格式化您的代码,您将看到它。 updateAfterFirstLogin
函数在 initializePlayer
之外无法访问。
还可以将它与selector
一起使用,将@objc
放入函数声明中,如下所示:@objc func updateAfterFirstLogin()
【讨论】:
【参考方案2】:我认为您缺少一些括号:
func initializePlayer(authSession:SPTSession)
if self.player == nil
self.player = SPTAudioStreamingController.sharedInstance()
self.player!.playbackDelegate = self
self.player!.delegate = self
try! player!.start(withClientId: auth.clientID)
self.player!.login(withAccessToken: authSession.accessToken)
func updateAfterFirstLogin ()
loginSpotify.isHidden = true
let userDefaults = UserDefaults.standard
if let sessionObj:AnyObject = userDefaults.object(forKey: "SpotifySession") as AnyObject?
let sessionDataObj = sessionObj as! Data
let firstTimeSession = NSKeyedUnarchiver.unarchiveObject(with: sessionDataObj) as! SPTSession
self.session = firstTimeSession
initializePlayer(authSession: session)
在func updateAfterFirstLogin()前加一个
【讨论】:
【参考方案3】:选择器参数接收选择器对象。这个 Object 是基于 obtective-C 的对象,所以你必须在 func 之前添加 @objc 标识符。
将您的代码(在正确的行中)更正为:
@objc func updateAfterFirstLogin ()
【讨论】:
【参考方案4】:这是因为括号放错了。 updateAfterFirstLogin()
方法在 initializePlayer(authSession:SPTSession)
方法内。您需要在 updateAfterFirstLogin()
中的 if 语句之后使用 ,如下所示:
func initializePlayer(authSession:SPTSession)
if self.player == nil
self.player = SPTAudioStreamingController.sharedInstance()
self.player!.playbackDelegate = self
self.player!.delegate = self
try! player!.start(withClientId: auth.clientID)
self.player!.login(withAccessToken: authSession.accessToken)
func initializePlayer(authSession:SPTSession)
之后,您必须从代码中删除多余的右括号。
【讨论】:
以上是关于Swift 声称我引用的类中没有方法的主要内容,如果未能解决你的问题,请参考以下文章