3

I tried converting this code in Objective C to Swift,

- (IBAction) sendMessage {

    NSString *response  = [NSString stringWithFormat:@"msg:%@", inputMessageField.text];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];
    inputMessageField.text = @"";

}

below is my swift code.

@IBAction func sendMessage() {

        var response  = NSString.localizedStringWithFormat("msg:\(inputMessageField?.text)")
        var data = NSData(Data :response , dataUsingEncoding:NSASCIIStringEncoding)
        outputStream(write:data.bytes, maxLength:data.length);
        inputMessageField?.text = ""

    }

I get an error telling that a data is an extra argument. Please help me solve this.

NorthCat
  • 9,643
  • 16
  • 47
  • 50
Jeyanth
  • 61
  • 10

2 Answers2

1

There is no NSData(Data :response , dataUsingEncoding:NSASCIIStringEncoding) method. You are probably looking for

let data = 
response.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

your whole code should look like this:

let response  = NSString.localizedStringWithFormat("msg:\(inputMessageField?.text)")
if let data = response.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
    outputStream(write:data.bytes, maxLength:data.length);
    inputMessageField?.text = ""
}
else {
    println("could not convert response \"\(response)\" to NSData")
}

use let when ever possible, and check if the failable initalizers return a value

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • Thanku:)(write:UnsafePointer, maxLength:Int)-> $T6 is not identical to NSOutputstream. I get this error for the code above. – Jeyanth Jan 14 '15 at 09:09
  • 1
    @Jeyanthu: This should work: `outputStream.write(UnsafePointer(data.bytes), maxLength:data.length)`. Also note that writing to a stream might actually write *less* bytes than you provided. Compare http://stackoverflow.com/a/25080834/1187415. – Martin R Jan 14 '15 at 10:26
0

The method you're looking for NSData(Data :response , dataUsingEncoding:NSASCIIStringEncoding) does not exist.

you can convert like this

var data = NSData(data :response.dataUsingEncoding(NSASCIIStringEncoding)!)


UPDATED

The method outputStream.write() expects UInt8[] array, so you've to convert your data to UInt8[] Array.

Your code should be,

@IBAction func sendMessage() {
    var response  = NSString.localizedStringWithFormat("msg:\(inputMessageField?.text)")
    var data = NSData(Data :response , dataUsingEncoding:NSASCIIStringEncoding)        
    outputStream.write(UnsafePointer(data.bytes), maxLength:data.length)
    inputMessageField?.text = ""
}
arthankamal
  • 6,341
  • 4
  • 36
  • 51
  • (write:UnsafePointer, maxLength:Int)-> $T6 is not identical to NSOutputstream. I get this error for the code above – Jeyanth Jan 14 '15 at 09:10
  • it's because `outputstream` expects `UInt8[] array`, please see the updated code – arthankamal Jan 14 '15 at 09:46
  • and also the method you're using `outputStream(write())` expects `3 parameters` where it's expecting `UnsafePointer`, but you don't need that method to write into stream, – arthankamal Jan 14 '15 at 09:53
  • 1
    Your `buffer` contains only zeros, not the bytes from `data`. – Martin R Jan 14 '15 at 10:24