I can't figure out why I get
use of undeclared identifier _cmd did you mean rcmd
on the line where NSAssert is.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int x = 10;
NSAssert(x > 11, #"x should be greater than %d", x);
[pool drain];
return 0;
}
Inside every Objective-c method there are two hidden variables id self and SEL _cmd
so
- (void)foo:(id)bar;
is really
void foo(id self, SEL _cmd, id bar) { ... }
and when you call
[someObject foo:#"hello world"]
it is actually
foo( someObject, #selector(foo), #"hello world")
If you cmd-click on NSAssert to jump to it's definition you will see that it is a macro that uses the hidden _cmd variable of the method you are calling it from. This means that if you are not inside an Objective-c method (perhaps you are in 'main'), therefore you don't have a _cmd argument, you cannot use NSAssert.
Instead you can use the alternative NSCAssert.
NSAssert is only meant to be used within Objective-C methods. Since main is a C function, use NSCAssert instead.
Try to replace
NSAssert(x > 11, [NSString stringWithFormat:#"x should be greater than %d", x]);
with
NSCAssert(x > 11, [NSString stringWithFormat:#"x should be greater than %d", x]);
You have to wrap your string in a NSString class if you want to use format parameters. That is because #"" is a default constructor for a plain NSString. The way it is written now gives a third parameter to the NSAssert function and messes with it.
NSAssert(x > 11, [NSString stringWithFormat:#"x should be greater than %d", x]);
TL;DR - stick with stray NSAssert() - don't try this in production
Original code
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int x = 10;
NSAssert(x > 11, #"x should be greater than %d", x);
[pool drain];
return 0;
}
Build failure
Compiling file hello.m ...
hello.m:9:5: error: use of undeclared identifier '_cmd'
NSAssert(x > 11, #"x should be greater than %d", x);
^
/usr/include/Foundation/NSException.h:450:32: note: expanded from macro 'NSAssert'
handleFailureInMethod: _cmd \
^
hello.m:9:5: error: use of undeclared identifier 'self'
/usr/include/Foundation/NSException.h:451:17: note: expanded from macro 'NSAssert'
object: self \
^
2 errors generated.
Based on explanation by #hooleyhoop #Robert and
id
self
SEL,
the following dirty hack may be applicable if I insist on using
NSAssert() instead of
NSCAssert()
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int x = 10;
// Dirty hack
SEL _cmd=NULL;
NSObject *self=NULL;
NSAssert(x > 11, #"x should be greater than %d", x);
[pool drain];
return 0;
}
Build & run
Compiling file hello.m ...
Linking tool hello ...
2021-03-04 21:25:58.035 hello[39049:39049] hello.m:13 Assertion failed in (null)(instance), method (null). x should be greater than 10
./obj/hello: Uncaught exception NSInternalInconsistencyException, reason: hello.m:13 Assertion failed in (null)(instance), method (null). x should be greater than 10
Hooray it works! But, alas, please stay away from it :)
Related
The following code compiles and runs fine (note the sel_registerName("+")):
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
#interface Integer : NSObject
{
NSInteger intValue;
}
#property (assign) NSInteger intValue;
#end
#implementation Integer
#synthesize intValue;
- (id) plus:(Integer*)anInteger
{
Integer* outInt = [Integer new];
[outInt setIntValue: intValue + [anInteger intValue]];
return outInt;
}
#end
int main (int argc, char const *argv[])
{
id pool = [[NSAutoreleasePool alloc] init];
SEL plusSel = sel_registerName("+");
Method m = class_getInstanceMethod([Integer class], #selector(plus:));
class_addMethod([Integer class], plusSel, method_getImplementation(m), method_getTypeEncoding(m));
Integer* i4 = [Integer new];
Integer* i20 = [Integer new];
[i4 setIntValue: 4];
[i20 setIntValue: 20];
Integer* res = objc_msgSend(i4, plusSel, i20);
NSLog(#"%d + %d = %d", [i4 intValue], [i20 intValue], [res intValue]);
// >> 4 + 20 = 24
[pool drain];
return 0;
}
Other than "yuck", are there reasons to be cautious about doing this?
The API to the ObjC runtime is unlikely to change, but the validity of calling sel_registerName("+") might. I've monkeyed around in the ObjC runtime a lot, and haven't run into any problems even after many updates. That being said, I wouldn't base a multimillion dollar business on this continuing to work forever.
Currently, the Objective-C runtime library doesn't perform any checks on the content of the string you are trying to register and it's unlikely that the development team change that behavior. If it is a non-empty C string, if you always use objc_msgSend to send messages for that selector and if you don't try to do something like [i4 +:i20] (which is going to cause a compiling error), there is no reason to be afraid.
Registered Objective-C selectors are actually C strings stored internally by the runtime system. The runtime system keeps a table of pointers to C strings, the so-called SEL set. When you call sel_registerName the ObjC runtime system calls strcmp for your string and for each C string stored in the SEL set. If any of the C strings in the SEL set is equal to the one you want to register, the function returns the address of the corresponding C string in the set. Otherwise, the system duplicates your string (with strdup), stores the resulting pointer in the SEL set and returns it. This new pointer becomes a new unique selector.
I am new to Objective-C. I was trying out a sample program using macros and getting errors.
#import <Foundation/Foundation.h>
#define HELLO_WORLD #"Hello World"
#define a(x,y) ((x)+(y))
#define PRINTMAC(x,y)\
NSLog(#"%d",a((x),(y));\
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
//NSLog(#"%d",add(10,20));
PRINTMAC(13,72); //error:
[pool drain];
return 0;
} //error:
Error: expected ';' before '}' token
You appear to be missing a ) on the NSLog line (line 8).
Additionally, I'm not sure you need the final \ on that line, as the macro is not being carried on to a third line.
Finally, I don't think you need the ; on that line either as it, combined with the semi-colon when you invoke the macro on line 15 results in an empty statement (shouldn't be harmful, though).
I am following the instructions for a tutorial but I cannot figure out what is wrong. I have double checked everything. I put the the compiler errors in the code's comments below. Sorry, this will probably show how much of a noob I am.
// main.m
#import <Foundation/Foundation.h>
#import "LotteryEntry.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Creates the date object
NSCalendarDate *now = [[NSCalendarDate alloc]init];
//Seed the random number generator
srandom(time(NULL));
NSMutableArray * array;
array = [[NSMutableArray alloc]init];
int i;
for (i = 0; i < 10; i++) {
//create a date/time object that is 'i' weeks from now
NSCalendarDate *iWeeksFromNow;
iWeeksFromNow = [now dateByAddingYears:0
months:0
days:(i * 7)
hours:0
minutes:0
second:0];
}
//create the LotteryEntry object
LotteryEntry *newEntry = [[LotteryEntry alloc]init];
[newEntry prepareRandomNumbers];
[newEntry setEntryDate: iWeeksFromNow];
//Error says "Use of undeclared identifier "iWeeksFromNow'. Did I not declare it above?
//add the lottery entry object to the array
[array addObject:newEntry];
}
for (LotteryEntry *entryToPrint in array) {
//Error says " Expected identifier or '('
//Display it's contents
NSLog(#"%#", entryToPrint);
}
[pool drain];
return 0;
//Error says " Expected identifier or '('
}
//Error says " Expected External declaration
You are declaring iWeeksFromNow inside a for loop, that's why the compiler doesn't consider it to exist outside
declare it outside, and assign values to it inside
You have an extra closing } as you call the -dateByAddingYears method.
First error : you declare iWeeksFromNew inside a for loop, thus it's unreachable from outside.
You have to declare before the beginning of the loop.
Second error : you have a bracket '}' after [array addObject:newEntry]; so the compiler thinks its the end of your method, remove it.
That should fix all other error you have
First, iWeeksFromNow is declared within the scope of a for loop, so it will be visible only within that loop. Second, as pointed out by Black Frog, you have an extra closing parenthesis.
Move the declaration out that loop block. You've got a scope problem here: The iWeeksFromNew only exists within the loop
NSCalendarDate *iWeeksFromNow;
int i;
for (i = 0; i < 10; i++) {
//create a date/time object that is 'i' weeks from now
iWeeksFromNow = [now dateByAddingYears:0
months:0
days:(i * 7)
hours:0
minutes:0
second:0];
}
I have done some testing on some behavior I have found, and I was wondering if someone can help me understand what is going on.
I have a struct, called myStruct, that looks like this:
typedef struct {
int size;
float floats[];
} myStruct;
And I run this code on it:
int main () {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *a = [[NSArray alloc] initWithObjects:#"0.2", #"0.5", #"0.5", nil];
NSLog(#"%#", a);
myStruct my;
my.size = a.count;
my.floats[0] = [[a objectAtIndex:0] floatValue];
my.floats[1] = [[a objectAtIndex:1] floatValue];
my.floats[2] = [[a objectAtIndex:2] floatValue];
NSLog(#"{ %lf, %lf, %lf }", my.floats[0], my.floats[1], my.floats[2]);
[a release];
[pool drain];
return 0;
}
It works fine. However, when I change the struct declaration to this:
typedef struct {
float myVar;
int size;
float floats[];
} myStruct;
I get EXEC_BAD_ACCESS when I call the line [a release].
Can anyone help me understand what is going on here?
You have to actually allocate space for your flexible array member! This line:
myStruct my;
Only makes enough stack space for size (or myVar and size from your second example). It appears that in your failing case you're overwriting a on the stack, but really both cases are wrong. To fix your problem, you need to allocate space for the floats[] member of the structure:
myStruct *my = malloc(sizeof(myStruct) + a.count * sizeof(float));
Don't forget to free() it when you're done!
A quick example - this program:
#include <stdio.h>
typedef struct {
float myVar;
int size;
float floats[];
} myStruct;
int main(int argc, char **argv)
{
printf("%zu\n", sizeof(myStruct));
return 0;
}
and its output:
$ make testapp
cc testapp.c -o testapp
$ ./testapp
8
You're not allocating any memory for your floats - I'm surprised it's not crashing sooner!
Do you need to malloc some memory for the floats[] pointer?
After a quick test I get EXC_BAD_ACCESS for both definitions of myStruct :)
The following code compiles fine ...
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// VARIABLES
MDD *MDD_structure;
NSString *mddFile = #"/Users/Gary/Code/Mdd/xTest.mdd";
NSFileHandle *inFile;
NSData *inBuffer;
int MDD_fCount;
int MDD_vCount;
// OPEN FILE ON DISK
inFile = [NSFileHandle fileHandleForReadingAtPath:mddFile];
if(inFile == nil) NSLog(#"FILE: Open ... ERROR");
else NSLog(#"FILE: Open ... OK");
// READ FRAME COUNT
inBuffer = [inFile readDataOfLength:sizeof(int)];
[inBuffer getBytes:&MDD_fCount length:sizeof(int)];
MDD_fCount = CFSwapInt32BigToHost(MDD_fCount);
NSLog(#"FC: %d", MDD_fCount);
But when I run it through the static analyzer "CLANG LLVM 1.0" I get the following ...
warning: Pass-by-value argument in function call is undefined.
MDD_fCount = CFSwapInt32BigToHost(MDD_fCount);
^ ~~~~~~~~~~
1 diagnostic generated.
Can anyone tell me what I am missing?
gary
You're getting an error because clang isn't convinced that simply passing the address of your variable to a function is the same as giving it a value. You could probably initialize MDD_fCount to 0 to start with to get rid of the error.
It means that you haven't initialized MDD_fCount. See this blog post and this other question for additional info.