0

I know this question must have been asked before in another form. I am trying to write to an NSOutputStream a message I am composing from different things from my view.

@IBAction func sendMessage(sender: UIButton!) {

    var msg = self.messageText.text as String!
    var response = "msgtouser:" + self.nameofSender + ":" + nameofReceiver + ":" + self.messageText.text
    var res : Int

    self.outputStream.write(response, maxLength :response.lengthOfBytesUsingEncoding(NSASCIIStringEncoding))
}

I get an error when I try to make the reponse by concatenating multiple strings. The error I am getting is saying that String is not convertible to UInt8 when I try to concatenate the self.messageText.text.

The same thing happens when I try to add the response variable

Shruti Thombre
  • 989
  • 4
  • 11
  • 27
tudoricc
  • 709
  • 1
  • 12
  • 31
  • 2
    I've noticed that Swift loves to give the UInt8 error whenever a conversion fails, regardless of the actual types involved. Are you sure that all the properties are Strings? What happens if you explicitly type `var response: String`? – sapi Aug 11 '14 at 08:42
  • Yes,this solves it but now the : fails – tudoricc Aug 11 '14 at 08:45
  • I'm not sure what you mean by : . If the new problem is to do with different code, it might be best to open a new question? Unless I'm missing something obvious, I don't see anything like that here. – sapi Aug 11 '14 at 08:48
  • Sorry i meant to put the ":" in bold...I fixed these issues by making the variables with "!" – tudoricc Aug 11 '14 at 08:51
  • @tudoricc Text here uses [Markdown formatting](http://stackoverflow.com/editing-help). For bold, surround the **item** with double asterisks, like `**this**`. – Matt Gibson Aug 11 '14 at 09:39
  • Thank you for the answer.fome reason i thought it was with the HTML tagging.Sorry then – tudoricc Aug 11 '14 at 09:45

2 Answers2

1

You should give a try to the "swift way". It may help as stated in the documentation. ex:

    let multiplier = 3
    let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html

Hope this helps.

vivien.destpern
  • 1,020
  • 1
  • 7
  • 14
0

To put my comment here for future readers:

As of the current beta, the Swift compiler seems to have issues implicitly typing certain expressions.

In order to avoid this, you can add an explicit type to the result variable. This will cause any operators in that expression (eg, the addition ones in this question) to behave more sanely.

So, in the code above, we simply need to do:

var response: String = ...
sapi
  • 9,944
  • 8
  • 41
  • 71