Problem in return value on objective-C - iphone

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.

Related

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?

how to define a returning NSString function in Objective-C / Xcode using a temporary variable?

I would like to define the following function in Objective-C. I have provided pseudo-code to help illustrate what I'm trying to do.
PSEUDOCODE:
function Foo(param) {
string temp;
if(param == 1) then
temp = "x";
else if(param == 2) then
temp = "y";
else if(param == 3) then
temp = "z";
else
temp = "default";
end if
return temp;
}
For some reason if I do this... the variable who I assign it to results in a "BAD Access" error.
I don't know what the difference between:
static NSstring *xx;
or the non-static:
NSString *xx;
declarations are, and how or why I would want to use one over the other.
I also do not fully understand the initializers of NSString, and how they differ. For example:
[[NSString alloc] initWithString:#"etc etc" ];
or the simple assignment:
var = #""
or even:
var = [NSString stringWithString:#"etc etc"];
Can you give me a hand please?
So far, using the NSString value returned from functions like those listed above, always causes an error.
static NSstring *xx;
That declares a statically allocated variable, much like it does in C.
NSstring *xx;
Inside a method that declares a normal local stack variable, just as it does in C.
As you should be aware, the difference between the two is that the first will keep its value between invocations of the function (and can cause trouble if the function is called from multiple threads).
[[NSString alloc] initWithString:#"etc etc"]
That creates a new NSString object, with the contents etc etc. This may or may not be the same as any other NSString object in your program with the same contents, but you don't have to care. Memory management wise, you own it, so you are responsible for ensuring that you eventually call release or autorelease on it to avoid leaking memory.
#"etc etc"
[NSString stringWithString:#"etc etc"]
Those are basically the same. Both give you an NSString object with the contents etc etc. This may or may not be the same as any other NSString object in your program with the same contents, but you don't have to care. Memory management wise, you do not own it, so you must not call release or autorelease on the object unless you first took ownership by calling retain. Also, since you do not own it, you can use it within your method, pass it as a parameter to other methods, and even use it as the return value from your method, but you may not store it in an ivar or static variable without taking ownership by calling retain or making a copy (with copy).
Also, note that "" and #"" are very different. The first gives you a const char * exactly as it does in C, while the second gives you an NSString object. Your program will crash if you use a const char * where the code expects an NSString object.
You can do it this way:
- (NSString *)functionName:(int)param {
NSString *result = nil;
switch (param) {
case 1:
result = [NSString stringWithString:#"x"];
break;
case 2:
result = [NSString stringWithString:#"y"];
break;
case 3:
result = [NSString stringWithString:#"z"];
break;
default:
result = [NSString stringWithString:#"defaultv"];
break;
}
return result;
}
Post real code, not pseudo code, as it makes it much easier to answer your question in concrete terms.
Given that you indicate that you are quite new to Objective-C, I would suggest starting with the language guide and then moving on to the memory management guide.

Very strange problem about NSString

A very strange behavior about this function,
void* getNSString(const NSString* str){
str = #"This is new test";
//NSString* str1 = #"so strange test";
return;
}
Then
NSLog(#"%#",getNSString(#"test"));
The result will be
This is new test
if uncomment
NSString* str1 = #"so strange test";
My understanding is that nothing is returned, so that should be NULL , why print out those string ?
Then result will be
so strange test
I don't believe that nothing is returned. I believe that it's undefined. In other words, anything could be returned.
In this case, it looks like it's returning whatever happened to be on the stack at a given location. That happens to be one of the strings you modified or created but I can assure you this is a fortunate accident (or unfortunate since a crash would probably be better).
If you want nothing to be returned, you need to change:
void* getNSString(const NSString* str){
to:
void getNSString(const NSString* str){
cocoa/objective c does this way.... I also face this type problem before,
for example :
-(NSObject*) someMethod ()
{ ...
NSString* result =#"some value";
// because too busy ,I just forgot write the return statement during dev :)
}
although nothing was return like above code, but the app also can return the result value ....
that is very funny....

NSString stringWithFormat swizzled to allow missing format numbered args

Based on this SO question asked a few hours ago, I have decided to implement a swizzled method that will allow me to take a formatted NSString as the format arg into stringWithFormat, and have it not break when omitting one of the numbered arg references (%1$#, %2$#)
I have it working, but this is the first copy, and seeing as this method is going to be potentially called hundreds of thousands of times per app run, I need to bounce this off of some experts to see if this method has any red flags, major performance hits, or optimizations
#define NUMARGS(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int))
#implementation NSString (UAFormatOmissions)
+ (id)uaStringWithFormat:(NSString *)format, ... {
if (format != nil) {
va_list args;
va_start(args, format);
// $# is an ordered variable (%1$#, %2$#...)
if ([format rangeOfString:#"$#"].location == NSNotFound) {
//call apples method
NSString *s = [[[NSString alloc] initWithFormat:format arguments:args] autorelease];
va_end(args);
return s;
}
NSMutableArray *newArgs = [NSMutableArray arrayWithCapacity:NUMARGS(args)];
id arg = nil;
int i = 1;
while (arg = va_arg(args, id)) {
NSString *f = [NSString stringWithFormat:#"%%%d\$\#", i];
i++;
if ([format rangeOfString:f].location == NSNotFound) continue;
else [newArgs addObject:arg];
}
va_end(args);
char *newArgList = (char *)malloc(sizeof(id) * [newArgs count]);
[newArgs getObjects:(id *)newArgList];
NSString* result = [[[NSString alloc] initWithFormat:format arguments:newArgList] autorelease];
free(newArgList);
return result;
}
return nil;
}
The basic algorithm is:
search the format string for the %1$#, %2$# variables by searching for %#
if not found, call the normal stringWithFormat and return
else, loop over the args
if the format has a position variable (%i$#) for position i, add the arg to the new arg array
else, don't add the arg
take the new arg array, convert it back into a va_list, and call initWithFormat:arguments: to get the correct string.
The idea is that I would run all [NSString stringWithFormat:] calls through this method instead.
This might seem unnecessary to many, but click on to the referenced SO question (first line) to see examples of why I need to do this.
Ideas? Thoughts? Better implementations? Better Solutions?
Whoa there!
Instead of screwing with a core method that you very probably will introduce subtle bugs into, instead just turn on "Static Analyzer" in your project options, and it will run every build - if you get the arguments wrong it will issue a compiler warning for you.
I appreciate your desire to make the application more robust but I think it very likely that re-writing this method will more likely break your application than save it.
How about defining your own interim method instead of using format specifiers and stringWithFormat:? For example, you could define your own method replaceIndexPoints: to look for ($1) instead of %1$#. You would then format your string and insert translated replacements independently. This method could also take an array of strings, with NSNull or empty strings at the indexes that don't exist in the “untranslated” string.
Your method could look like this (if it were a category method for NSMutableString):
- (void) replaceIndexPointsWithStrings:(NSArray *) replacements
{
// 1. look for largest index in "self".
// 2. loop from the beginning to the largest index, replacing each
// index with corresponding string from replacements array.
}
Here's a few issues that I see with your current implementation (at a glance):
The __VA_ARGS__ thingy explained in the comments.
When you use while (arg = va_arg(args, id)), you are assuming that the arguments are nil terminated (such as for arrayWithObjects:), but with stringWithFormat: this is not a requirement.
I don't think you're required to escape the $ and # in your string format in your arg-loop.
I'm not sure this would work well if uaStringWithFormat: was passed something larger than a pointer (i.e. long long if pointers are 32-bit). This may only be an issue if your translations also require inserting unlocalised numbers of long long magnitude.

objective C string Confusion

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!