Swift Instance会员问题

时间:2015-10-18 22:58:45

标签: ios swift videocore

我试图在类文件的顶部设置一个变量,这样我就可以使用几行了。我遇到的问题是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

}

因此,deinitbtnConnectTouchconnectionStatusChanged等部分功能都使用顶部定义的var会话。

我在问:我如何在var session:VCSimpleSession内使用变量?

非常感谢!

1 个答案:

答案 0 :(得分:2)

初始化其他实例变量时,您无法使用实例变量。

如果我是你,我会将session更改为implicitly unwrapped optional并稍后对其进行初始化:

  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
  }