why is this for loop not getting called? - objective-c

Hello and thanks for the help
Why is this for loop not getting called. (contents is an nsmutableArray)
NSString *setBiz = [[NSString alloc]init];
setBiz = #"MomAndPop";
NSLog(#"??????????listby???????????%#\n",setBiz);
for (NSDictionary *key in self.contents) {
NSLog(#"hi inside loopppp"); //I never see this ????????
NSString *c = [key objectForKey:#"BizName"];
NSString *string = [NSString stringWithFormat:#"%#", key]; //random test
if ([c isEqualToString:setBiz]) {
NSLog(#"gotch you");
}
}

The most likely answer is that self.contents has no elements inside of it.
Place this before your loop to output the number of elements in the loop:
NSLog(#"self.contents.count: %lu", self.contents.count);

Related

Why does NSMutablearray keep returning null?

I am generating a random equation say like 2*3+4..... and using DDMathparser to evaluate it. Here I have a class method which is supposed to return a random equation(stored inside a mutable array) only if it evaluates to a integer.
however it keeps returning Null and i can't figure out why. Please help me out.!
#import "Equation.h"
#import "DDMathParser.h"
#implementation Equation
-(NSMutableArray*)randEquation{
NSMutableArray* usableEquation=[[NSMutableArray alloc]init];
while(1){
NSArray *nums = #[#"1", #"2", #"3", #"4", #"5",#"6",#"7",#"8",#"9"];
unsigned index1=arc4random()%9;
NSString* num = [NSString stringWithFormat:#"%#", [nums objectAtIndex:index1]];
NSArray *symbols = #[#"+", #"-", #"*", #"/"];
unsigned index=arc4random()%4;
NSString* symb = [NSString stringWithFormat:#"%#", [symbols objectAtIndex:index]];
NSMutableArray *arrayOfSymbolsAndNumbers = [[NSMutableArray alloc] init];
for( int i=0;i<=10;i++){
if (i%2==0) {
[arrayOfSymbolsAndNumbers addObject:num];
}
else{
[arrayOfSymbolsAndNumbers addObject:symb];
}
}
NSMutableString *stringOfSymbolsAndNumbers=[[NSMutableString alloc]init];
for (NSObject * obj in arrayOfSymbolsAndNumbers)
{
[stringOfSymbolsAndNumbers appendString:[obj description]];
}
usableEquation=arrayOfSymbolsAndNumbers;
NSNumber *result=[stringOfSymbolsAndNumbers numberByEvaluatingString];
float resultFloat = [result floatValue];
float checker=resultFloat;
if (floor(checker)==checker) {
break;
}
else{
continue;
}
}
return usableEquation;
}
#end
NSLog(#"The content of array is%#",[equation randEquation]);
Based on your code, for this log to output The content of array is(null) means that equation is nil. Your randEquation (while not efficient) looks ok, the problem is that you haven't created the equation instance when you run the log statement.

addObject replaces the previous values

In the following code i am trying to add the user selected names in arrayDoctors, say A,B and then i add it to selectedDoctors array. But the 2nd time i select doctors C,D and add it to selectedDoctors array it replaces the previous objects A,B to C and D.
- (void)doneDoctorSelection:(id)sender
{
[pop3 dismissPopoverAnimated:YES];
NSString *str = [arrayDoctors objectAtIndex:0];
NSString *str1 = [str stringByAppendingString:[NSString stringWithFormat:#" + %d",arrayDoctors.count-1]];
if([arrayDoctors count] == 1)
lblDoctor.text = str;
else
lblDoctor.text = str1;
[selectedDoctors addObject:arrayDoctors];
[selectedDoctorIdList addObject:arrayDoctorsId];
NSLog(#"selectedDoneDoctors %# ",selectedDoctors);
}
What am i doing wrong?
Instead of this part
NSMutableArray *tempDoctor = [[NSMutableArray alloc]initWithArray:arrayDoctors];
[selectedDoctors addObject:tempDoctor];
You can use
[selectedDoctors addObject:[arrayDoctors mutableCopy]];
Ah!! finally found the solution. I was removing the objects from arrayDoctors before putting new values in it, and i had initialized it in viewDidLoad method. So what was happening is when i did [selectedDoctors addObject:arrayDoctors]; the second time it was replacing the same object at 1st place also since its uses the same memory location. The solution was to create a temporary object. The following is the changed code.
- (void)doneDoctorSelection:(id)sender
{
[pop3 dismissPopoverAnimated:YES];
NSString *str = [arrayDoctors objectAtIndex:0];
NSString *str1 = [str stringByAppendingString:[NSString stringWithFormat:#" + %d",arrayDoctors.count-1]];
NSMutableArray *tempDoctor = [[NSMutableArray alloc]initWithArray:arrayDoctors];
NSMutableArray *tempDocorId = [[NSMutableArray alloc]initWithArray:arrayDoctorsId
];
if([arrayDoctors count] == 1)
lblDoctor.text = str;
else
lblDoctor.text = str1;
NSLog(#"Before Adding %# ",selectedDoctors);
[selectedDoctors addObject:tempDoctor];
[selectedDoctorIdList addObject:tempDocorId];
//[selectedDoctors addObjectsFromArray:arrayDoctors];
//[selectedDoctorIdList addObjectsFromArray:arrayDoctorsId];
NSLog(#"selectedDoneDoctors %# ",selectedDoctors);
}
Thanx for pointing me in right direction.

How to view the entire content of an array in a label (xcode 4.1)

i'm programming in Obj-c with xcode4.1, i have an array with numbers in it, and i want to visualize all of them in a label...can anyone help me around this please?
thanks!
this is the code:
combinedString=[[NSMutableArray alloc] init];
NSString *finalStringLabel=#"";
for (i=0; i<=textLength; i++) {
//character coding
char myChar = [myString characterAtIndex:i];
NSString *myCharS=[NSString stringWithFormat:#"%c", myChar];
int asciiCode=[myCharS characterAtIndex:0];
NSString *asciiS=[NSString stringWithFormat:#"%i", asciiCode];
[combinedString addObject:asciiS];
}
finalStringLabel=[NSString stringWithFormat:#"", [combinedString componentsJoinedByString:#"."]];
myLabel.text=finalStringLabel;
[combinedString release];
}
You can use this
NSArray *yourArray;
NSString *createdString = [yourArray componentsJoinedByString:#" "];
myLabel.text = createdString;
As your array is combinedString,
combinedString=[[NSMutableArray alloc] init];
looks like you are providing values after this line or this is not a property (this is a local as you are releasing it later), and your code in not complete.
Anyways,
You don't need to create an empty string and then assign new object to it, need to do as :
myLabel.text=[combinedString componentsJoinedByString:#"."];
[combinedString release];
}

JSON text and variable count

I am reading like this...
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:path];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
// getting the data from inside of "menu"
NSString *message = (NSString *) [data objectForKey:#"message"];
NSString *name = (NSString *) [data objectForKey:#"name"];
NSArray *messagearray = [data objectForKey:#"message"];
NSArray *namearray = [data objectForKey:#"name"];
NSDictionary* Dictionary = [NSDictionary dictionaryWithObjects:message forKeys:name];
for (NSString* Key in [Dictionary allKeys]){
NSLog(#"%# %#",Key,[Dictionary objectForKey:Key]);
}
...this JSON file...
{"message":["Untitled1a","Untitled2a","Untitled3a"],"name": ["Untitled1b","Untitled2b","Untitled3b"]}
...this is the result...
Untitled3b Untitled3a
2012-05-12 11:31:17.983 Quick Homework[721:f803] Untitled1b Untitled1a
2012-05-12 11:31:17.983 Quick Homework[721:f803] Untitled2b Untitled2a
...but for each pair (Untitled 1b 2b) I would like to alloc two UITextFields, witch display the correspondent text...
I tried using this method:
for (NSString *string in messagearray){
}do{
NSLog(#"happt = %i", b);
b++;
}
while(b == b);
//While loop
while (b == b ) {
NSLog(#"x = %i", b);
b++;
}
}
I would like to count the objects in the array in order to repeat an alloc code for UITextField that number of times, and display the text accordingly, but I am not able. Please help!!
Why can't you use -count?
b = [messagearray count]
To directly answer your question:
b = 0;
for (id item in messagearray)
b++;

Comparing strings in objective c

NSString *title = [myWebView
stringByEvaluatingJavaScriptFromString:#"document.title"];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:8];
int j = 0;
int i=0;
int count=0;
int len;
len = [title length];
for(i=0;i<len;i++){
NSString *c = [title substringWithRange:NSMakeRange(i, 1)];
if([c isEqualToString:#","])
{
//array[count]= [title substringWithRange:NSMakeRange(j, 2)];
NSString *xxx = [title substringWithRange:NSMakeRange(j,(i-j))];
NSLog(xxx);
//insert the string into array
[array insertObject:xxx atIndex:count];
j=i;
count = count + 1;
}
}
My app always crashes at the line
[c isEqualToString:#","]
and gives the error - Thread1 : Program received signal: "SIGBART".
I know for sure that the problem is occurring while comparing strings since the app runs if I remove that one line of code.
Can someone please help? Thanks
Consider using:
- (NSArray *)componentsSeparatedByString:(NSString *)separator
Example:
NSString *title = [myWebView stringByEvaluatingJavaScriptFromString:#"document.title"];
NSMutableArray *array = [title componentsSeparatedByString:#","];
If I ends up longer than the string, or is undefined, you've got issues. If you replace i with 1, it doesn't crash, right?
Incidentally, you could use:
unichar c = [title characterAtIndex:i];