2013-11-04 116 views
0

这是关于一些日子,我努力尝试将使用Qt的C++代码片段翻译为objective-c,这是为了解析来自服务器的xml作为响应。我真的尝试但无法实现。如果有人可以给我一些帮助。将C++翻译成目标C

void RestClient::_prepareRequest(QNetworkRequest& a_request, const QString& a_uri)     { 
     QSslConfiguration config(QSslConfiguration::defaultConfiguration()); 
     config.setProtocol(QSsl::SslV3); 
     config.setSslOption(QSsl::SslOptionDisableServerNameIndication, true); 
     a_request.setSslConfiguration(config); 
     a_request.setRawHeader("Accept", "application/xml"); 
     a_request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); 
     QByteArray l_api_key; l_api_key.append(toQString(m_api_key)); 
     QByteArray l_request_hash; 
    l_request_hash.append(toQString(_buildRequestHash(toStlString(a_uri)))); 
     a_request.setRawHeader("EMApikey", l_api_key); 
     a_request.setRawHeader("EMRequestHash", l_request_hash); 


     a_request.setUrl(QUrl(a_uri)); 
    } 

- (NSString *)hmacsha1:(NSString *)data secret:(NSString *)key { 

const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding]; 
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding]; 

unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH]; 

CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC); 

NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)]; 

NSString *hash = [HMAC base64EncodedStringWithOptions:(nil)]; 

return hash; 

}

这里是我的代码:

- (void)viewDidAppear:(BOOL)animated { 
[super viewDidAppear:animated]; 

if ([stories count] == 0) { 

    NSString * myServerUrl = @"**************"; 
    NSString * l_api_key = @"*******************************"; 
    NSString * l_secret_key = @"************************************"; 

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[myServerUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]; 
    //do post request for parameter passing 
    [theRequest setHTTPMethod:@"GET"]; 


    [theRequest setValue:@"xml" forHTTPHeaderField:@"xml"]; 

    [theRequest addValue:@"l_api_key" forHTTPHeaderField: l_api_key]; 

    [theRequest addValue:@"l_request_hash" forHTTPHeaderField:[self hmacsha1:l_api_key secret:l_secret_key]]; 

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    if(theConnection) 
    { 
     NSLog(@"we get here !"); 
     NSURL *webData = [[NSMutableData data] retain]; 
     [self parseXMLFileAtURL: webData]; 
     cellSize = CGSizeMake([newsTable bounds].size.width, 60); 
    } 
    else 
    { 
     NSLog(@"theConnection is NULL"); 
    } 

    [theConnection release]; 


} 

//cellSize = CGSizeMake([newsTable bounds].size.width, 60); 

}

+0

为什么你不能继续使用Qt?你的翻译工作是很多无聊的工作! –

+0

因为我没有在我的目标c项目中使用Qt。我们可以在目标c中使用Qt吗? –

+1

然后不要考虑将一些Qt代码翻译成Objective C,而是要从头开始重写*。在这种情况下,除了作为灵感来源之外,您并不关心旧的Qt代码。然后你的问题可能变成:“如何在Objective C中提供一些HTTP数据给一些XML数据”(我不知道这个问题,而不是Objective C中的编码!)。顺便说一句,这样的问题不属于StackOverflow,除非你显示一些代码。 –

回答

2

随着视频游戏发展的背景下,这是常见的有端口的代码从一种语言/架构到另一个。有两种方法来解决这个问题: -

1)取每个函数/类,直接复制代码行。

2)看看系统是如何作为一个黑盒子,输入和输出并自己编写代码。

通常2号是更好的选择,因为你比别人更了解自己的代码,你会以这种方式做得更好,而不是试图去理解另一个开发人员的意图可能与他们有关实现。

因此,我建议您只是试着理解原代码应该做什么,然后在Objective-C中编写自己的实现。如果这涉及到如何实施它的学习曲线,那么你将会更好地研究和学习如何做到这一点。

+1

谢谢你的好建议。 –

+0

不客气; O) – TheDarkKnight