如何在swift中创建自己的委托(协议)?

时间:2015-03-10 09:18:44

标签: iphone swift

我是swift的新手,我想在swift中创建协议。我在目标c中也这样做。

@protocol CustomURLConnectionDelegate;

    @interface CustomURLConnection : NSObject <NSURLConnectionDataDelegate,NSURLConnectionDelegate>
    {
        //Data
        NSMutableData *receivedData;

        //Connection
        NSURLConnection *urlConnection;

    }
//Delegate for Protocol
@property(nonatomic, unsafe_unretained) id<CustomURLConnectionDelegate> delegate;


//Init Method
-(CustomURLConnection *)initURLConnectionWithRequest:(NSURLRequest *)request withTag:(NSInteger)apiTag;  
@end
    @protocol CustomURLConnectionDelegate <NSObject>
    @optional
    -(void)getReceivedData:(NSDictionary *)dictionary withTag:(NSInteger)dataTag;
    -(void)getReceivedData:(NSDictionary *)dictionary withTag:(NSInteger)dataTag withObj:(NSObject *)obj;
    -(void)getDidFail:(NSString *)error withTag:(NSInteger)dataTag;
    -(void)bytesReturn:(float)returnBytes expectedTotalBytes:(float)totalBytes withTag:(int)progressTag;
    @end

但是如何在swift中做同样的事情?

1 个答案:

答案 0 :(得分:2)

检查Apple documentation about protocols。简单地说明你可以制定这样的协议:

protocol YourProtocolName {
// protocol definition goes here
}

并称之为:

weak var delegate:YourProtocolName?
相关问题