Enumeration in the dialog box - objective-c

My tableViewController with a list of items of various types will provide a button to show a modal dialog box. This dialog box (similar to alert view) will provide the user with an exclusive choice from a list of 6 options.
Based on what the user chooses and confirms, the list in the main tableview controller screen will be filtered down to only show items that match the selected type.
At the moment, I have those six types listed in a typedefed enum. So far so good.
However I also need to be able to populate my custom dialog box with six nsstrings whose names will match the types used in the enumeration.
How to reconciliate this enum with my requirement for a source of those strings, but in such a way that I would ensure some level of consistency between the two? I do not want to hardcode anything.

You need a helper method that returns a string for each enum value. This should be written to deal with possible localization. All of your data and event handling should be based on the enum value. The string should be used for display.
The helper method should take an enum value and use a switch statement to return the proper string.

I can think of a few:
Change the enum to a bunch of strings. This makes things a bit tedious if they need to be integers too (-[NSArray indexOfObject:]).
Make a C array of strings. This lets you use C99's handy syntax:
NSString * const names[] = {
[Foo] = #"Foo",
[Bar] = #"Bar",
};
Autogenerated code to do the above.
Caveats:
Both of these will make i18n rather painful. This might not be relevant if it's contract work that will only need to be in one language, but it's Bad Practice.
Using button indexes as keys works until you decide you need to remove buttons in the middle. String keys work much better in the general case (I wrote a UIAlertView/UIActionSheet wrapper that accept (key,title) pairs and returned the key instead of the button index).

I take your remark that you "do not want to hardcode anything" to mean that you don't want any string constants in your code. So:
You could simply assign the strings to your sheet's UI elements (perhaps check boxes, for example) and give those UI elements tag values that match your enumeration (something you could query as your sheet closes). This has the additional benefit that you can easily localize the sheet.
Or:
If you want to keep the strings separate from your sheet, you could create a .strings file (perhaps you could call it Enumeration.strings or some such) formatted something like this:
"001" = "string one";
"002" = "string two";
.
.
"010" = "string ten";
and you could then retrieve the strings using your enumeration values like this:
NSString *myString = NSLocalizedStringFromTable([NSString stringWithFormat:#"%03d", myEnumerationValue], #"Enumeration", #"");
but then you'd have to have a way of plugging the strings into your UI, keeping track of UI elements through IBOutlets. Note that I used three decimal places here; perhaps you could get by with two, or even one. Finally, you get the ability to localize as in the first suggestion.

Related

Building a (process) variable in Appian using the value of another one?

As far as I understand, it is not possible in Appian to dynamically construct (process) variable names, just like you would do e.g. with bash using backticks like MY_OBJECT=pv!MY_CONS_`extract(valueOfPulldown)`. Is that correct? Is there a workaround?
I have set of Appian constants, let's call them MY_CONS_FOO, MY_CONS_BAR, MY_CONS_LALA, all of which are e.g. refering to an Appian data store entity. I would like to write an Appian expression rule which populates another variable MY_OBJECT of the same type (here: data store entity), depending e.g. of the options of a pull-down menu having the possible options stored in an array MY_CONS_OPTIONS looking as follows
FOO
BAR
LALA
I could of course build a lengthy case-structure which I have to maintain in addition to MY_CONS_OPTIONS, so I am searching for a more dynanmic approach using the extract() function depending on valueOfPulldown as the chosen value of the pulldown-menu.
Edit: Here the expression-rule (in pseudo-code) I want to avoid:
if (valueOfPulldown = 'FOO') then MY_OBJECT=pv!MY_CONS_FOO
if (valueOfPulldown = 'BAR') then MY_OBJECT=pv!MY_CONS_BAR
if (valueOfPulldown = 'LALA') then MY_OBJECT=pv!MY_CONS_LALA
The goal is to be able to change the data store entity via pulldown-menu.
This can help you find what is behind your constant.
fn!typeName(fn!typeOf(cons!YOUR_CONSTANT)).
Having in mind additional details I would do as follows:
Create separate expression that will combine details into list of Dictionary like below:
Expression results (er):
{
{dd_label: "label1", dd_value: 1, cons: "cons!YOUR_CONSTANT1" }
,{dd_label: "label2", dd_value: 2, cons: "cons!YOUR_CONSTANT2" }
}
on UI for your dropdown control use er.dd_label as choiceLabels and er.dd_value as choiceValues
when user selects value on Dropdown save dropdown value to some local variable and then use it to find your const by doing:
property( index(er, wherecontains(local!dropdownselectedvalue, tointeger(er.dd_value))), "cons")
returned value of step 3 is your constant
This might not be perfect as you still have to maintain your dictionary but you can avoid long if...else statements.
As a alternative have a look on Decisions Tables in Appian https://docs.appian.com/suite/help/21.1/Appian_Decisions.html

wxWidgets - wxGrid - reading/writing non string cell values

I have a wxGrid to edit an array of numerical data.
I was wondering what's the best way to get non-string data in and out of the cells without going through the string to numeric conversion all the time.
I've used SetCellEditor() to control the data entry.
currently I use this:
// numeric value into cell
str.clear();
str << val1;
m_grid4->SetCellValue(row, col, str);
..
// read value from back into variable
val = atoi(m_grid4->GetCellValue(row, col));
Apart from the fact that atoi() is a bit ugly and a template function with a stringstream would be better, is there a way do get non-string values a bit better in and out of cells?
I was looking at the editors and renderers but can't figure it out.
If you worry about efficiency, you almost certainly should use a custom table class deriving from wxGridTableBase instead of using the default trivial wxGridStringTable implementation which stores everything as strings. Then, and much less importantly, if it makes sense in your case, you can use wxGridCellNumberRenderer which will call your table GetValueAsLong() method instead of GetValue() (which returns a string).
Both of those are demonstrated in wxGrid sample, notably look at BugsGridTable there.
Good luck!

Add bold formatting to localized string that has placeholders

I have a localization string with a placeholder:
Verb {0}
I use this string in my view-model to return a string to my view that, in turn, is displayed in a TextBlock. Easy enough. But a new requirement has arisen saying that the "Verb" portion (everything other than the placeholder's inserted value) be displayed in bold.
Using a string with placeholders seems like the typical and easiest way to indicate word order. So the first question, then, is: where should I parse the localization string in order to add the bold formatting? The parse operation will need knowledge of the original placeholder's location. So far, the view-model has been responsible for utilizing the localization strings by using string.Format to insert values and return its result to the view. If I leave this responsibility in the view-model, as is probably necessary, then the view-model also needs to return rich text.
Is binding to rich text even supported by RichTextBlock? Even if it is supported, I've never before had a view-model return formatted text before. It initially feels sacrilegious to a follower of MVVM-ism, but perhaps upon further consideration I may find it acceptable.
What's the best way to add bold formatting to a localized string that has placeholders? Is returning rich text from the view-model the best way?

Difference between identical and equal?

I am having trouble understanding the concept between two things being equal and two things being identical. What confuses me is the statement "Two objects can be identical, which means they are equal. But, two object that are equal are not identical." Could someone please help me understand the difference? Thanks.
in objective-c, slightly pseudo code...
NSString * a = #"hello";
NSString * b = #"hello";
a == a //they are identical (the same object)
[a isEqualToString:a]; //they are equal.
a != b //Even though the string contents are the same they are not the same object.
[a isEqualToString:b]; // they aren't identical but they are equal.
For a very long and good blog post on the topic see this
http://www.karlkraft.com/index.php/2008/01/07/equality-vs-identity/
Welcome to the world of boxes... (this works a lot better with pictures, apologies I haven't included any)
Preamble
When you write:
int x;
you are asking for a box which is capable of holding an int and to equate the name of that with x - all boxes have an internal name, what it exactly is is unimportant. So now when you write:
x = 4;
you asking to store the value 4 in the box referenced by the name x. A box is often called a "variable". This use of boxes is why computer programming is different from mathematics, you can write:
x = x + 1;
in computer programming, but not in maths! It means "go to the box referenced by x, copy the value out, add 1 it to, put the value back into the same box".
Now boxes of different types (meaning what can be stored in them) can be "glued" together to form multi-compartment boxes. The whole collection is referred to by a single name, and the individual boxes by two names - the one for the collection and the one for the individual box. Such collections of boxes appear in different programming languages under the terms "record", "structure", "object" etc.
And finally, what can you put in a box? The answer (depending on the rules of the programming language) is anything and this includes the names of other boxes. E.g. you can write:
int *y;
which asks for a box capable of holding the "name of a box which holds an integer" and to call this box y. Such boxes are often called "pointer variables" or "reference variables"
== vs. isEqual
In Objective-C the == operator looks at the contents of two boxes and determines whether they contain the same value - and that value might well be the name of another box.
The isEqual method operates only on boxes (of certain restricted types) which contain the names of other boxes. It looks into the two boxes, uses the names in those boxes to locate two more boxes, and then compares the contents of those boxes. If the boxes it is comparing themselves contain other boxes which contain names of yet further boxes then it traverses to those further boxes to compare them, etc.
Furthermore isEqual does not need to compare for exact equality, but is allowed to compare for "means the same thing". E.g. if you create two dictionaries which contain the same key/value pairs but they are entered in different orders then the arrangement of boxes that make up those two dictionaries may not be identical, but semantically they are the same - the isEqualDictionary: method is defined as:
Two dictionaries have equal contents if they each hold the same number of entries and, for a given key, the corresponding value objects in each dictionary satisfy the isEqual: test.
Conclusion
The == operator is usually only used for "primitive" data types - int, float, NSInteger etc. - and structures of such types - e.g. NSRect. These are the types where the values determine equality.
The isEqual: method is is usually used for Obj-C objects - you usually don't want to know whether to boxes contain the same name (which is what == would determine), but whether what those names refer to are semantically equivalent - which is what isEqual: determines.
Identical generally means they are the same object in memory (occupy the same memory footprint).
Equal usually means their attribute values are the same.

How to use a single NSValueTransformer subclass to toggle the titles of multiple menu items

I would like to make some bindings that toggle item titles in a popup menu based on a single numerical value in a text field. Let me explain:
This is my UI:
I want my menu items to automatically adjust to singular or plural based on the number in the text field. For example, when I type "1" in the box, I want the menu items to be labeled "minute", "hour" and "day". When I type "4", I want the menu items to be labeled "minutes", "hours" and "days".
What I did to date:
I bound the three menu item's labels to the same key path as the text field's value.
I created an NSValueTransformer subclass to interpret the value in the text field and return singular or plural to be used as item titles.
I applied this value transformer to the three bindings.
The issue:
My value transformer can either return singular or plural but it can't set the appropriate string based on the menu item it's applied to.
It looks to me like a value transformer is generic and can't return different values for different destinations. That would mean that to have the three titles change automatically, I would need to have three different value transformers that return the appropriate string for each menu item. That doesn't seem optimal at all.
Ideally I would be able to combine a string stored in the destination menu item (let's say in the item's tag property) with an "s" in the value transformer when the text field's value is larger than one, or something similar that would enable me to use a single value transformer for all the menu items.
Is there something I missed? What would be an ideal solution?
Okay, this is probably not an ideal solution, but something you could consider: if you set your value transformers from your code (and not in IB), you could instantiate 3 different transformers of the same class. You could give your value transformer an ivar NSString *unit (and add something like [[MyValueTransformer alloc] initWithUnit:]) to allow each to return their own string, but you still have to write the value transformer's code only once.
(Also, if you're ever going to consider making your application localizable, simply appending an "s" to create the plurals is not going to work. You could of course add ivars for both NSString *singular and NSString *plural to do that.)
Edit: Wait, I just realized you can register value transformers! If you register them as MyValueTransformerHours and MyValueTransformerMinutes (by manually allocating and initializing them in your code), you can use them from Interface Builder. See also https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ValueTransformers/Concepts/Registration.html#//apple_ref/doc/uid/20002166-BAJEAIEE.