0

我想使用一个协议,我们如何在 iPhone 中实现它。

///在 POCViewController.h

#进口

@protocol BasicAPI -(NSString*)你好;@end @interface HessianPOCViewController : UIViewController { idbasicAPI;

}

@结尾

///

// 在 POCViewController.m // 在一些方法中

NSURL* url = [NSURL URLWithString@" http://www.caucho.com/hessian/test/basic "];

id proxy = (id)[CWHessianConnection proxyWithURL:url 协议:@protocol(basicAPI)];

NSLog(@"你好:%@", [代理你好]);

////

请帮助我如何实现上述代码?

4

2 回答 2

2

在上面的代码片段中 - @protocol 块位于您的头文件中,位于已经存在的 @end 声明下方。常见的用例是这样的:

@interface MyClass
// properties, method definitions, etc
@end

@protocol BasicAPI

-(NSString*)hello;

@end

然后在你的实现文件中的某个方法体中,MyClass.m

-(void)myMethod { 
   NSURL* url = [NSURL URLWithString@"http://www.caucho.com/hessian/test/basic"];
   id proxy = (id)[CWHessianConnection proxyWithURL:url protocol:@protocol(basicAPI)];
   NSLog(@"hello: %@", [proxy hello]);
}
于 2010-05-10T12:39:31.670 回答
0

我看到您给出的示例取自Hessian Objective-C implementation的文档。它向您展示了如何从 Objective-C 客户端与 Hessian Web 服务进行交互。

您是否有正在尝试与之通信的现有 Hessian Web 服务?如果是这样,您需要在@protocol块中声明该服务的接口。这个问题的答案给出了一些很好的例子来说明这在客户端和服务器端是如何工作的。

于 2010-05-10T12:48:26.980 回答