【问题标题】:Objective-C/CocoaHTTPServer - Is it possible pass how response a NSString object?Objective-C/CocoaHTTPServer - 是否可以传递如何响应 NSString 对象?
【发布时间】:2013-08-29 15:32:27
【问题描述】:

我在我的项目中使用 CocoaHTTPServer,现在我需要用一个字符串来响应一个简单的 GET 请求。所以我检查了传入的参数,并创建了一个带有我想要返回的字符串的 NSString 对象。但我不知道如何返回这个字符串。显然我不能使用“return xmlList”,因为 xcode 记得指针不兼容。服务器想要一个结果类型 NSObject。我该怎么办?

- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path{

  HTTPLogTrace();
  if ([path isEqualToString:@"/getPhotos"]){
      NSString *xmlList = [[NSString alloc] init];
      MyServerMethods* myServerMethods = [[MyServerMethods alloc] init];
      xmlList = [myServerMethods getPhotos];
      NSLog(@"RETURN PHOTOS LIST %@", xmlList);
  }

  return nil;

}

谢谢

【问题讨论】:

    标签: ios objective-c cocoahttpserver


    【解决方案1】:

    好的,我已经在这个模式下解决了我的问题:

    if ([path isEqualToString:@"/getPhotos"]){
        NSString *xmlList = [[NSString alloc] init];
        MyServerMethods* myServerMethods = [[MyServerMethods alloc] init]; //First, we create an instance of MyServerMethods
        xmlList = [myServerMethods getPhotos];
        NSData* xmlData = [xmlList dataUsingEncoding:NSUTF8StringEncoding];
        HTTPDataResponse *xmlResponse = [[HTTPDataResponse alloc] initWithData:xmlData];
        return xmlResponse;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 2015-06-12
      • 1970-01-01
      相关资源
      最近更新 更多