代码之家  ›  专栏  ›  技术社区  ›  Community wiki

iPhone上ivar的继承问题

  •  0
  • Community wiki  · 技术社区  · 1 年前

    我正在使用BLIP/MYNetwork库在iPhone和我的电脑之间建立一个基本的tcp套接字连接。到目前为止,代码在模拟器中正确构建和运行,但部署到设备会产生以下错误:

    错误:属性“delegate”正在尝试 使用中声明的ivar“_delegate” “TCPConnection”的超类

    @interface TCPConnection : TCPEndpoint {
        @private
        TCPListener *_server;
        IPAddress *_address;
        BOOL _isIncoming, _checkedPeerCert;
        TCPConnectionStatus _status;
        TCPReader *_reader;
        TCPWriter *_writer;
        NSError *_error;
        NSTimeInterval _openTimeout; }
    
    
    /** The delegate object that will be called when the connection opens, closes or receives messages. */ 
        @property (assign) id<TCPConnectionDelegate> delegate;
    
    /** The delegate messages sent by TCPConnection. All methods are optional. */ 
        @protocol TCPConnectionDelegate <NSObject> 
        @optional 
    
    /** Called after the connection successfully opens. */
        - (void) connectionDidOpen: (TCPConnection*)connection; 
    
    /** Called after the connection fails to open due to an error. */
            - (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error; 
    
    /** Called when the identity of the peer is known, if using an SSL connection and the SSL
                settings say to check the peer's certificate.
                This happens, if at all, after the -connectionDidOpen: call. */
            - (BOOL) connection: (TCPConnection*)connection authorizeSSLPeer: (SecCertificateRef)peerCert; 
    
    /** Called after the connection closes. You can check the connection's error property to see if it was normal or abnormal. */
            - (void) connectionDidClose: (TCPConnection*)connection; 
        @end
    
    
        @interface TCPEndpoint : NSObject {
            NSMutableDictionary *_sslProperties;
            id _delegate; 
    }
        - (void) tellDelegate: (SEL)selector withObject: (id)param;
    @end
    

    有人知道我该怎么解决吗?我会简单地将_delegate声明为基类“TCPEndPoint”的公共属性吗?谢谢你的帮助!

    2 回复  |  直到 15 年前
        1
  •  2
  •   Dave DeLong    15 年前

    看起来TCPEndpoint有一个名为“delegate”的私有实例变量,由于它是私有的,所以这个子类不能访问它。

    如果您需要TCPConnection来拥有一个不同的委托对象,那么我建议您使用以下方法(去掉不必要的东西):

    //TCPConnection.h
    @interface TCPConnection : TCPEndpoint {
      id<TCPConnectionDelegate> _connectionDelegate;
    }
    
    @property (assign) id<TCPConnectionDelegate> delegate;
    
    @end
    
    //TCPConnection.m
    @implementation TCPConnection
    @synthesize delegate=_connectionDelegate;
    
    ...
    @end
    

    基本上,属性语法允许您使用simple=运算符使合成的属性对应于与属性名称不同的实例变量。

        2
  •  1
  •   Kendall Helmstetter Gelner    15 年前

    既然基类是具有_delegate iVar的基类,为什么不在TCPEndpoint基类中定义属性?属性只是像其他方法一样被继承的方法。。。