swift 我如何使我的函数成为公共选择器?

wn9m85ua  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(101)

我已经排除了UIViewControllerRepresentable的其他协议存根以缩短此代码,但此代码仅在尝试上传到Apple Connect时才会出错。
资产验证失败(50)应用引用Payload/some/path中的非公共选择器:volumeChanged:(ID:060de305-b6d2-4314-a238-c679fa85404e)
为什么volumeChanged函数不是公共选择器?

import SwiftUI
import AVFoundation
import MediaPlayer

struct VolumeEventReader<Content: View>: UIViewControllerRepresentable {
    class Coordinator: NSObject {
        var parent: VolumeEventReader
        var lastVolumeNotificationSequenceNumber: Int?
        var currentVolume = AVAudioSession.sharedInstance().outputVolume

        init(_ parent: VolumeEventReader) {
            self.parent = parent
        }

        @objc func volumeChanged(_ notification: NSNotification) {
            DispatchQueue.main.async { [self] in
                volumeControl(notification)
            }
        }

        func manageVolume(volume: Float, minVolume: Float) {
            switch volume {
            case minVolume: do {
                currentVolume = minVolume + 0.0625
            }
            case 1: do {
                currentVolume = 0.9375
            }
            default: break
            }

            parent.updateUIView(volume: volume)
            
            currentVolume = volume
        }

        func volumeControl(_ notification: NSNotification) {
            let minVolume: Float = 0.0625

            if let volume = notification.userInfo!["Volume"] as? Float {
                //avoiding duplicate events if same ID notification was generated
                if let seqN = self.lastVolumeNotificationSequenceNumber {
                    if seqN == notification.userInfo!["SequenceNumber"] as! Int {
                        // Duplicate nofification received
                    }
                    else {
                        self.lastVolumeNotificationSequenceNumber = (notification.userInfo!["SequenceNumber"] as! Int)
                        manageVolume(volume: volume, minVolume: minVolume)
                    }
                }
                else {
                    self.lastVolumeNotificationSequenceNumber = (notification.userInfo!["SequenceNumber"] as! Int)
                    manageVolume(volume: volume, minVolume: minVolume)
                }
            }
        }
    }

    func makeCoordinator() -> Coordinator {
        let coordinator = Coordinator(self)

        NotificationCenter.default.addObserver(
            coordinator,
            selector: #selector(Coordinator.volumeChanged),
            name: NSNotification.Name(rawValue: "SystemVolumeDidChange"),
            object: nil
        )

        return coordinator
    }
}
6ioyuze2

6ioyuze21#

它不会告诉你你的函数是一个私有选择器,而是检测你正在调用一个匹配Apple内部API的选择器。
换句话说,看起来您正在尝试调用私有API。
只需更改函数的名称,使其与Apple的内部函数不匹配。

相关问题