objective C string Confusion - iphone

I am having code
NSString *cellValue1 = [products1 objectAtIndex:indexPath.row];
when i try to print NSLog(#"cell value is %#",cellValue1);
in log i am not getting anything,
if i use %s, i am getting some symbols, not the string located in cellValue1.
Please help me.
Thanks in advance.

Check to make sure that products1 is actually set. It sounds as though it's nil when you send it the -objectAtIndex: message.

It surely means your string is empty...
Check it with the length method...

Solution:
NSString *cellValue1 = [products1 objectAtIndex:indexPath.row];
NSLog([NSString stringWithFormat:#"Cell Value is %#", cellValue1]);
Reason:
NSLog operates with String inputs. While your statement should work, if there is some/any issue with your original cellValue1 string, your original statement will not catch the issue and assure that that NSLog() is being handed a pure string. By using the stringWithFormat: syntax you assure that even if your cellValue1 values is null or nil, you will receive your "cell value is" comment and possible some hint as to what is being passed into the statement by your cellValue1 string.
Testing Note:
If the above doesn't work for you, Test your original string by just using NSLog(cellValue1);. If this doesn't work it will tell you that your original NSString is not properly pulling your product at indexPath.row values.
Hope this helps!

Related

Problem in return value on objective-C

please look at this code:
-(NSString *) myfun:(NSString *)name paramnoone:(int)a paramnotwo:(int)b {
static int numberofcall=0;
if(a>b) {
return name;
}
NSString *secondname = [[NSString alloc]init];
secondname = [name StringByAppendingString:#"test"];
numberofcall++;
return secondname;
}
i have a problem on it, when my code is on "return secondname" next step is going to "return name" on if statement part, im confusing a lot , because c++ does not have this problem,
please help me on solve it,
thanks for ur help and sorry for my bad English.
(Until the question is explained further I can't really answer the question, but still have valuable infos which justify being posted as an answer, so here it goes.)
In the line:
NSString *secondname = [[NSString alloc]init];
You allocate an empty string. But in the very next line:
secondname = [name StringByAppendingString:#"test"];
You overwrite the pointer secondname to the previously allocated empty string, thus creating a memory leak. Since you do not use the empty string at all, remove the first line and turn the second line into:
NSString *secondname = [name StringByAppendingString:#"test"];
Edit: Based on comments to the questions, I think what you're asking is this (correct me if I'm wrong):
You are debugging the method.
While stepping through the method with the debugger, the flow proceeds normally through the method.
But after the numberofcall++; line, the debugger suddenly jumps to the return name; instead of the return secondname; line.
If that's what's happening to you: this is normal behavior, unfortunately. When the debugger reaches a return statement the marker always "jumps" to the first return statement in the method. But even though it doesn't look that way, your return secondname; statement is really executed.

Objective - C Rookie NSString Problem

I have this code:
// Fill out the email body text
NSString *emailBody = (#"Name:%#\nNumber of People:\nDate:", name.text);
NSLog(#"%#", emailBody);
As you can see I'm trying to append name.text to the e-mail body just after "Name:". However, NSLog only outputs the string contained in name.text and none of the rest of the e-mail body. What am I doing wrong here that the code deletes the rest of the string apart from name.text?
E.G if name.text contained the text "Jack", then NSLog would only output "Jack" and not:
Name: Jack
Number of People: x
Date: x
Which is what I am looking for.
Can anyone give me an insight as to what I'm doing wrong?
Thanks,
Jack
Use +stringWithFormat method:
NSString *emailBody = [NSString stringWithFormat:#"Name:%#\nNumber of People:\nDate:", name.text];
What you have now is a valid code, but it doesn't do what you want:
(#"Name:%#\nNumber of People:\nDate:", name.text);
calls a comma operator - it evaluates its 1st parameter, discards it and returns 2nd parameter, so that's why emailBody is eventually filled with name.text value
You should write
NSString *emailBody = [#"Name:%#\nNumber of People:\nDate:" stringByAppendingString:name.text];
Or, if it doesn't compile,
[[NSString stringWithString:#"Name:%#\nNumber of People:\nDate:"] stringByAppendingString:name.text]
generally you want to either use stringWithFormat as was suggested, which creates an autorelease string that follows the format you have, or you can use initWithFormat instead, which creates a string you can manually manage for better memory behavior, if necessary.
some books will insist that for the iphone, which has limited memory, you don't depend on autorelease objects more than it absolutely necessary, so you'd often find this instead:
NSString *emailBody = [[NSString alloc] initWithFormat:#"Name:%#\nNumber of People:\nDate:", name.text];
Then you can use "emailBody" and immediately after you are done with it, put in this line:
[emailBody release];
This is a good habit to get into in general.

Objective-C: Comparing normal strings and strings found in NSMutableArrays

I am confused about strings (a beginner's problem, I'm afraid):
I have one NSMutableArray called Notebook. At index position 1, I have an object, which I think is a string. At least I put it into the array like this:
[NoteBook replaceObjectAtIndex:1 withObject:#"x-x-x-x"];
So far so good. If I put this into an UILabel, it will show x-x-x-x on my screen. The nightmare starts when I try to compare this string with other strings. Let's consider that I do not want to display the string x-x-x-x on my screen, but just to have a blank instead. So I thought I could achieve this by coding this:
NSString *tempDateString;
tempDateString = [NSString stringWithFormat:#"%#",[NoteBook objectAtIndex:1]];
if (tempDateString == #"x-x-x-x") {
UISampleLabel.text = #"";
}
For some reason, this does not work, i.e. even if the string at position 1 of my array is 'x-x-x-x', it will still not set my UISampleLabel to nothing.
I suppose that I am getting confused with the #"" markers. When do I really need them? Why can't I simply code tempDateString = [NoteBook objectAtIndex:1]; without the formatting thing?
Any help and suggestions would be very much appreciated!
You need to compare string with isEqualToString:
if ([tempDateString isEqualToString:#"x-x-x-x"]) {
UISampleLabel.text = #"";
}
In addition to the question that's been answered:
Why can't I simply code tempDateString = [NoteBook objectAtIndex:1]; without the formatting thing?
You can. Why do you think you can't?

int to NSString issue

I use the following statement for into to NSString conversion (with a find/replace)
curr_rep_date = [tmpRptDt stringByReplacingOccurrencesOfString:tmpYrVal withString:[NSString stringWithFormat:(tmpCurrYearInt-1)]];
I have declared
int tmpYrVal;
NSMutableString *tmp_dt,*curr_rep_date;
But the program seems to be crashing and the debugger is not giving any hint.
Could someone help me with the issue and what would be the correct usage.
There's a number of problems here.
Firstly, sringByReplacingOcurrancesOfString:withString: is expecting NSStrings as parameters, not ints. That's the reason why it crashes. The method Is attempting to send a message to a primitive type, not an object.
Secondly, you need to use a proper format string for the stringWithFormat: method. This is the same as how NSLog works.
A format string can look like #"some text %d". It would then be followed by a comma separated list of values to be used in place of the % placeholders.
Example:
[NSString stringWithFormat:#"%d", myIntValue];
Will effectively turn your int into a string, as it creates a new string with a format using your int.
You invoked the stringWithFormat - Method without a format string. [NSString stringWithFormat: #"%i", (tmpCurrYearInt-1)] should solve your problem.
You are missing the format
curr_rep_date = [tmpRptDt stringByReplacingOccurrencesOfString:tmpYrVal withString:[NSString stringWithFormat:#"%d", (tmpCurrYearInt-1)]];
Basic int to NSString conversion works like this:
NSString* s = [NSString stringWithFormat:#"%d", intNumber];

if-query: if (Nslog isEqualtoString #"...") - How can I make this?

I want my app to do something when the last NSLog has a certain string. I thought I could realize this with an if-query and isEqualtoString, but how can I make this?
Sorry for my bad English ;)
Maybe I don't understand what you're trying to do, but you can just create the string somewhere, log it, and then test it:
NSInteger _someInt = 2;
NSString *_someString = #"bananas";
NSString *_stringToBeLogged = [NSString stringWithFormat:#"%d %#", _someInt, _someString];
NSLog(#"%#", _stringToBeLogged);
if ([_stringToBeLogged isEqualToString:#"2 bananas"]) {
NSLog(#"I logged two bananas...");
}
You could consider creating your own custom log function which calls NSLog() after checking for your string constant. This would keep your code a bit cleaner if you want this functionality in multiple places and also allows you to easily extend the logging function further if desired.