设置委托 - 发送到实例的无法识别的选择器

时间:2014-09-11 08:37:53

标签: objective-c xcode macos delegates

在为Mac OS编写应用程序时,我在xcode下的代表遇到了一些问题。如果他们通过TCP在同一个网络中,我想与手机通信。我在项目中集成了以下服务器:

https://github.com/tuscland/osc-echo-example/blob/master/TCPServer.m

https://github.com/tuscland/osc-echo-example/blob/master/TCPServer.h

我在服务器上唯一改变的是我扩展了start方法,以便我可以指定一个特殊的端口。我还将TCPServer.h中的TCPServerDelegation更改为@protocol。

现在我想将委托设置为此类。但后来我收到以下错误:

  

[MobileSync copyWithZone:]:无法识别的选择器发送到实例0x109e0f950

我尝试了很多,但我找不到任何解决方案。

这是我的代码,取出了一些不相关的函数:

MobileSync.h

    #import <Foundation/Foundation.h>
    #import "TCPServer.h"

    @interface MobileSync: NSObject <TCPServerDelegation> {
    }

    -(id)init;
    -(void)StartServer:(int)port;
    -(void)StopServer;

    // Properties

    @end

MobileSync.m

    #import "MobileSync.h"
    #import "TCPServer.h"

    @implementation MobileSync {
        TCPServer *tcpServer;
        // Other variables
    }

    -(id)init {
        self = [super init];
        if (self != nil) {
            // Fill variables with values
        }
        return self;
    }

    -(void)StartServer:(int)port {
        tcpServer = [[TCPServer alloc] init];
        [tcpServer setDelegate:self] // <<<<<<< This line is broken

        NSError *__autoreleasing* error = NULL;
        if ([tcpServer start:port error:error]) {
            NSLog(@"Server started successfully");
        }
    }

    -(void)StopServer {
        if (tcpServer.stop)
            NSLog(@"Server stoped successfully");
    }

    // different sync functions

    // tcpServer Delegate function
    -(void)TCPServer:(TCPServer *)server didReceiveConnectionFromAddress:(NSData *)addr inputStream:(NSInputStream *)istr outputStream:(NSOutputStream *)ostr {
        NSLog(@"Connection received.");
    }

TCPServer.h

    #import <Foundation/Foundation.h>
    #import <CoreServices/CoreServices.h>

    NSString * const TCPServerErrorDomain;

    typedef enum {
        kTCPServerCouldNotBindToIPv4Address = 1,
        kTCPServerCouldNotBindToIPv6Address = 2,
        kTCPServerNoSocketsAvailable = 3,
    } TCPServerErrorCode;

    @interface TCPServer : NSObject {
    @private
        id delegate;
        NSString *domain;
        NSString *name;
        NSString *type;
        uint16_t port;
        CFSocketRef ipv4socket;
        CFsocketRef ipv6socket;
        NSNetService *netService;
    }

    @property (readwrite, copy) id delegate;
    @property (readwrite, copy) NSString *domain;
    @property (readwrite, copy) NSString *name;
    @property (readwrite, copy) NSString *type;
    @property (readwrite) uint16_t port;

    -(BOOL)start:(int)port error:(NSError **)error;
    -(BOOL)stop;
    -(void)handleNewConnectionFromAddress:(NSData *)addr inputStream:(NSInputStream *)istr outputStream:(NSOutputStream *)ostr;

    @end

    @protocol TCPServerDelegation

        -(void)TCPServer:(TCPServer *)server didReceiveConnectionFromAddress:(NSData *)addr inputStream:(NSInputStream *)istr outputStream:(NSOutputStream *)ostr;

    @end

有人可以帮我解决问题吗?那会很棒

0 个答案:

没有答案
相关问题