Why is this NSLocalizedString not being Reverse for Hebrew - ios

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.

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.

replace a object in NSMutableArray

I try to replace an object from my Array with a string. I use
#property (strong, nonatomic) NSMutableArray *myArray;
NSString *oldString = [NSString stringWithFormat:#"oldObject"];
NSString *toReplace = [NSString stringWithFormat:#"newObject"];
NSUInteger index = [self.myArray indexOfObject:oldString];
[self.firstLanguageArray replaceObjectAtIndex:index withObject:toReplace];
But every time I try to replace, the app will crash.
Edit: I logged the "index". I will become a Integer like 2147483647.
Probably because your call to indexOfObject returns NSNotFound.
NSNotFound is a constant that tells you that the oldString object is not found in your self.myArray dictionary, and as this constant has a value of (NSUInteger)-1 (which is equivalent to the unsigned value 2147483647 because of integer underflow), value that will obviously crash your app with an "Out of Bounds" exception.
The solution is to test whether the index != NSNotFound before using it. Or to make sure that your array self.myArray actually contains an object of type NSString whose value is "oldObject".
If, given your actual code, you expected oldObject to be present in your self.myArray, then think again, maybe log the content of self.myArray to lookup what it actually contains, and also check that this content is a string.
(if you see "oldObject" when logging the content of your myArray, that's probably just the description string of the object in your array, so the array does not contain the string "oldObject" itself, but an object whose description is "oldObject")
Side note: there is no need to use stringWithFormat if you don't use any format placeholder (like %d or %# etc). You should simply directly use the string constant in that case, no need to dynamically build a string if that string is a constant: simply use NSString* oldString = #"oldObject" and NSString* toReplace = #"newObject".

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

Multi-line string formatting issue in UILabel

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];

Resources