简体   繁体   English

无法将NSMutableArray类型的值分配给UIBarButtonItem类型

[英]Cannot assign value of type NSMutableArray to type UIBarButtonItem

I am developing an iOS audio app as a learning project. 我正在开发一个iOS音频应用程序作为一个学习项目。 I'm having a problem in one of the functions, which checks if the player is playing or paused/stopped. 我在其中一个功能中遇到问题,该功能可以检查播放器是在播放还是在暂停/停止。 If it's playing it should change the button in the button bar to a "pause" button, or if it's paused or stopped, change the button to a "play" button. 如果正在播放,则应将按钮栏中的按钮更改为“暂停”按钮,或者如果已暂停或停止,则将其更改为“播放”按钮。 I am assigning the button bar items to a variable "items" as a NSMutableArray, then make the necessary change to the button, then assign "items" back to the button bar items to update the display. 我将按钮栏项目作为NSMutableArray分配给变量“ items”,然后对按钮进行必要的更改,然后将“ items”分配回按钮栏项目以更新显示。

In my code I am getting an error on the line: 在我的代码中,我在网上遇到了一个错误:

self.toolbar.items = items

The error message is 错误消息是

Cannot assign value of type NSMutableArray to Type UIBarButtonItem 无法将类型NSMutableArray的值分配给类型UIBarButtonItem

I assume that I need to cast the NSMutableArray, I've tried using Array, and when I do the error goes away but the buttons break. 我假设我需要转换NSMutableArray,我尝试使用Array,但当我这样做时错误消失了,但是按钮坏了。

The related parts of my code are: 我的代码的相关部分是:

@IBOutlet weak var toolbar: UIToolbar!

@IBOutlet var playButton: UIBarButtonItem!


@IBAction func playPausePressed(sender: AnyObject) {
    let playbackState = self.player.playbackState as MPMusicPlaybackState
    let items = NSMutableArray(array: self.toolbar.items!)
    if playbackState == .Stopped || playbackState == .Paused{
        self.player.play()
        items[2] = self.pauseButton
    } else if playbackState == .Playing{
        self.player.pause()
        items[2] = self.playButton
    }
    self.toolbar.items = items
}

Any help or direction would be much appreciated. 任何帮助或指示将不胜感激。 Thanks! 谢谢!

The error occurs because NSMutableArray is not related to Swift Array 发生错误是因为NSMutableArray与Swift Array无关

Why not using the native property directly? 为什么不直接使用本机属性? Due to reference semantics it's not necessary to create a temporary array. 由于引用语义,因此不必创建临时数组。

@IBAction func playPausePressed(sender: AnyObject) {
    let playbackState = self.player.playbackState as MPMusicPlaybackState
    var items = self.toolbar.items!
    if playbackState == .Stopped || playbackState == .Paused{
        self.player.play()
        items[2] = self.pauseButton
    } else if playbackState == .Playing{
        self.player.pause()
        items[2] = self.playButton
    }
}

I have a feeling that you could do better. 我觉得您可以做得更好。 Do you really need to switch between two different buttons for your items[2] ? 您是否真的需要在两个不同的按钮之间进行切换items[2]

UIBarButtonItem can be used pretty much like a regular UIButton with different states. UIBarButtonItem可以像具有不同状态的常规UIButton使用。 I'm assuming you'd want to switch between a play and pause image whenever your player is paused/playing. 我假设您想在播放器暂停/播放时在播放和暂停图像之间切换。 You could do this by setting two different images to your UIBarButtonItem ( setBackgroundImage(_:forState:barMetrics:) ) then by setting the appropriate state when your playbackState changes. 您可以通过在UIBarButtonItem( setBackgroundImage(_:forState:barMetrics:) )中设置两个不同的图像,然后在playbackState更改时设置适当的状态来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型为“ nil”的值分配给类型为“ NSMutableArray”的值 - Cannot assign a value of type 'nil' to a value of type 'NSMutableArray' 无法分配类型为[AnyObject]的值tp类型为NSMutableArray的值 - Cannot Assign a value of type [AnyObject] tp a value of type NSMutableArray 无法分配值类型 - Cannot not assign Value Type 错误:无法将类型“ [SomeObject]”的值分配给类型“ [NSMutableArray]” - Error: Can't assign Value of type '[SomeObject]' to type '[NSMutableArray]' 无法分配通用类型的值 - Cannot assign value of generic type 无法将类型“ NSMutableArray”的值转换为预期的参数类型“ [SKTexture]” - Cannot convert value of type 'NSMutableArray' to expected argument type '[SKTexture]' 无法转换“[UICollectionViewLayoutAttributes]”类型的值? 到指定类型 'NSMutableArray' - Cannot convert value of type '[UICollectionViewLayoutAttributes]?' to specified type 'NSMutableArray' 无法将类型“ AppDelegate”的值分配给类型“ GIDSignInDelegate!” - Cannot assign value of type 'AppDelegate' to type 'GIDSignInDelegate!" 无法分配“字符串”类型的值? 输入“UILabel?” - Cannot assign value of type 'String?' to type 'UILabel?' 无法将Player类型的值分配给SKSpriteNode类型 - Cannot assign value of type Player to type SKSpriteNode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM