如何在swift中转换java代码?

时间:2014-11-25 09:12:49

标签: ios swift

这里我的java代码如下所示:

this.mNetworkConnectionManager = new NetworkConnectionManager(new NetworkConnectionManager.OnNetworkListener() {

            @Override
            public void onNetworkPacketSent(String packet) {
                BASPacket bp = new BASPacket(packet);
                bp.setTimestampedPacket(timestampPacket(packet, "sent"));

                mOnBASMessageListener.onBASMessageSent(bp);
            }


        });

我尝试在swift中转换这些代码,如下所示:

self.mNetworkConnectionManager = NetworkConnectionManager(l: { NetworkConnectionManager.OnNetworkListener() 
            func onNetworkPacketSent(packet : String)
            {
                var bp : BASPacket = BASPacket(packet: packet)
                bp.setTimestampedPacket(timestampPacket(packet, sentrec: "sent"));

                mOnBASMessageListener.onBASMessageSent(bp);
            }
        }())

但是收到编译错误。“由于信号命令失败:分段错误:11”

我在swift中也有如下的NetworkManager类:

public class NetworkConnectionManager
{
    var mOnNetworkListener:OnNetworkListener = OnNetworkListener()

     init(l : OnNetworkListener)
    {
        self.mOnNetworkListener = l

    }
    class OnNetworkListener
    {

    }
}

1 个答案:

答案 0 :(得分:0)

Swift不支持匿名类。如果要覆盖类中的某些内容,则必须快速为其创建一个子类。

public class NetworkConnectionManager
{
    var mOnNetworkListener:OnNetworkListener = OnNetworkListener()

    init(l : OnNetworkListener)
    {
        self.mOnNetworkListener = l

    }
    class OnNetworkListener
    {
        func onNetworkPacketSent(packet : String)
        {
            //default implementation
        }
    }
}

class MyOnNetworkListener : NetworkConnectionManager.OnNetworkListener
{
    override func onNetworkPacketSent(packet: String) {
        var bp : BASPacket = BASPacket(packet: packet)
        bp.setTimestampedPacket(timestampPacket(packet, sentrec: "sent"));

        mOnBASMessageListener.onBASMessageSent(bp);
    }
}

let mNetworkConnectionManager = MyOnNetworkListener()

如果没有默认实现,则可以将其声明为协议

public class NetworkConnectionManager
{
    var mOnNetworkListener:OnNetworkListener

    init(l : OnNetworkListener)
    {
        self.mOnNetworkListener = l
    }
}

protocol OnNetworkListener {
    func onNetworkPacketSent(packet : String)
}

class MyOnNetworkListener : OnNetworkListener
{
    func onNetworkPacketSent(packet: String) {
        var bp : BASPacket = BASPacket(packet: packet)
        bp.setTimestampedPacket(timestampPacket(packet, sentrec: "sent"));

        mOnBASMessageListener.onBASMessageSent(bp);
    }
}

let mNetworkConnectionManager = MyOnNetworkListener()

如果类/协议仅由一种方法组成,则更好的Swifty方法是传递单个闭包。

public class NetworkConnectionManager
{
    var mOnNetworkListener : (String) -> ()

    init(l : @escaping (String) -> ())
    {
        self.mOnNetworkListener = l

    }
}

let mNetworkConnectionManager = NetworkConnectionManager { (packet) in
    var bp : BASPacket = BASPacket(packet: packet)
    bp.setTimestampedPacket(timestampPacket(packet, sentrec: "sent"));

    mOnBASMessageListener.onBASMessageSent(bp);
}