在標題中我使用了Objective C,但是這個問題對其他OOP語言也是可行的,我的問題是這樣的:下面哪個聲明(樣本)更有效?當聲明Objective C對象時效率更高
1)
NSString *[email protected]"string_literal_for_URL";
NSURL *url = [NSURL urlWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:40];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requestFromLogin
delegate:self];
2)
NSURLRequest *requestFromLogin = [NSURLRequest requestWithURL:[NSURL URLWithString[NSString stringWithFormat:@"http://%@:%@/ipad/login.php?username=%@&password=%@",server.text,port.text,username.text,password.text]]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:40];
[[NSURLConnection alloc] initWithRequest:requestFromLogin delegate:self];
注:據我記得從CS193p Video Lectures的iOS好與延遲實例(據我明白,第一樣品是更好)但我不確定..任何人都可以請在OOP的角度給予答案..在此先感謝
這只是我的看法。大部分時間我都會使用示例1,因爲它更易於閱讀。如果您嘗試優化Web服務下載,則此問題不會以任何可衡量的方式提高性能。坦率地說,你過度思考。嘗試http://programmers.stackexchange.com/來獲取這些問題。 – scord 2012-07-20 14:54:33
這基本上不會在性能上產生任何差異。 – Taum 2012-07-20 15:46:35
@ scordova88好評,也可能是回答 – ilhnctn 2012-07-20 18:26:17