简体   繁体   English

CFStreamCreatePairWithSocketToHost意外地与Swift崩溃

[英]CFStreamCreatePairWithSocketToHost crashes unexpectedly with Swift

Finally time to ask my first question here! 最后在这里问我的第一个问题!

Up front: Xcode 7.1.1, OS 10.11.2, iOS 9.0.2 (on physical device) 预先:Xcode 7.1.1,OS 10.11.2,iOS 9.0.2(在物理设备上)

I'm writing a small app that communicates with my Raspberry Pi. 我正在写一个与我的Raspberry Pi通信的小应用程序。 I've got some working code that's written in Obj-C (for iOS 7) borrowed from a tutorial , and it all works fine for me in Obj-C (connects and behaves as expected with the Pi). 我有一些工作代码是用Obj-C(iOS 7)编写的,从教程中借用,它在Obj-C中都能正常工作(连接和行为与Pi一样)。 The issue lies with rewriting it for Swift/iOS 9 (which is the goal). 问题在于为Swift / iOS 9重写它(这是目标)。

The good bit: 好一点:

func initNetworkCommunication() {
    var readStream: Unmanaged<CFReadStreamRef>?
    var writeStream: Unmanaged<CFWriteStreamRef>?

    CFStreamCreatePairWithSocketToHost(nil, "192.168.1.22", 777, &readStream, &writeStream)

    inputStream = readStream?.takeRetainedValue() as! NSInputStream
    outputStream = writeStream?.takeRetainedValue() as! NSOutputStream

    inputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
    outputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

    inputStream.open()
    outputStream.open()
}

I believe the issue to lie in the above as this is the last method call in the stack, however the application crashes quietly with little information: 我认为问题在于上面,因为这是堆栈中的最后一个方法调用,但应用程序在很少信息的情况下悄悄崩溃:

崩溃的屏幕

Any help would be much appreciated! 任何帮助将非常感激!
Please feel free to ask for more information. 请随时询问更多信息。

ps I understand the formatting on this site is rather "strict", anything I missed, overdid, etc, please let me know :) ps我明白这个网站的格式是相当“严格”,我错过了什么,过度等等,请让我知道:)

I've solved it. 我已经解决了。

Firstly: 首先:

inputStream = readStream?.takeRetainedValue() as! NSInputStream
outputStream = writeStream?.takeRetainedValue() as! NSOutputStream

Should be: 应该:

inputStream = readStream!.takeRetainedValue()
outputStream = writeStream!.takeRetainedValue()

Secondly, I abstracted the connection out of the ViewController (where I had this method initially) to a new class called Connection. 其次,我将ViewController(我最初使用此方法)中的连接抽象为一个名为Connection的新类。

Here's Connection: 这是连接:

import UIKit

class Connection: NSObject, NSStreamDelegate {
    var inputStream: NSInputStream!
    var outputStream: NSOutputStream!

    func connect() {
        var readStream:  Unmanaged<CFReadStream>?
        var writeStream: Unmanaged<CFWriteStream>?

        CFStreamCreatePairWithSocketToHost(nil, "192.168.1.22", 777, &readStream, &writeStream)

        self.inputStream = readStream!.takeRetainedValue()
        self.outputStream = writeStream!.takeRetainedValue()

        self.inputStream.delegate = self
        self.outputStream.delegate = self

        self.inputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
        self.outputStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

        self.inputStream.open()
        self.outputStream.open()
    }
}

Often I find starting to type these helps me out :p 通常我发现开始输入这些可以帮助我:p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM