Building up a URL in Objective-C [closed]

随声附和 提交于 2020-01-07 09:16:10

问题


I want to make a request to diffrent servers I only want to fill in the url or ip address in a textfield. I build different solutions but they all faild. I think with this one i'm close.

This is what I build so far:

NSString *part0 = @"http://";
NSString *part1 = [NSString stringWithFormat:@"%@" , _serveradres.text ];
NSString *part2 = @"/API/";
NSString *compURL = [NSString stringWithFormat:@"%@" , part0 , part1 , part2 ];

NSURL *url = [NSURL URLWithString:compURL];



ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

Only the address is diffrent, the api's are all ways on the same place.

Later on i want to make a switch that can choose http or https


回答1:


You should change this:

NSString *compURL = [NSString stringWithFormat:@"%@" , part0 , part1 , part2 ];

To this:

NSString *compURL = [NSString stringWithFormat:@"%@%@%@" , part0 , part1 , part2 ];

Because now your compURL would be just http://

Hope it helps




回答2:


Do like this

NSString *compURL = [NSString stringWithFormat:@"http://%@/API/", serverAddr];



回答3:


No need for all those instantiated NSString's:

NSString *compURLString = [NSString stringWithFormat:@"http://%@/API/", _serveradres.text]; // Probably ought to be self.serveradres.text instead.
NSURL *url = [NSURL URLWithString:compURLString];

*Note - I changed the name for your pointer to compURL to compURLString. The habit of naming your variables after what they do AND what they are will save you headache later.



来源:https://stackoverflow.com/questions/16437346/building-up-a-url-in-objective-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!