In title i used Objective C but this question is viable for other OOP Languages too, my question is this: which of the declarations(samples) below is more efficent?
1)
NSString *urlString=@"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];
Note: As far as i remember from CS193p Video Lectures iOS is good with lazy instantiation(as far as i understand, the first sample is better) but i’m not sure.. Can anyone please give an answer in OOP point of view.. Thanks in advance
There is a slight difference when the code is compiled with ARC and the optimization is turned off: the first example would increment and decrement a reference count on the
urlobject one extra time. When optimization is turned on, however, the compiler should be able to figure out that theurlvariable is not used beyond the call toNSURLRequest‘s initializer, and optimize it out.