Re: Can I call Obj-C from a C++ source?
- From: Jens Ayton <VFNZRCIMVBRO@xxxxxxxxxxxxx>
- Date: Fri, 23 Mar 2007 16:21:16 GMT
Steve555:
My project is Objective-C++, but from one of the C++ classes I want to
write to the log window:
long val = 123;
NSString nsStr = [NSString stringWithFormat:@"%d", val];
NSLog(nsStr);
That's a very bad habit. The correct code is:
NSLog(@"%d", val);
In your case there's no practical difference, but if nsStr had a %@ and took a
string, it might end up with a % sequence in it, and then NSLog() would print
bad data or crash. If the string being substituted in was user input, you'd be
vulnerable to a format string attack.
That said, if you want to use NSLog from C or C++ code, and have a real reason
not to turn it into an Objective-C or Objective-C++ file, you can declare
NSLog as:
extern void NSLog(CFStringRef format, ...);
then call it as:
NSLog(CFSTR("%d"), val);
This won't port to other OpenStep implementations such as GNUstep.
--
Jens Ayton
.
- Follow-Ups:
- Re: Can I call Obj-C from a C++ source?
- From: Sean McBride
- Re: Can I call Obj-C from a C++ source?
- From: Steve555
- Re: Can I call Obj-C from a C++ source?
- References:
- Can I call Obj-C from a C++ source?
- From: Steve555
- Can I call Obj-C from a C++ source?
- Prev by Date: Re: Can I call Obj-C from a C++ source?
- Next by Date: Develop GUI
- Previous by thread: Re: Can I call Obj-C from a C++ source?
- Next by thread: Re: Can I call Obj-C from a C++ source?
- Index(es):
Relevant Pages
|