c++ - 将 C++ 转换为 Objective C

标签 c++ objective-c xml qt

这几天我一直在努力做考古学工作,试图将使用 Qt 的 c++ 代码片段翻译成 objective-c,它用于发送请求以解析来自服务器的 xml 作为响应。我真的很努力但做不到。如果有人能给我一些帮助。

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);

最佳答案

具有视频游戏开发背景,将代码从一种语言/架构移植到另一种语言/架构是很常见的。有两种方法可以解决这个问题:-

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

2) 将系统作为一个黑匣子来看待,包括输入和输出,然后自己编写代码。

通常数字 2 是更好的选择,因为您比其他人的代码更了解自己的代码,这样您会做得更好,而不是试图努力理解其他开发人员对其实现的意图.

因此,我建议您只是尝试理解原始代码应该做什么,然后在 Objective-C 中编写您自己的实现。如果这涉及如何实现它的学习曲线,那么您最好研究并了解执行此操作所需的条件。

关于c++ - 将 C++ 转换为 Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19764292/

相关文章:

python - 尝试读取在 Python 中创建的对象时访问冲突传递给 C++ 端的 std::vector 然后返回给 Python

javascript - 将 XML 文件加载到 JavaScript 数组中

asp.net - XSLT 母版页?

c++ - cudaMemcpy 无效参数

c++ - 解析已删除的 pdf

Ios - 如何在不同的 View Controller 中启动计时器

iphone - 隐藏默认键盘

ios - NSPredicate 过滤

javascript - 给我一个从 Javascript 对 KML 文档执行 xpath 查询的示例

c++ - 为什么 fstream.read 工作而不是 >>?