Expression Result Unused (with array description) - objective-c

Basically, I'm trying to print out an NSArray's description.
I'm getting 'Expression Result Unused', even though I've tried everything I could find online!
Does anyone know?
NSArray *WalletBalance= [responseDict objectForKey:#"balance"];
NSString *wBalance = (#"This wallet currently has %# dollars", [WalletBalance description]);
How could I append WalletBalance's description into a string?

You need to use:
NSString *someText = [NSString stringWithFormat:#"This wallet currently has %# dollars", [WalletBalance description]];
Courtesy of Append string with variable

Related

How to pass the dictionary output value to the url in objective C?

I am working on a proof of concept app that scans a barcode and returns the barcode information as a UPC value(i.e. 9780596001612). I am confused on how to pass that return value to the url(I read some tutorials online but those doesn't seem to have what I am looking for). I have hardcoded the UPC value in the code and I am getting the right response but I want to be able to pass the return value from the barcode scan and pass that value to the url. I recently started working on Objective C and would greatly appreciate your help. Thank you.
- (void) onQRCodeScanned:(NSString*) result {
NSString *theJSONString = [result description];
NSError *theError = NULL;
NSDictionary *theDictionary = [NSDictionary dictionaryWithJSONString:theJSONString error:&theError];
NSArray *results = [theDictionary objectForKey:#"decodedString"];
//results returns 9780596001612
NSString *barcodeUrl = #"http://www.outpan.com/api/get_product.php?barcode=9780596001612";
NSString *resp = [self makeRestAPICall: barcodeUrl];
}
You need to format the string to include the barcode like this (I'm assuming the barcode is the first result of the results variable - it doesn't make sense in it's current format - and that first result does indeed exist):
NSArray *results = [theDictionary objectForKey:#"decodedString"];
NSString *barcode = [results firstObject];
NSString *barcodeUrl = [NSString stringWithFormat:#"http://www.outpan.com/api/get_product.php?barcode=%#",barcode];
NSString *resp = [self makeRestAPICall: barcodeUrl];
Notice how I using stringWithFormat: and then use %# to represent a 'variable' as such, followed by the actual variable I want to include?
You can include primitive types using placeholders like %d (int), %c (char). You use %# to represent an object, such as a NSString.
You can do this for any number of variables, but you'll get an error if the number of placeholders don't match the number of variables!
You may like reading this: String Format Specifiers

getting NSArray length/count - Objective C

Well I've looked at similar problems over the site but haven't reached a solution thus far so I must be doing something wrong.
Essentially, I am importing a text file, then splitting each line into an element of an array. Since the text file will be updated etc.. I won't every know the exact amount of lines in the file and therefore how many elements in the array. I know in Java you can do .length() etc.. and supposedly in Objective C you can use 'count' but i'm having no luck returning the length of my array... suggestions?
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"allshows"
ofType:#"txt"];
NSString *fileString = [NSString stringWithContentsOfFile:filePath];
NSArray *lines = [fileString componentsSeparatedByString:#"\n"];
NSUInteger *elements = [lines count];
NSLog(#"Number of Shows : ", elements);
and what is being output is NOTHING. as in "Number of Shows : " - blank, like it didn't even count at all.
Thank you for any help!
You're missing the format string placeholder. It should be:
NSLog(#"Number of shows: %lu", elements);
You need to use a format specifier to print an integer (%d):
NSLog(#"Number of Shows : %d", elements);
Looking at your other post, it seems like you are a Java developer. In Java's System.out, you just append the variables. In Objective-C, I suggest you look at "print format specifiers". Objective-C uses the same format.

stringByAppendingFormat not working

I have an NSString and fail to apply the following statement:
NSString *myString = #"some text";
[myString stringByAppendingFormat:#"some text = %d", 3];
no log or error, the string just doesn't get changed. I already tried with NSString (as documented) and NSMutableString.
any clues most welcome.
I would suggest correcting to (documentation):
NSString *myString = #"some text";
myString = [myString stringByAppendingFormat:#" = %d", 3];
From the docs:
Returns a string made by appending to the receiver a string constructed from a given format string and the following arguments.
It's working, you're just ignoring the return value, which is the string with the appended format. (See the docs.) You can't modify an NSString — to modify an NSMutableString, use -appendFormat: instead.
Of course, in your toy example, you could shorten it to this:
NSString *myString = [NSString stringWithFormat:#"some text = %d", 3];
However, it's likely that you need to append a format string to an existing string created elsewhere. In that case, and particularly if you're appending multiple parts, it's good to think about and balance the pros and cons of using a mutable string or several immutable, autoreleased strings.
Creating strings with #"" always results in immutable strings. If you want to create a new NSMutableString do it as following.
NSMutableString *myString = [NSMutableString stringWithString:#"some text"];
[myString appendFormat:#"some text = %d", 3];
I had a similar warning message while appending a localized string. This is how I resolved it
NSString *msgBody = [msgBody stringByAppendingFormat:#"%#",NSLocalizedString(#"LOCALSTRINGMSG",#"Message Body")];

simple question concerning NSString adding multiple strings

I have a fairly simple question concerning NSString however it doesn't seem to do what I want.
this is what i have
NSString *title = [NSString stringWithformat: character.name, #"is the character"];
This is a line in my parser takes the charactername and inserts in into a plist , however it doesn't insert the #"is the character" is there something I'm doing wrong?
Your code is wrong. It should be :
NSString *title
= [NSString stringWithformat:#"%# is the character", character.name];
assuming that character.name is another NSString.
Read the Formatting String Objects paragraph of the String Programming Guide for Cocoa to learn everything about formatting strings.
stringWithFormat takes a format string as the first argument so, assuming character.name is the name of your character, you need:
NSString *title = [NSString stringWithformat: #"%s is the character",
character.name];
What you have is the character name as the format string so, if it's #"Bob" then Bob is what you'll get. If it was "#Bob %s", that would work but would probably stuff up somewhere else that you display just the character name :-)
Note that you should use "%s" for a C string, I think "%#" is the correct format specifier if character.name is an NSString itself.

Need formatting help for isEqualToString args

Code sample:
NSString *title = [[NSString alloc]initWithFormat: #"%#", [self.answers valueForKey:idVor]];
NSString *message = [[NSString alloc]initWithFormat: #"%#",nameVor];
NSLog(#"%#", title);
NSLog(#"%#", message);
if([title isEqualToString:message])
NSLog(#"equal");
The vars title and message never respond to the if statement even though they contain the same string.
I ran NSLogs for these to see what was contained in each string var.
I got the following output:
f[Session started at 2009-09-21 17:27:56 -0500.]
2009-09-21 17:28:00.256 pickerReview[2394:20b] (
Amedee
)
2009-09-21 17:28:00.257 pickerReview[2394:20b] Amedee
I guess it's not equal because the NSString title var has parentheses around it... Is there a way to format this so that it satisfies the expression in the if statement?
The issue appears to be that you're asking self.answers (an NSArray) for its valueForKey:#"whatever" — this doesn't return a string, but an array made up of the result of asking each object in the array for that key value. NSArray's description method (what gets printed when you NSLog it) is the contents of the array surrounded by parentheses. So you're comparing a string to an array containing a string.