tudoricc
tudoricc

Reputation: 729

Conversions in swift

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

Upvotes: 0

Views: 83

Answers (2)

vivien.destpern
vivien.destpern

Reputation: 1150

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.

Upvotes: 1

sapi
sapi

Reputation: 10224

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 = ...

Upvotes: 0

Related Questions