objective-c – 在swift中实现SimpleFTPSample

我正在尝试将Objective-C应用程序转换为
swift.

我到达了需要通过FTP将文件发送到远程服务器的部分.我能够在Objective-C中实现这一点,但仅仅因为我有苹果为帮助开发人员而制作的
SimpleFTPSample项目.

现在我几乎完成了两种语言之间的转换,但我被卡住了.

我需要转换该代码(来自SimpleFTPSample personal):

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
    // An NSStream delegate callback that's called when events happen on our 
    // network stream.
{
    #pragma unused(aStream)
    assert(aStream == self.networkStream);

    switch (eventCode) {
        case NSStreamEventOpenCompleted: {
            [self updateStatus:@"Opened connection"];
        } break;
        case NSStreamEventHasBytesAvailable: {
            assert(NO);     // should never happen for the output stream
        } break;
        case NSStreamEventHasSpaceAvailable: {
            [self updateStatus:@"Sending"];

            // If we don't have any data buffered, go read the next chunk of data.

            if (self.bufferOffset == self.bufferLimit) {
                NSInteger   bytesRead;

                bytesRead = [self.fileStream read:self.buffer maxLength:kSendBufferSize];

                if (bytesRead == -1) {
                    [self stopSendWithStatus:@"File read error"];
                } else if (bytesRead == 0) {
                    [self stopSendWithStatus:nil];
                } else {
                    self.bufferOffset = 0;
                    self.bufferLimit  = bytesRead;
                }
            }

            // If we're not out of data completely, send the next chunk.

            if (self.bufferOffset != self.bufferLimit) {
                NSInteger   bytesWritten;
                bytesWritten = [self.networkStream write:&self.buffer[self.bufferOffset] maxLength:self.bufferLimit - self.bufferOffset];
                assert(bytesWritten != 0);
                if (bytesWritten == -1) {
                    [self stopSendWithStatus:@"Network write error"];
                } else {
                    self.bufferOffset += bytesWritten;
                }
            }
        } break;
        case NSStreamEventErrorOccurred: {
            [self stopSendWithStatus:@"Stream open error"];
        } break;
        case NSStreamEventEndEncountered: {
            // ignore
        } break;
        default: {
            assert(NO);
        } break;
    }
}

现在我设法做到了:

// An NSStream delegate callback that's called when events happen on our
// network stream.
func stream(theStream: NSStream!, handleEvent streamEvent: NSStreamEvent) {
    switch (streamEvent) {
    case NSStreamEvent.OpenCompleted:
        println("Opened connection")
    case NSStreamEvent.HasBytesAvailable:
        println("Not suposed to happend")
    case NSStreamEvent.HasSpaceAvailable:
        println("Sending")

        // If we don't have any data buffered, go read the next chunk of data.
        if self.bufferOffset == self.bufferLimit {
            var bytesRead: Int
            bytesRead = self.fileStream.read(self.buffer, maxLength: 32768)

            if bytesRead == -1 { self.stopSendWithStatus("File read error") }
            else if bytesRead == 0 { self.stopSendWithStatus(nil) }
            else {
                self.bufferOffset = 0
                self.bufferLimit  = size_t(bytesRead)
            }
        }

        // If we're not out of data completely, send the next chunk.
        if self.bufferOffset != self.bufferLimit {
            var bytesWritten: Int

            bytesWritten = self.networkStream.write(&self.buffer[self.bufferOffset], maxLength: self.bufferLimit - self.bufferOffset)

            if bytesWritten == -1 { self.stopSendWithStatus("Network write error") }
            else { self.bufferOffset += size_t(bytesWritten) }
        }
    case NSStreamEvent.ErrorOccurred:
        self.stopSendWithStatus("Stream open error")
    case NSStreamEvent.EndEncountered:
        println("ignore")
    default:
        println("default")
    }
}

使用的变量类型是:

var isSending = Bool()
var networkStream: NSOutputStream! = NSOutputStream()
var fileStream: NSInputStream! = NSInputStream()
var buffer: CConstPointer<UInt8>
var bufferOffset = size_t()
var bufferLimit = size_t()

我被困在if self.bufferOffset!= self.bufferLimit部分.

显然& self.buffer [self.bufferOffset]不正确(没有成员名称下标),以及self.bufferLimit – self.bufferOffset(找不到接受所提供参数的重载’ – ‘)

说实话,我甚至不确定得到什么& self.buffer [self.bufferOffset]的意思. (不是变量,而是符号.

因此,如果有人知道如何解决这个问题,他将非常欢迎!

您可以找到SimpleFTPSample项目here.

对不起,如果我的英语不完美.

最佳答案 难道你不能简单地将SimpleFTP编译到框架中并直接从swift中使用它的方法吗?无需重写所有内容,Xcode为您做到了这一点.我将它用于许多自定义库,它就像一个魅力.只需添加目标,并放入所需的框架,包括框架,你就完成了!

点赞