Prevent NSTextField from inserting commas when setting intValue - objective-c

In my app I have an NSTextField into which the user can type a value for "Year". This works fine and I save the value off to a file as an integer. However, when I read this value back and place it back into the text field for editing, like so:
YearTextField.intValue = dataFromFile.year;
The year is comma separated, so an original value of 2020 appears as: 2,020
I know I can stringWithFormat the value into an NSString and then set the stringValue of the NSTextField, but is there a more straightforward way to simply tell YearTextField to not insert commas when setting the value?
Thanks!

Related

NSTextField: integerValue behaving inconsistently

I have found that [NSTextField integerValue] behaves differently with values containing thousands separators depending on how the value was set.
(I am in Germany, so my thousands separator in these examples is a point ".").
If I call [myTextField setIntegerValue:4567], the text field will contain 4.567 (with tousands separator), and [myTextField integerValue] returns 4567.
If I type the value 4.567 info the text field manually, or use [myTextField setStringValue:#"4.567"], then [myTextField integerValue] returns just 4.
Apparently it stops parsing the number at the thousands separator, even though it inserts such a separator itself when calling setIntegerValue.
So I have actually two questions:
Is there a setting or other easy way that I can prevent it to format the number when using -setIntegerValue: ?
Can I "enable" the number parsing to understand/accept thousands separators when calling -integerValue? Or, if not, what would be the simplest way to parse a number with thousands separator from an NSString?
Add a Number Formatter (NSNumberFormatter) to the text field in the storyboard or XIB. This makes the contents of the cell and objectValue of the text field a NSNumber instead of a NSString.
Is there a setting or other easy way that I can prevent it to format the number when using -setIntegerValue: ?
Switch off Grouping Separator of the formatter.
Can I "enable" the number parsing to understand/accept thousands separators when calling -integerValue?
Switch on Grouping Separator of the formatter and set Primary Grouping to 3.
Or, if not, what would be the simplest way to parse a number with thousands separator from an NSString?
Use a NSNumberFormatter, see the class reference and Number Formatters.
integerValue is not locale-aware, it will always use . as the decimal point.
You should use NSNumberFormatter.numberFromString: instead.
Link to NSNumberFormatter class reference.

The textfield displayed a value which is the calculate result from an action, how to bind the value to core data

I'm working on mac app project. I have a table and a textfield, one column named price, and the textfield names price as well. I binding the column price to a controller with keypath arrangedObject, for the textfield, I bind to the same controller, with keypath selection. It worked. It can read the data from user input to textfield.
However now, I need to calculate the number then display the number to the textfield, and then bind the value. so that the column can also display the value. How to do that?
If I understand you correctly, you are already successfully doing the bind part. So what you need to do is set a calculated value in the text field - which, due to the binding, will then also be shown in the appropriate cell in the table.
To set a value in a text field, you create a new outlet (yourTextFieldName) in the controller class by control-click dragging to a blank line in the controller class .h file - but I'm guessing you are far enough along to know that. Then to set the value use
[yourTextFieldName aString];
Presuming that your data is numeric (as you refer to computation) you may have to convert it to a string first, possibly with
[[NSString alloc] initWithFormat:#"%#", numericValue ]; // with NSNumber *numericValue or %g if it is a float, or...

NSString and NSMutableString concatenation

I have three strings (a NSString, a NSMutableString, and another NSString) which I need to concatenate into a mutable string, in that order, to display as the source for a UIWebView. Comming from a PHP/JavaScript/HTML background, my knowledge of concatenation is pretty much this:
var concatenatedString = string1 + string2 + string3;
I presume that sort of thing won't work in Objective-C, so I'm wondering how to go about pulling them all together properly.
To give a bit of setting for this, the first string (NSString) is the header and canvas element of a web page, the second string (NSMutableString) is javascript from a text field that the user can define to manipulate the canvas element, and the third string (NSString) is the end tags of the web page.
Also, rather than initially creating the NSMutableString, should I just referance the UITextView.text to the get the user's text when concatenating the whole thing, or should I pull the text from the UITextView first?
NSMutableString *concatenatedString = [[NSString stringWithFormat:#"%#%#%#", string1, string2, string3] mutableCopy];
The other two answers are correct in that they answer the question as you asked it. But by your description of what you want to do there is a much easier way. Use a format.
Assuming string1 and string3 will always be the same and only string2 will change,which is what it sounds like you are doing you can write something like this.
static NSString *formatString = #"String1Text%#String3Text";
NSString *completeString = [NSString stringWithFormat:formatString,self.myTextFieldName.text];
NSLog(#"%#",completeString);
The %# in the format says to insert the description of the object following the format.(The description of an NSString is the stringValue.)
Assuming you have a UITextField named myTextFieldName, that currently contains the text 'String2Text' Then this will be the output:
'String1TextString2TextString3Text'
In this way you only create 1 instance of an NSString format for the whole class no matter how many times you call this code.
To me it sounds like you don't need a mutable string at all. Feel free to leave a comment if I misunderstood anything.
Response to comment:
I'm not sure how you are implementing 'moves to test it out again' but, let's say you have a button named 'testJavaScript'. The IBAction method connected to that button would have the first two lines in it. So each time you pushed the button it would make a new formatted NSString filled with the current contents of the textfield. Once this string was formed it could not be changed. But it won't matter since next time it will make another.
NSString *concatenatedString = [string1 stringByAppendingFormat:#"%#%#", string2, string3];
You can make the resulting string mutable (if you really need to) by adding mutableCopy as shown in the answer by #Vinnie.

Making an NSTextField act like a "microwave timer", store as NSNumber

What I'm trying to accomplish is two fold:
I want my NSTextField to act like a "microwave timer" and update as characters are entered. So the field would default to "00:00" (minutes, seconds). When a 1 is pressed, the field would look like "00:01". Then a 4 is pressed and "00:14". A 2 is pressed and "01:42".
I want to store this as an NSNumber on the controller as the number of seconds in the timer. So if the text field shows "10:00", then the value on the property would be 600.
Seems like NSFormatter is a good place to be looking, but my attempts have been full of fail. Any help is welcome. Thanks!
a) Divide the value of your timer property into minutes and seconds. Then use an NSFormatter setup with two leading zeros to create strings from the two numbers (see: How to specify decimal places when formatting NSNumber objects?). Then use stringWithFormat: to put the two strings on either side of a colon.
b) Or just divide out each digit of the four (in your example) numbers and put that number in a string with the colon in the correct spot. (so first the tens of minutes, then the minutes, then the tens of seconds, then the seconds)
Either of the above could be in a custom NSFormatter subclass set on the NSTextField or in a method in the view's controller called by an observer (using KVO) of your timer property.

UILabel and right to left text

I have a collection of NSString objects that contain arabic text. However, when I try to display any of them using a UILabel, the text shows left-to-right instead of right-to-left (NSLog shows the strings properly)
I am thinking about a work-around, applying a transform to the UILabel to make a y-axis symmetry, but how can I detect if a NSString contains a RTL string?
Try prepending the unicode character 0x200F to the beginning of each string. This character is an invisible marker character that indicates text directionality.
Have you tried setting your region to Arabic? I would have thought this type of thing would be handled automatically.