I'm trying to stringize an Objective C string at runtime.
Is it possible to use the stringizing operator to do this? I realize it's a preprocessed macro, but could I combine it with a function that returned a char*?
This is what I'd like to do is..
#define strthis(x) #x
char *itsstringized = strthis([#"my string" UTF8String]);
printf("%s", itsstringized);
Result:
"my string"
not
my string
Do you want something like this?
Pass a string to a method and it will return the string enclosed in a quotes.
-(NSString *)stringizedString(NSString *)str{
return [NSString stringWithFormat:#"\"%#\"", str];
}
You sure can! Try this macro out:
#define QUOTEIFY(input) #("\"" #input "\"")
#define QUOTEIFY_STRING(input) #("\"" input "\"")
Usage:
NSLog(QUOTEIFY(Hello World!)); // notice I didn't quote what's inside the brackets.
// or, if you prefer:
NSLog(QUOTEIFY_STRING("Hello, World!"));
Related
I want to create macro that receives 2 arguments: NSString and ObjCBool and returns NSString.
I'm not familiar a lot with macros, anyway this is what I did so far:
#define fooOne(url)\
#"1111111" url
#define fooTwo(url)\
#"2222222" url
#define root(url, flag)\
if(!flag)fooOne(url)\
else fooTwo(url)
Here I have 2 problems:
1) when I call fooOne as:
NSString *url = #"zzz";
NSLog(#"%#", fooOne(url));
// expected log should be: "1111111 zzz"
I get error: Expected ')'
2) When I call root(url, flag) as:
BOOL flag = YES;
NSString *url = #"zzz";
NSLog(#"%#", root(url, flag));
// expected log should be: "2222222 zzz"
I get an error: Expected expression
please help,
Compile-time string concatenation only works with string literals. At compile time #"hello, " #"world" is combined into a new string literal #"hello, world". This doesn't work with strings contained in variables — #"blabla" url is not a valid expression, even if url contains a string literal at runtime. You would have to call your macros with the literal inside: fooOne(#"zzz"), which would expand to #"blabla" #"zzz".
If you want to combine strings at runtime, you'll need to use +[NSString stringWithFormat:], or append the strings.
I have a problem to create a preprocessor macro function, that concatenates two Strings and "return" a NSString (#"...") value.
Here is what I tried:
#define ObjectKeyMake(NAME) #"com.test.##NAME"
if I print the result from a call I get:
NSLog(#"%#", ObjectKeyMake(foo)); // com.test.##NAME
so my question is: How can i concatenate 2 Strings in a preprocessor macro and "return" a NSString (#"..") ?
and no I can't use #define ObjectKeyMake(NAME) [#"com.test." stringByAppendingString: NAME] because i need a compile-time constant.
You can take advantage of the fact that the compiler combines string literals that are next to each other, like this:
NSString* greeting = #"Hello, " "world";
The macro implementation would look like this:
#define ObjectKeyMake(NAME) (#"com.test." #NAME)
#define ObjectKeyMake(NAME) #"com.test."#NAME
I want a input from user their name and output that input name in NSLog using NSString.
I don't know which % sign and how to output that.
Can i use scanf() function for that?
Please help me , i am just beginner of Objective-C.
You can use %# for all objects including NSString. This will in turn call the objects description method and print the appropriate string. Most objects have a rather useful representation already there (e.g. NSArray objects return the descriptions of all their contents).
Mark Dylan is the name which would be stored in the Name variable.
NSString* Name = #"Mark Dylan";
This code will allow you to ask their name and scan it into memory which will be stored in the Name variable.
NSLog(#"What is your name?");
scanf("%#", &Name);
If you want to print out the variable you can use;
NSLog(#"Your name is %#", Name);
%# is what you want. It fit for object like NSString, [YourViewController class]
To get input from the user use a UITextField or a NSTextField. To output a string to the log file you can use NSLog, ie:
NSString* userName = #"Zawmin";
NSLog(#"name = %#", userName);
NSLog accepts a format string, so you can do something like this:
#include <stdio.h>
#include <Foundation/Foundation.h>
// 1024 characters should be enough for a name.
// If you want something more flexible, you can use GNU readline:
// <http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html>
#define MAX_NAME_LENGTH 1024
// Get name from user input
char name[MAX_NAME_LENGTH];
name[0] = '\0'; // just in case fgets fails
fgets(name, MAX_NAME_LENGTH, stdin);
// Put name into NSString object and output it.
NSString *name = [NSString stringWithUTF8String:name];
NSLog(#"%#", name);
%# works for all Objective-C objects.
If you want to output a C-string (char* or const char*), use %s. Never put a non-literal string as the first argument to NSLog as this opens security holes.
How to get input from an NSString as scanf ("%#", &str); doesn't work?
scanf will read into a C string and not into a NSString (as far as I know). So, to do what you're trying to do you need to first read your input into a C string (i.e. str) and then make that into an NString as follows
myString = [NSString stringWithUTF8String:str];
By the way, you don't need to pass the address of str i.e. &str if str is an array. Simply do:
scanf("%s",str);
Silly as it may sound, I am trying to write a simple function in objective-c which returns a string and displays it, the following code nearly works but I can't get printf to accept the functions return value ...
NSString* getXMLElementFromString();
int main(int argc, char *argv[])
{
printf(getXMLElementFromString());
return NSApplicationMain(argc, (const char **) argv);
}
NSString* getXMLElementFromString() {
NSString* returnValue;
returnValue = #"Hello!";
return returnValue;
}
NSString* is not equivalent to a traditional C string, which is what printf would expect. To use printf in such a way you'll need to leverage an NSString API to get a null-terminated string out of it:
printf("%s", [getXMLElementFromString() UTF8String]);
You should instead use NSLog() which takes a string (or a format string) as a parameter.
You could use either
NSLog(getXMLElementFromString());
or
NSLog(#"The string: %#", getXMLElementFromString());
Where the %# token specifies an Objective-C object (in this case an NSString). NSLog() works essentially the same as printf() when it comes to format strings, only it will also accept the object token.
I don't know that printf can handle an NSString. Try somethign like:
printf ("%s\n", [getXMLElementFromString()cString]);