title: EXC_BAD_INSTRUCTION when setting UILabel.text in swift
body: <p>I am a swift newbie. I am studying iOS Programming The Big Nerd Ranch Guide 4th Edition. The code in the book is in objc. I am translating them to swift. The following code from page 150 does not work for me. It throws EXC_BAD_INSTRUCTION. Could anybody please point where I am wrong? Thank you for your help.</p>
  
  <pre><code>func drawHypnoticMessage(message: NSString) {
      for i in 0..20 {
          let messageLabel = UILabel()
          messageLabel.backgroundColor = UIColor.clearColor()
          messageLabel.textColor = UIColor.whiteColor()
          messageLabel.text = message // Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode0x0)
  
          messageLabel.sizeToFit()
  
          let width = self.view.bounds.size.width - messageLabel.bounds.size.width
  
          let x = Int(arc4random()) % Int(width)
  
          let height = (self.view.bounds.size.height - messageLabel.bounds.size.height)
          let y = Int(arc4random()) % Int(height)
  
          var frame = messageLabel.frame
          frame.origin = CGPointMake(CGFloat(x), CGFloat(y))
          messageLabel.frame = frame
  
          self.view.addSubview(messageLabel)
      }
  }
  </code></pre>
  
  <p>NB: I have tried replacing </p>
  
  <pre><code>messageLabel.text = message
  </code></pre>
  
  <p>with</p>
  
  <pre><code>messageLabel.text = "text"
  </code></pre>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>I found the solution</p>
  
  <pre><code>func drawHypnoticMessage(message: NSString) {
      for i in 0..&lt;20 {
          let messageLabel = UILabel()
          messageLabel.backgroundColor = UIColor.clearColor()
          messageLabel.textColor = UIColor.whiteColor()
          messageLabel.text = message
  
          messageLabel.sizeToFit()
  
          let width = self.view.bounds.size.width - messageLabel.bounds.size.width
  
          let x = Int(arc4random() % UInt32(width))
  
          let height = (self.view.bounds.size.height - messageLabel.bounds.size.height)
          let y = Int(arc4random() % UInt32(height))
  
          var frame = messageLabel.frame
          frame.origin = CGPointMake(CGFloat(x), CGFloat(y))
          messageLabel.frame = frame
  
          self.view.addSubview(messageLabel)
      }
  }
  </code></pre>
  
  <p>Basically, I change <code>let x = Int(arc4random()) % Int(width)</code> to <code>let x = Int(arc4random() % UInt32(width))</code>. <strong>Does anybody know why it work?</strong></p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>Latest revision using <code>arc4random_uniform</code>:</p>
  
  <pre><code>func drawHypnoticMessage(message: NSString) {
      for i in 0..&lt;20 {
          let messageLabel = UILabel()
          messageLabel.backgroundColor = UIColor.clearColor()
          messageLabel.textColor = UIColor.whiteColor()
          messageLabel.text = message
  
          messageLabel.sizeToFit()
  
          let width = self.view.bounds.size.width - messageLabel.bounds.size.width
  
          let x = CGFloat(arc4random_uniform(UInt32(width)))
  
          let height = (self.view.bounds.size.height - messageLabel.bounds.size.height)
          let y = CGFloat(arc4random_uniform(UInt32(height)))
  
          var frame = messageLabel.frame
          frame.origin = CGPointMake(x, y)
          messageLabel.frame = frame
  
          self.view.addSubview(messageLabel)
      }
  }
  </code></pre>