Multi-line string formatting issue in UILabel - ios

I want my label to read like this:
Name of Activity
nn%
Instead, here's what appears:
%#
%f%
In addition, I'm getting this warning: Expression result unused
Here's the code I'm trying:
firstLabel.text = #"%#\n%#%",[self.thisSpec activityOfInterest],focusActivityPercent;
[self.thisSpec activityOfInterest] returns a string containing the name of an activity, and focusActivityPercent is a double.
This is the first time I've tried a multiline label.
Any suggestions?
Thanks

You can't specify string formatting on a string literal on its own like that. In fact, the code you've shown should be producing a syntax error. You have to use NSString's stringWithFormat: class method:
firstLabel.text = [NSString stringWithFormat:#"%#\n%#%",[self.thisSpec activityOfInterest],focusActivityPercent];

Related

UILabel won't display specific String variable in Swift

I have an array containing string type values, and one value of a string contains the symbol & and another the symbol ^. So when it's time for them to be shown the UILabel remains blank.
let myString = arrayStrings[0] // The value is "M&M" or "(0C)^3"
myLabel.text = myString //UILabel remains blank
On the other hand, when I hardcode the string, the UILabel displays it.
myLabel.text = "M&M" //UILabel displays it normally
What can I do?
Just realised that when I print the Array I have the following result:
print("Array: \(arrayStrings)" // Array: ["\0M&M\0", "\0(0C)^3\0"]
"\0" doesn't exist to the rest Strings of the array
Thank you!
\0 means string termination in programming. So your String "\0M&M\0" means that this string is terminated on index 0, and then again later. So when you assign this string to your label, your label is displaying empty string.
To tweak it, do this and you will see the difference. Your problem is not related to & or ^
myLabel.text = "M&M\0 Hey I have lots of stuff here but the string is already terminated"
So in this situation, you need to find out why \0 exists in your string. You can possibly remove them by regex or string replace.

Property access results unused but NSLog works fine

I'm trying to display results in a text label, but I get different result than NSLog:
if( [elementName isEqualToString:#"CommunityID"])
{
self.recordResults = FALSE;
ResultLabel.text = #"CommunityID: %#", self.soapResults;
NSLog(#"CommunityID:%#",self.soapResults);
self.soapResults = nil;
}
NSlog correctly shows the text result, but the UILabel doesn't. The error shows:
"Property access result unused - getters should not be used for side effects"
I don't understand how NSlog gets the info just fine but the other doesn't? Any ideas?
You can't assign directly, You need to do with stringWithFormat property
ResultLabel.text = [NSString stringWithFormat:#"CommunityID: %#", self.soapResults];
Just so you know what was happening and what the compiler warning was about…
In C and Objective-C, a comma (,) that's not separating arguments in a function or method call is the comma operator. It is a compound expression. The left-hand sub-expression is evaluated but its result is discarded. Then, the right-hand sub-expression is evaluated and the overall compound expression takes its value.
In your case, the compound expression was just used as a statement and its result was not used. So, your statement:
ResultLabel.text = #"CommunityID: %#", self.soapResults;
was the equivalent of:
ResultLabel.text = #"CommunityID: %#";
self.soapResults;
The second of those statements calls a property getter and discards the resulting value. The compiler is warning you. Either you didn't mean to do that (as in this case) or you were invoking the getter because the getter has side effects that you wanted, which is a really bad idea.

Displaying two different types of variables in one label string

I am trying to make a CCLabelTTF display a string and an integer together. Like this:
Your score is 0.
I've tried a few things but I usually get the warning Data argument not used by format string, and the label doesn't output the correct statements.
I am just trying to figure out the format in which to put these in and searching Google hasn't provided much, as I'm not really sure what exactly to search.
I've tried
label.string = (#"%#", #"hi", #"%d", investmentsPurchased);
but obviously that isn't correct. How would I do this?
Thanks.
(I assume this is ObjC and not Swift.) Try something like this:
label.string = [NSString stringWithFormat:#"hi %d", investmentsPurchased];
You use a single format string, which contains static text and replacement tokens (like %d) for any replacement variables. Then follows the list of values to substitute in. You can use multiple variables like:
label.string = [NSString stringWithFormat:#"number %d and a string %#", someInteger, someString];
use NSString newString = [NSString stringWithFormat:#"hello %#", investmentsPurchased];
in short: use stringWithFormat

Why is this NSLocalizedString not being Reverse for Hebrew

I have the following statement :
NSString* mess = [NSString stringWithFormat:NSLocalizedString(#"this_statement", nil),
value,
friendName];
the localized statement in Hebrew is :
"this_statement" = "אמריקה <b>%#</b> אל %#‎‏.";
In English :
"this_statement" = "America <b>%#</b> to %#.";
Now, when i actually do instantiate mess and run it, the value returned is printed and reversed, but the stuff I passed i.e. value and friendName are not reversed as Hebrew should be. Can someone help me out with this.
NSLocalizedString is executed first an so "this_statement" is looked up first and then the result has the value and friendName inserted into it. If you want localisation for 'value' and 'friendName' to occur you will have to wrap them in NSLocalizedString also. For example
NSString* mess = [NSString stringWithFormat:NSLocalizedString(#"this_statement", nil),
NSLocalizedString(value, nil),
NSLocalizedString(friendName, nil)];
You will have to supply localised values for what every value and friendName can be.

Method To Delete 2nd Label, Then First Label, Not Functioning Correctly

I know I am missing something obvious, but I just cannot see it. This method is meant to compare the text of a label to the text of a text box, and then delete the text. So if the 1st label reads "Puppy" and the 2nd label reads "Kittens," and the text box says "Kittens," the method should delete the text of the 2nd label and leave the 1st label's text. If the 2nd label is blank, then the method should delete the text of the 1st label.
But no matter how I mess with the method, either it deletes the 2nd label but not the 1st, deletes both of them, or deletes neither of them. Here's what I've tried
(lblFabric1 is the 1st label, lblFabric2 is the 2nd label, txtType is the text box):
-(IBAction)btnDelete:(id)sender
{
if ((self.lblFabric2.text== self.txtType.text))
{
self.lblFabric2.text = #"";
}
else if ((self.lblFabric2.text != self.txtType.text))
{
self.lblFabric1.text=#"";
}
}
It deletes the 2nd label, but not the 1st label. If I try to set the "Else if" to:
else if ((self.lblFabric2.text==#""))
it gives me an error (""Direct comparison of a string literal has undefined behavior.") Am I just going about this the wrong way? What am I missing?
You should not use == or != for string comparison in Objective C. You need to use the isEqualToString or isEqual method.
if (([self.lblFabric2.text isEqualToString:self.txtType.text]))
When you use == or != you are comparing the pointers where the strings are stored.
To compare NSStrings use:
if ([myString1 isEqualToString:myString2])
Documentation
Compairing String literals using == is not guaranteed to behave as you might expect. Use isEqual: or isEqualToString: instead.
See http://nshipster.com/equality/.
When you are comparing NSStrings with == what you are actually comparing are two memory addresses and that is not what you are really intended for. You want to compare the values of two strings what == operator is not suitable for and thus you are getting the warning
Direct comparison of a string literal has undefined behavior.
To compare the values of NSStrings you should use isEqualToString: method. You could have also use isEqual: method derived from NSObject class but that is not suitable for Unicode comparison. So isEqualToString: is always the safest bet.
After using isEqualToString: your code should look something like:
-(IBAction)btnDelete:(id)sender
{
if ([self.lblFabric2.text isEqualToString:self.txtType.text])
{
self.lblFabric2.text = #"";
}
else
{
self.lblFabric1.text=#"";
}
}

Resources