我可以用 Objective-C 代码获取系统代理,但我想用 c++ 编写它。你能帮我将 Objective-C 代码转换为 C++ 吗?
我对 Objective-C 不熟悉,所以我很难理解它并将其转换为 C++。 Objective-C代码如下,它将获取iOS系统代理。你能帮我把它转换成c++吗?
NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary *)CFNetworkCopySystemProxySettings() autorelease]);
NSArray *proxies = NSMakeCollectable([(NSArray *)CFNetworkCopyProxiesForURL((CFURLRef)[NSURL URLWithString:@"http://www.google.com"], (CFDictionaryRef)proxySettings) autorelease]);
NSDictionary *settings = [proxies objectAtIndex:0];
NSLog(@"host=%@", [settings objectForKey:(NSString *)kCFProxyHostNameKey]);
NSLog(@"port=%@", [settings objectForKey:(NSString *)kCFProxyPortNumberKey]);
NSLog(@"type=%@", [settings objectForKey:(NSString *)kCFProxyTypeKey]);
I'm not familiar with objective-c, so it is hard for me to understand it and convert it to c++.
The objective-c code as follows, it will get the iOS system proxy. Could you help me to convert it to c++?
NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary *)CFNetworkCopySystemProxySettings() autorelease]);
NSArray *proxies = NSMakeCollectable([(NSArray *)CFNetworkCopyProxiesForURL((CFURLRef)[NSURL URLWithString:@"http://www.google.com"], (CFDictionaryRef)proxySettings) autorelease]);
NSDictionary *settings = [proxies objectAtIndex:0];
NSLog(@"host=%@", [settings objectForKey:(NSString *)kCFProxyHostNameKey]);
NSLog(@"port=%@", [settings objectForKey:(NSString *)kCFProxyPortNumberKey]);
NSLog(@"type=%@", [settings objectForKey:(NSString *)kCFProxyTypeKey]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您找到的代码示例已经使用了 C API(CFNetworkCopySystemProxySettings、CFNetworkCopyProxiesForURL),然后将进行一些额外的工作,将结果转换为对 Objective-C 程序更友好的内容(例如,调用 NSMakeCollectable 来启用 Objective-C 垃圾处理)收藏)。
如果您知道“CF*”调用是“Core Foundation”API,可以从 C(或 C++)调用,而“NS*”是 Objective-C 类/方法,那么它可能会帮助您理解这一点。因此,您的任务是仅使用 Core Foundation 调用 & 来重新转换代码。数据类型。例如,CFDictionary 和 NSDictionary 是可以互换的(用 Apple 的说法是“免费桥接”),因此您可以使用 CFDictionary API 而不是 NSDictionary 来处理 C/C++ 程序中的字典。
要了解它是如何工作的(而不是仅仅要求其他人为您翻译),您需要研究如下功能:
CFNetworkCopySystemProxySettings
CFURLCreateWithString
CFNetworkCopyProxiesForURL
CFDictionaryGetValue
您还需要对 Core Foundation内存管理规则
The code example you found is already using C APIs (CFNetworkCopySystemProxySettings, CFNetworkCopyProxiesForURL), and is then going to some additional work to convert the results to something more friendly for an Objective-C program (for example, calling NSMakeCollectable to enable Objective-C garbage collection).
It may help you to understand this if you know that the "CF*" calls are "Core Foundation" API, which is callable from C (or C++), and "NS*" things are Objective-C classes/methods. So your task is to re-cast the code using just the Core Foundation calls & datatypes. For example, CFDictionary and NSDictionary are interchangeable ("toll-free bridged", in Apple parlance), so you can handle the dictionary from a C/C++ program by using the CFDictionary API instead of NSDictionary.
To understand how this is working (instead of just asking someone else to translate it for you), you will want to research functions like:
CFNetworkCopySystemProxySettings
CFURLCreateWithString
CFNetworkCopyProxiesForURL
CFDictionaryGetValue
You'll also need a basic understanding of Core Foundation memory management rules
谢谢大卫·盖尔哈。根据您的建议,我终于写出了代码,它可以在iOS和MAC上运行:
Thanks David Gelhar. With your suggestion, I wrote out the code at last, it can work on iOS and MAC: