【发布时间】: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函数是不是有问题?
感谢 StackOverflow 社区阅读我的问题!
【问题讨论】:
-
这可能是问题中的拼写错误,但我认为您缺少
initializePlayer函数上的右括号。 -
您发布之前确保每个 { 都以 } 结束
-
这是因为
updateAfterFirstLogin在initializePlayer内部。正确格式化您的代码,您将看到它。updateAfterFirstLogin函数在initializePlayer之外无法访问。 -
@hardikparmar 为什么不将其添加为答案
-
@KevinPeterson 当然。