I am fairly new to Objective-C and whilst running the Clang static analyser this section of code gave me the following error
warning: Pass-by-value argument in message expression is undefined
[artistCollection removeObject:removeArtist];
Can anyone cast any light on this warning for me?
case 6:
NSLog(#"(*) - First Name:");
scanf("%s", userFirName);
objFirName = [[NSString alloc] initWithUTF8String:userFirName];
for(eachArtist in artistCollection) {
if([[eachArtist firName] isEqualToString: objFirName]) {
removeArtist = eachArtist;
}
}
[artistCollection removeObject:removeArtist];
[objFirName release], objFirName = nil;
break;
gary
If you never get a match on that if inside your loop (because userFirName isn't in your collection), removeArtist will never get assigned a value. Assign it a value before starting the loop (nil, probably), and you should be fine.
Related
I was doing some tinkering with tree traversals (which I have solved in a much more straightforward way) but I have come across an issue in the following piece of Objective C logic:
- (NSString *)someWrapperFunction
{
NSString *result = #"";
NSString *(^appendBlock)(int, NSString **) = ^NSString *(int a, NSString **adder){
if (a == 0)
{
// base case
return #"";
}
NSLog(#"%d", a);
*adder = [*adder stringByAppendingFormat:#"-%d-", a];
NSLog(#"adder: %#", *adder);
return [*adder stringByAppendingString:appendBlock(a-1, adder)];
};
appendBlock(5, &result);
return result;
}
Basically, I want to create a block of code that concatenates numbers into the given string (adder). The result should be: "-5--4--3--2--1-".
I get a segmentation fault with the above code but with some other code that I wrote for the tree traversal, the adder string was essentially not getting updated. Any pointers to what I am doing wrong here? (Is it possible that the variable that is being updated by the inner block (inside recursion) is disallowed as it is already being occupied by the outer block OR is it just that NSString is non-mutable data type?)
In any case, I want to keep the design of the function the same; how would I solve this problem (using c/objective)?
After some searching and experimenting I found a way to fix this.
There is no reason to be using a double-pointer for your adder parameter in the block. Just use a regular pointer and update your code accordingly.
The error is coming from the fact that inside of the block, appendBlock is NULL and you end up dereferencing the NULL pointer trying to call it.
Here's an updated version that works:
- (NSString *)someWrapperFunction
{
NSString *result = #"";
NSString *(^appendBlock)(int, NSString *);
__block __weak NSString *(^weakBlock)(int, NSString *);
weakBlock = appendBlock = ^NSString *(int a, NSString *adder){
NSString *(^innerBlock)(int, NSString *) = weakBlock;
if (a == 0)
{
// base case
return #"";
}
NSLog(#"%d", a);
adder = [adder stringByAppendingFormat:#"-%d-", a];
NSLog(#"adder: %#", adder);
// Split this update to make it easier to debug.
NSString *update = innerBlock(a-1, adder);
return [adder stringByAppendingString:update];
};
appendBlock(5, result);
return result;
}
Output: "-5--4--3--2--1-"
This update is rewritten for point #1 (which really has nothing to do with your original issue.
To solve point #2 this update creates the original appendBlock variable as well as a new __block __weak weakBlock reference to the same block. And then inside the block, a new (strong) block pointer is created to reference the weak block pointer. Without the use of the weak pointer, the code works but causes a warning.
I'm not exactly sure how to check whether a NSString is blank or not, I've got this code...
NSString *imageName = [myItem objectForKey:#"iconName"];
if(imageName == #"")
{
}
And when I do a print on the myItem object, it comes up as..
iconName = "";
At the NSString *imageName line, I noticed in xcode in the console it says
"variable is not NSString"
Which I don't get as iconName is saved and stored on the parse.com database as a NSString.
When I run that code though it doesn't seem to realise that imageName = "";
You should use this code block when comparing strings:
if ([imageName isEqualToString:#""]){
}
You need to use isEqualToString to compare two strings. If you just use == then you are comparing two pointers.
You could also check to see if the object you are receiving is a NSString by:
if ([imageName isKindOfClass:[NSString class]])
Hope this helps.
Although you have a few answers already, here is my take.
First of all, your warning (not error) can be fixed like this:
NSString *imageName = (NSString *)[myItem objectForKey:#"iconName"];
Then, I would check to make sure that the string is not nil and that it is not blank. The easiest way to do this in objective-C is to check the length of the string, since if it nil it will return 0, and if it is empty, it will return 0:
if([imageName length] == 0)
{
// This is an empty string.
}
As #jlehr points out, if there is the possibility that imageName may not actually be stored as a string, then in order to prevent a crash you need to check first. (This may or may not be needed, depending on the logic of your application):
if ([imageName isKindOfClass:[NSString class]]
{
if([imageName length] == 0)
{
// This is an empty string.
}
}
The "variable is not NSString" is probably because objectForKey: return an id.
To should use [imageName isEqualToString:#""].
I got a error in my app and as I tried to fix it I had a really strange behavior when I debugged my method.
Check the following code:(testString is an NSString)
NSLog(#"logging:AAAA%#AAAA",[testObject testString]);
if ([[testObject testString] isEqualToString:#"(null)"]) {
NSLog(#"yeah im here!");
}
and its printing:
logging:AAAA(null)AAAA
But it never reaches the "yeah im here!". How is that possible?
The output of testString is nil.
NSLog produces "(null)" when passed a nil object. However calling a method (like isEqualToString) on a nil object evaluates to nil again and thus to false in the "if".
NSLog always prints the description of an Object.
In fact it's NS_FORMAT_FUNCTION.
FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2);
So:
NSString *stringForNSLog = [NSString stringWithFormat:#"%#",[testObject testString]];
the *stringForNSLog should exactly be "(null)".
But the value of testString is nil.
If you're looking into GNUStep, an open-source implementation of Apple's Cocoa, you'll find something like this:
all the string format things are written in GSFormat.m
and in GSFormat.m
size_t len;
id obj;
NSString *dsc;
obj = args_value[specs[nspecs_done].data_arg].pa_object;
if (!obj) dsc = #"(null)";
else if ([obj respondsToSelector: #selector(descriptionWithLocale:)]) dsc = [obj descriptionWithLocale: locale];
else dsc = [obj description];
http://svn.gna.org/svn/gnustep/libs/base/trunk/Source/NSString.m
http://svn.gna.org/svn/gnustep/libs/base/trunk/Source/GSFormat.m
Because testString method returns nil and calling a method on nil does nothing.
NSLog prints the literal (null) if your NSString is nil. Change your test to:
if ([[testObject testString] == nil)
Because the testObject is nil itself.
You could test:
if ((testObject==nil)||([[testObject testString] isEqualToString:#"(null)"])) {
NSLog(#"yeah im here!");
}
if ((testObject==nil)||([[testObject testString] isEqualToString:#"(null)"])) {
NSLog(#"yeah im here!");
}
I'm very new to objective-c and I'm getting a basic error and unsure on how to fix it.
for(ZBarSymbol *sym in syms) {
resultText.text = sym.data; //sym.data = 0012044012482
[self phpPost:(int)sym.data];
break;
}
}
- (void)phpPost: (int)barcode {
NSString *theValue = [NSString stringWithFormat:#"%#", barcode]; //problem line
labelScan.text = theValue;
//labelScan.text = #"Barcode scanned";
}
when i use #"%#" the text of the label is correct (0012044012482), but when i use #"%d" it isn't (random number every time i restart the app). I would like to use #"%#" (or something that works) but for some reason xCode is giving me an error. and I'm unsure on how to fix the error.
The error is:
Format specifies type 'id' but the argument has type 'int'
In the end I plan on having that code (sym.data) written to a MySQL database using the POST method.
You can't just convert it to an int by casting if it's an object (which it must be if the %# format specifier isn't causing a crash).
Assuming from the fact that you're assinging it directly to a label's text that it's an NSString, you should either change the parameter type of phpPost:
- (void)phpPost: (NSString *)barcode {
labelScan.text = barcode;
}
or extract the intValue before passing sym.data:
[self phpPost:[sym.data intValue]];
and then use the proper %d format specifier in phpPost:.
Your barcode isn't an int, it is an NSString. Instead of doing (int)sym.data, pass in [sym.data intValue]. That should correctly convert it to an integer.
The reason you get a random number is because you can't just cast a string object to a primitive data type :)
I don't know what type sym.data is, but it is likely a pointer to an object, and not the value itself. You cast that pointer to int, so when you are using %d you are effectively printing the memory location of the object. That is why it changes each time you run the program (Objective-C let's you do this without any warnings - something to watch out for).
To fix this, either extract the integer value you need from the sym.data object using it's properties; or pass the object as a pointer. For instance, you could try calling your method like this:
[self phpPost:sym.data];
And then change your method to be:
- (void)phpPost: (id)barcode {
NSString *theValue = [NSString stringWithFormat:#"%#", barcode];
labelScan.text = theValue;
}
Ok, I did some thinking while I was at work today, and I figured out that an INT isn't going to work for me. if I make that object to an int, I would loss some data that is vital to what I'm doing. eg. object=001234 int=1234. I need the zeros. So, in the end, I'm keeping it an object (string) and just passing it into the function.
Here is my code after I got it working correctly.
for(ZBarSymbol *sym in syms) {
resultText.text = sym.data;
[self phpPost:sym.data];
break;
}
}
- (void)phpPost: (NSString *)barcode {
labelScan.text = barcode;
//labelScan.text = #"Barcode scanned"; //My custon label
}
Thanks, everyone for your responses. Your answer will not go unused. I'm sure I'll be needing this information here soon.
O, if you see that I did this wrong, or not the correct way, please make a comment and tell me .
I've started learning how to use blocks/functions/lambda in Objective-C. But I can't get it to work with Objects. Probably I'm missing some pointer, but it's not working however I do.
This is my code for the moment:
MyEventArgs (^skapaEventArg)(Operation);
skapaEventArg = ^(Operation a) { return *[[MyEventArgs alloc] initWithOperation:a]; };
MyEventArgs *a = skapaEventArg(Add);
But I get the error that this pic shows:
If I do
MyEventArgs a = skapaEventArg(Add);
to put it on the stack, Xcode gives me the usual "Interface cannot be statically allocated"-error
How do I get this simple code to work, using blocks?
It should be:
MyEventArgs *(^skapaEventArg)(Operation);
skapaEventArg = ^(Operation a) { return [[MyEventArgs alloc] initWithOperation:a]; };
MyEventArgs *a = skapaEventArg(Add);
Note the * in the first line, and the lack of it in the second line.
return *[[MyEventArgs alloc] initWithOperation:a];
I think you get "incompatible type" error because you return dereferenced pointer.