2015-09-27 82 views
0

我已經更新了我的應用程序到Xcode 7並轉換爲swift2我已經清除了所有錯誤,但是現在在運行應用程序時它剛剛看到啓動屏幕後崩潰。從下面這行代碼是綠色(:斷點1.4線程1):強調Swift 2崩潰選項

var cellNumber:Int! = 1 

我真的不知道是什麼原因造成的問題。它我的第一個應用程序請請原諒我的編碼奇怪的方式:

ViewOne.swift

import Foundation 
import UIKit 

     class ViewOne : UITableViewController { 



     override internal func viewDidLoad() { 
      super.viewDidLoad() 

     } 

     var cellNumber:Int! = 1 
     //var cellNumber = 1 

     @IBAction func Cell2Pressed(sender: UIButton) { 
      cellNumber = 2 
      } 

     @IBAction func Cell3Pressed(sender: UIButton) { 
      cellNumber = 3 
     } 


     @IBAction func Cell4Pressed(sender: UIButton) { 
      cellNumber = 4 
     } 

     @IBAction func Cell5Pressed(sender: UIButton) { 
      cellNumber = 5 
     } 

     @IBAction func Cell6Pressed(sender: UIButton) { 
      cellNumber = 6 
     } 

     @IBAction func Cell7Pressed(sender: UIButton) { 
      cellNumber = 7 
     } 

     @IBAction func Cell8Pressed(sender: UIButton) { 
      cellNumber = 8 
     } 

     @IBAction func Cell9Pressed(sender: UIButton) { 
      cellNumber = 9 
     } 



     override internal func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 
     } 

    `// @IBOutlet weak var Cell1Pressed: UITableViewCell!` 


     override internal func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
      let DestViewControler : ViewController = segue.destinationViewController as! ViewController 
      // var Dest2ViewControler : RadioPlayer = segue.destinationViewController as! RadioPlayer 

      DestViewControler.trackNumber = cellNumber! 
      // Dest2ViewControler.trackNumber = cellNumber 




     } 


    } 

ViewControler.swift

`import UIKit` 
`import AVFoundation` 
`import MediaPlayer` 

`class ViewController: UIViewController {` 


    override func canBecomeFirstResponder() -> Bool { 
     return true 
    } 

    static let sharedInstance = ViewController() 
    @IBOutlet var PausePlay: UIButton! 

    var ButtonAudioPlayer = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("ButtonAudio", ofType: "wav")!)) 

    private var BackgroundAudio1 = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ants", ofType: "mp3")!)) 
    private var BackgroundAudio2 = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ants", ofType: "mp3")!)) 
    private var BackgroundAudio3 = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ants", ofType: "mp3")!)) 
    private var BackgroundAudio4 = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ants", ofType: "mp3")!)) 
    private var BackgroundAudio5 = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ants", ofType: "mp3")!)) 
    private var BackgroundAudio6 = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ants", ofType: "mp3")!)) 
    private var BackgroundAudio7 = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ants", ofType: "mp3")!)) 
    private var BackgroundAudio8 = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ants", ofType: "mp3")!)) 
    private var BackgroundAudio9 = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ants", ofType: "mp3")!)) 




    var trackNumber = Int() 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     play() 





     //in info.playlist you need to add 'Required background modes and add to idem 0 ap plays audio airplay then below code to play even when iphone is locked: -marcin 
     /* 
     PausePlay.setTitle("Play", forState: UIControlState.Normal) 

     do { 
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
     } catch _ { 
     } 

     // Do any additional setup after loading the view, typically from a nib. 
     PausePlay.setTitle("Play", forState: UIControlState.Normal) 
     if NSClassFromString("MPNowPlayingInfoCenter") != nil { 
      let image:UIImage = UIImage(named: "logo_player_background")! 
      let albumArt = MPMediaItemArtwork(image: image) 
      let songInfo: NSMutableDictionary = [ 
       MPMediaItemPropertyTitle: "WIND", 
       MPMediaItemPropertyArtist: "DEEP BASE", 
       MPMediaItemPropertyArtwork: albumArt 
      ] 
      MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo as [NSObject : AnyObject] 
      //MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo 
     } 
     if (AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)) { 
      print("Receiving remote control events") 
      UIApplication.sharedApplication().beginReceivingRemoteControlEvents() 
     } else { 
      print("Audio Session error.") 
     } 

*/ 

} 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func PlayAudio1(sender: AnyObject) { 
     ButtonAudioPlayer!.play() 

    } 
    @IBAction func Stop(sender: AnyObject) { 

     stop() 

     //BackgroundAudio1.stop() 
     //BackgroundAudio1.currentTime = 0 

     //PausePlay.setTitle("Play", forState: UIControlState.Normal) 





    } 

    @IBAction func Restart(sender: AnyObject) { 

     BackgroundAudio1!.stop() 
     BackgroundAudio1!.currentTime = 0 
     BackgroundAudio1!.play() 


    } 


    @IBAction func PausePlay(sender: AnyObject) { 

     /* 
     if (BackgroundAudio.playing == true){ 
      BackgroundAudio.stop() 
      PausePlay.setTitle("Play", forState: UIControlState.Normal) 

     } 
     else{ 

      BackgroundAudio.play() 
      PausePlay.setTitle("Pause", forState: UIControlState.Normal) 
      } 
      */ 



     toggle() 



    } 


    private var isPlaying = false 


    func play() { 

     if(trackNumber == 2){ 
      BackgroundAudio2!.play()} 
     if(trackNumber == 3){ 
      BackgroundAudio3!.play()} 
     if(trackNumber == 4){ 
      BackgroundAudio4!.play()} 
     if(trackNumber == 5){ 
      BackgroundAudio5!.play()} 
     if(trackNumber == 6){ 
      BackgroundAudio6!.play()} 
     if(trackNumber == 7){ 
      BackgroundAudio7!.play()} 
     if(trackNumber == 8){ 
      BackgroundAudio8!.play()} 
     if(trackNumber == 9){ 
      BackgroundAudio9!.play()} 

     isPlaying = true 
    } 

    func pause() { 
     if(trackNumber == 2){ 
      BackgroundAudio2!.pause()} 
     if(trackNumber == 3){ 
      BackgroundAudio3!.pause()} 
     if(trackNumber == 4){ 
      BackgroundAudio4!.pause()} 
     if(trackNumber == 5){ 
      BackgroundAudio5!.pause()} 
     if(trackNumber == 6){ 
      BackgroundAudio6!.pause()} 
     if(trackNumber == 7){ 
      BackgroundAudio7!.pause()} 
     if(trackNumber == 8){ 
      BackgroundAudio8!.pause()} 
     if(trackNumber == 9){ 
      BackgroundAudio9!.pause()} 

     isPlaying = false 
    } 

    func pause2() { 

      BackgroundAudio2!.pause() 

      BackgroundAudio3!.pause() 

      BackgroundAudio4!.pause() 

      BackgroundAudio5!.pause() 

      BackgroundAudio6!.pause() 

      BackgroundAudio7!.pause() 

      BackgroundAudio8!.pause() 

      BackgroundAudio9!.pause() 

     isPlaying = false 
    } 


    //dodana przezemnie: 
    func stop() { 
     if(trackNumber == 2){ 
      BackgroundAudio2!.stop()} 
     if(trackNumber == 3){ 
      BackgroundAudio3!.stop()} 
     if(trackNumber == 4){ 
      BackgroundAudio4!.stop()} 
     if(trackNumber == 5){ 
      BackgroundAudio5!.stop()} 
     if(trackNumber == 6){ 
      BackgroundAudio6!.stop()} 
     if(trackNumber == 7){ 
      BackgroundAudio7!.stop()} 
     if(trackNumber == 8){ 
      BackgroundAudio8!.stop()} 
     if(trackNumber == 9){ 
      BackgroundAudio9!.stop()} 

     isPlaying = false 
    } 

    func toggle() { 
     if isPlaying == true { 
      pause() 
     } else { 
      play() 
     } 
    } 

    func currentlyPlaying() -> Bool { 
     return isPlaying 
    } 

    override func remoteControlReceivedWithEvent(event: UIEvent?) { 
     if event!.type == UIEventType.RemoteControl { 
      if event!.subtype == UIEventSubtype.RemoteControlPlay { 
       print("received remote play") 
       play() // these are producing terrible error 
      } else if event!.subtype == UIEventSubtype.RemoteControlPause { 
       print("received remote pause") 
       pause2() // these are producing terrible error 
      } else if event!.subtype == UIEventSubtype.RemoteControlTogglePlayPause { 
       print("received toggle") 
       ViewController.sharedInstance.toggle() 
      } 
     } 
    } 


} 
+0

[錯誤「線程1:斷點2.1」]的可能重複(http://stackoverflow.com/questions/32038226/error-thread-1-breakpoint-2-1) – Moritz

回答

0

馬上蝙蝠,我想你應該說:

var cellNumber: Int? = 1 

表達式Int!展開一個可選變量,但您可以使用Int?來聲明一個可選變量。