objective c - Check string containing URL for "http://" -
i trying check url entered user, fighting against errors , warnings.
-(bool) textfieldshouldreturn:(uitextfield *)textfield { //check "http://" nsstring *check = textfield.text; nsstring *searchstring = @"http://"; nsrange resultrange = [check rangewithstring:searchstring]; bool result = resultrange.location != nsnotfound; if (result) { nsurl *urladdress = [nsurl urlwithstring: textfield.text]; } else { nsstring *good = [nsstring stringwithformat:@"http://%@", [textfield text]]; nsurl *urladdress = [nsurl urlwithstring: good]; } // open url nsurlrequest *requestobject = [nsurlrequest requestwithurl:urladdress];
they :
nsstring may not respond -rangewithstring
unused variable urladdress in condition "if … else" (for both)
urladdress undeclared : in urlrequest
does have idea do?
nsstring responds rangeofstring:
, not rangewithstring:
.
the variable urladdress declared both in if
statement, , in else
statement. means lives in scope. once leave if/else statement, variable gone.
for url it's best if begins scheme (like "http://"), , code gladly accept apple.http://.com being valid.
you can use hasprefix:
method instead, this:
bool result = [[check lowercasestring] hasprefix:@"http://"]; nsurl *urladdress = nil; if (result) { urladdress = [nsurl urlwithstring: textfield.text]; } else { nsstring *good = [nsstring stringwithformat:@"http://%@", [textfield text]]; urladdress = [nsurl urlwithstring: good]; } nsurlrequest *requestobject = [nsurlrequest requestwithurl:urladdress];
Comments
Post a Comment