looping in iphone [duplicate] - objective-c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
For loop and if statement
Hi everyone,
i have a following for loop and preeceding to it a if else condition.m using the following code.
for (int intPrjName=0 ; intPrjName<[arrPrjName count] ;intPrjName++)
{
if ([strSelectedProjectName caseInsensitiveCompare:[arrPrjName objectAtIndex:intPrjName])
{
//some code
}
else
{
//some code
}
}
suppose strSelectedProjectName is "aaa"and the arrPrjName contains "aaa" "bbb" "ccc" .. after the first iteration of for loop if condition gets true i.e string "aaa" matches with the string in array list,it should get out of the loop at the second iteration,i.e it should not enter the else condition..

add a break; command
for (int intPrjName=0 ; intPrjName<[arrPrjName count] ;intPrjName++)
{
if ([strSelectedProjectName caseInsensitiveCompare:[arrPrjName objectAtIndex:intPrjName])
{
//some code
break;
}
else
{
//some code
}
}

Use break to exit the iteration loop.

Use the break keyword:
for (...) {
if (condition) {
// do stuff
break;
} else {
// do other stuff
}
}

You need to use a break; in your loop to break out of the code if you do not want it to continue to loop.
for (int intPrjName=0 ; intPrjName<[arrPrjName count] ;intPrjName++)
{
if ([strSelectedProjectName caseInsensitiveCompare:[arrPrjName objectAtIndex:intPrjName])
{
//some code
break;//Get out of for loop
}
else
{
//some code
}
}

Related

Reading unread emails with selenium

i have the below code to read emails and check if the subject is matching with the expected message
Message[] messages = folder.getMessages();
String message="Thanks for contacting";
if(folder.getUnreadMessageCount()!=0)
{
for (Message mail : messages)
{
if(!mail.isSet(Flags.Flag.SEEN) && mail.getSubject().contains("Thanks"))//if mail is unread and the message matches
{
mail.setFlag(Flags.Flag.SEEN, true);
softAssert.assertTrue(true,"Email received ->");
Reporter.log("Email received ->" + mail.getSubject(), true);
break;
}
if(!mail.isSet(Flags.Flag.SEEN) && !mail.getSubject().contains("Thanks"))//if mail is unread and the message does not match
{
System.out.println(mail.getSubject() + "-> is not the email we are looking for");
}
}
}
else
{
softAssert.assertTrue(false,"Email not received");
Reporter.log("Email not received ->" + message, true);
}
The problem is I want to fail this test if both the conditions inside the for loop fail. if i put the else inside the for loop it prints not received for reach element in the loop. how do i go about this?
You can use boolean expression. See the below simple example,
boolean test = true;
boolean test2 = true;
for (int i = 0; i < 3; i++) {
if (!test) {
System.out.println("Test value is false.");
} else {
test = false;
}
if (!test2) {
System.out.println("Test2 value is false.");
} else {
test2 = false;
}
if(!test && !test2) {
System.out.println("Breaking loop.");
break;
}
}
Create two boolean variables and set their values as true incase if your if condition fails inside the loop then by using else block change the boolean value to false and do the same for your second if condition as well. In the last if condition if both statements fails then break the loop.

where must be break in switch objective-c

What is the difference between this solution:
switch (value)
{
case 1:
{
// some code
} break;
}
When break stay after brackets
And this:
switch (value)
{
case 1:
{
// some code
break;
}
}
When break stay in brackets
A break makes the switch statement to end its execution. If you don't add a break to your case, the next case will start executing. Consider:
NSInteger myInt = 0;
switch (myInt) {
case 0:
NSLog("0");
case 1:
NSLog("1");
default:
NSLog("Default");
}
will print:
0
1
Default
because there are no breaks.
It doesn't matter if you wrap break into a block { break; } because the block changes the scope of variables declared inside of it but has no effect on the break itself.
There is no difference between { ... } break; and { ... break; } because in both versions break is the last statement that is executed and that's all that matters.
However,
case 0:
if (myCondition) {
break;
}
case 1:
...
would have a very different meaning. If myCondition is NO, then the next case (case 1:) is executed too (fall-through) because the break statement has not been executed.

Error: control may reach end of non-void function in Objective C [duplicate]

This question already has answers here:
Warning: control reaches end of non-void function - iPhone
(2 answers)
Closed 6 years ago.
I want to return "K" when setLocationType is "ABC Office" and return "W" when setLocationType is "ABCDE Office". I am getting "control may reach end of non-void function" error and I am not able to proceed further.
+ (NSString*) retrieveLocationType
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *setLocationType = [prefs stringForKey:locationType];
if (setLocationType == #"ABC Office"){
return #"K";
}
else if (setLocationType == #"ABCDE Office"){
return #"W";
}
}
The error is given by the fact that the compiler finds a structure like the following
if (condition1) { return foo; }
else if (condition2) { return bar; }
And what happens when both condition1 and condition2 are false? No return statement is executed but the function must return a value of type NSString. You must change the statements into something like
if (condition1) { .. }
else if (condition2) { .. }
else { return baz; }
or
if (condition1) { .. }
else { .. }
Mind that comparing NSString with == operator compares just the memory address of the objects, you should use isEqualToString:.

Skip first line execution from while loop

I created a code in while loop in Orange HRM.
I am taking data for UID & Pwd from a text file.
But while Executing that it executes 1st line also, which is not necessary.
I want to skip the first line (UID, PWD) and proceed further.
I want the Solution with While as well as with For Loop.
I think It's simple but i am unable to do it immediately.
Please find my code written with while loop.
BufferedReader br = new BufferedReader(new FileReader("E:/WorkSpace/Test/InputData/uid.txt"));
String sCurrentLine;
while ((sCurrentLine = br.readLine())!= null)
{
String str[] = sCurrentLine.split(" ");
driver.findElement(By.id("txtUsername")).sendKeys(str[0]);
driver.findElement(By.id("txtPassword")).sendKeys(str[1]);
driver.findElement(By.id("btnLogin")).click();
Thread.sleep(3000);
if (driver.findElement(By.tagName("a")).getAttribute("id").contains("welcome"))
{
System.out.println("Login is Successful");
temp = true;
break;
}
else
{
System.out.println("Login Failed");
temp = false;
driver.navigate().to("http://opensource.demo.orangehrm.com/");
}
if (temp) {
break;
}
}
If you want to skip the first line, read it before the loop:
br.readLine()
while ((sCurrentLine = br.readLine())!= null)
{
...
Couldn't you just move the code you want executed only once, outside of the loop? Put it right before the loop, and it'll be executed, then go immediately into the loop.
Or you could have something like:
for(int i=0; i<length; i++) {
if (i == 0) {
//code you want executed
}
}

How to write a custom FindElement routine in Selenium?

I'm trying to figure out how to write a custom FindElement routine in Selenium 2.0 WebDriver. The idea would be something like this:
driver.FindElement(By.Method( (ISearchContext) => {
/* examine search context logic here... */ }));
The anonymous method would examine the ISearchContext and return True if it matches; False otherwise.
I'm digging through the Selenium code, and getting a bit lost. It looks like the actual By.* logic is carried out server-side, not client side. That seems to be complicating matters.
Any suggestions?
I do a multi-staged search. I have a method that performs a try catch and then a method that gets the element. In theory you could do a try catch until instead of this way but I like this way better because of my setup.
public bool CheckUntil(IWebDriver driver, string selectorType, string selectorInfo)
{
int Timer = 160;
bool itemFound = false;
for (int i = 0; i < Timer; i++)
if(itemFound)
{
i = 0
}
else
{
Thread.Sleep(500);
if(selectorType.ToLower() == "id" && TryCatch(driver, selectorType, selectorInfo))
{
if(driver.FindElement(By.Id(selectorInfo).Displayed)
{
itemFound = true;
}
}
else if(selectorType.ToLower() == "tagname" && TryCatch(driver, selectorType, selectorInfo))
{
if(driver.FindElement(By.TagName(selectorInfo).Displayed)
{
itemFound = true;
}
}
}
return itemFound;
}
Here's my try catch method you can add as many different types as you want id, cssselector, xpath, tagname, classname, etc.
public bool TryCatch(IWebDriver driver, string selectorType, string selectorInfo)
{
bool ElementFound = false;
try
{
switch(selectorType)
{
case "id":
driver.FindElement(By.Id(selectorInfo);
break;
case "tagname":
driver.FindElement(By.TagName(selectorInfo);
break;
}
ElementFound = truel
}
catch
{
ElementFound = false;
}
return ElementFound;
}
Ok, I figured out how to do this. I'm leveraging driver.ExecuteScript() to run custom js on the webdriver. It looks a bit like this:
function elementFound(elem) {
var nodeType = navigator.appName == ""Microsoft Internet
Explorer"" ? document.ELEMENT_NODE : Node.ELEMENT_NODE;
if(elem.nodeType == nodeType)
{
/* Element identification logic here */
}
else { return false; }
}
function traverseElement(elem) {
if (elementFound(elem) == true) {
return elem;
}
else {
for (var i = 0; i < elem.childNodes.length; i++) {
var ret = traverseElement(elem.childNodes[i]);
if(ret != null) { return ret; }
}
}
}
return traverseElement(document);