0

我正在尝试在类文件的顶部设置一个变量,以便我可以在几行之后使用它。我遇到的问题是Instance member 'streamWidth' cannot be used on type 'BroadcastViewController'。所以我认为这是因为我不能在主类中引用这个变量,我必须在函数中使用它。现在如果是这种情况,有没有办法解决这个问题?

 class BroadcastViewController: UIViewController, VCSessionDelegate {
      @IBOutlet weak var previewView: UIView!
      @IBOutlet weak var btnConnect: UIButton!

      var streamWidth:Int = Int(5)

      var session:VCSimpleSession = VCSimpleSession(videoSize: CGSize(width: streamWidth, height: 720), frameRate: 30, bitrate: 1000000, useInterfaceOrientation: false)

      override func viewDidLoad() {
           super.viewDidLoad()
           previewView.addSubview(session.previewView)
           session.previewView.frame = previewView.bounds
           session.delegate = self

      }

      deinit {
           btnConnect = nil
           previewView = nil
           session.delegate = nil;
      }

      @IBAction func btnConnectTouch(sender: AnyObject) {
           switch session.rtmpSessionState {
           case .None, .PreviewStarted, .Ended, .Error:
                session.startRtmpSessionWithURL("rtmp://east-us.livewx.tv/live", andStreamKey: "fb4843e86976e37" + "?key=" + "e5615d9b171384a0613f")
           default:
                session.endRtmpSession()
           break
       }
       func connectionStatusChanged(sessionState: VCSessionState) {
            switch session.rtmpSessionState {
            case .Starting:
                 btnConnect.setTitle("Connecting", forState: .Normal)
            case .Started:
                 btnConnect.setTitle("Disconnect", forState: .Normal)
            default:
                 btnConnect.setTitle("Connect", forState: .Normal)
            }
      }

      // more functions left out for length reasoning

}

因此,其中一些函数,如deinitbtnConnectTouch和 ,connectionStatusChanged都使用顶部定义的 var 会话。

我要问的是:我将如何在var session:VCSimpleSession?

非常感谢!

4

1 回答 1

2

初始化其他实例变量时不能使用实例变量。

如果我是你,我会更改session隐式展开的可选项并稍后对其进行初始化:

  var session: VCSimpleSession!

  override func viewDidLoad() {
       super.viewDidLoad()

       session = VCSimpleSession(videoSize: CGSize(width: streamWidth, height: 720), frameRate: 30, bitrate: 1000000, useInterfaceOrientation: false)
       previewView.addSubview(session.previewView)
       session.previewView.frame = previewView.bounds
       session.delegate = self
  }
于 2015-10-18T23:09:52.963 回答