0

I am running into a problem that causes my app to crash trying to update the contents of a label on the Viewcontroller. Here is my code below.

if numberOfLoops <  numberOfLoopsReq {
    // delay before and play
    counter?.increment()
    //getCounterValue()
    dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue),0)) {
        ViewController().self.numberOfLoopsExecuted.text = self.counter?.count.description
        dispatch_async(dispatch_get_main_queue()) {
            //do nothing here
        }
    }

    let delayMp3 = (GlobalVariables.DelayBeforeSong  + GlobalVariables.DelayAfterSong) * Double(NSEC_PER_SEC)// nanoseconds per second
    let delayTimePlayer = dispatch_time(DISPATCH_TIME_NOW, Int64(delayMp3))
    dispatch_after(delayTimePlayer, dispatch_get_main_queue(),{
        self.player!.play()
    })

    numberOfLoops = GlobalVariables.NumberOfLoops
}

The call that causes the crash is the call to is the:

ViewController().self.numberOfLoopsExecuted.text = self.counter?.count.description 
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sophman
  • 85
  • 2
  • 17
  • You need to provide more details like, 1. what is the error?, 2. Whether this `if numberOfLoops < numberOfLoopsReq {` code block in `ViewController` class – R P Dec 10 '15 at 23:11

1 Answers1

2

Firstly, ViewController().self.numberOfLoopsExecuted.text doesn't make any sense - self refers to "this object" and so providing an instance and then accessing its self is redundant.

More fundamentally ViewController() creates a new instance of your ViewContoller class. Since you aren't creating this instance from a NIB or storyboard scene, none of its IBOutlets will be initialised and you will get a crash unexpectedly found nil. Further, this new instance won't be visible since you haven't presented it in any way.

You don't indicate which class the code you have shown is in, but assuming it is in your ViewController then you just want

self.numberOfLoopsExecuted.text=self.counter?.count.description

If this class isn't your ViewController then you will need to somehow provide a reference to your current, on screen, view controller instance.

You shouldn't use the description function to convert an int to a string; this is just meant for debugging. You should format using either string interpolation or string formatting.

Also, it probably makes more sense to declare counter as var counter=0 rather than var counter? - this will avoid the need to continually unwrap it.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • hi Paulw11's you are correct the error I am getting is unexpectedly found nil – Sophman Dec 11 '15 at 00:49
  • In that case you need to supply a reference to your view controller to this class or perhaps this class could use NSNotification to broadcast the new information or your viewcontroller could be a delegate of this class and this class could call the delegate method. Without knowing more about the structure of your app it is hard to provide more information. Also i just noticed that you are dispatching the update to the label on a queue other than the main queue. You can't do this. All UI updates must be on the main queue – Paulw11 Dec 11 '15 at 19:15
  • thanks for your help. I have been looking at it more and it appears that I need to use KVO as outlined in the link below. http://stackoverflow.com/questions/24092285/is-key-value-observation-kvo-available-in-swift/25219216#25219216 – Sophman Dec 11 '15 at 19:18
  • Avoid KVO if you can. Use NSNotification or delegation if only a single class needs the update. – Paulw11 Dec 11 '15 at 19:20
  • another excellent hint. I have been trying to use KVO and run into problems. Thank you!! – Sophman Dec 11 '15 at 19:21