ios - Compare values of an array at runtime, not knowing what they are going to be - objective-c

I have 2 places where I require a UIPickerView in my app and the values that the second represents is dependent on the result of the first one.
However the values of the first one can be changed at run time and so I do not know what they are going to be. This leaves me out the hardcoding option of doing it this way:
if([_menuCategoryPickerFld.text isEqualToString:#"food_breakfast"])
{
}
This would require hardcoding values into my app.
Now the source of my data is MySQL imported via JSON, so when I select an option in the first UIPickerView, it will query and return the correct JSON file.
I need to know if I can compare a value of an array that I will only receive once the app is running??
Thanks in advance.

Not sure I understand, but why not just:
NSString* mystery = #"blah";
if([_menuCategoryPickerFld.text isEqualToString:mystery]){ ... }
You don't need to the the contents of a string to use it in a compare this way.

Related

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!

Enumeration in the dialog box

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.

Select a string to change it

I would like to create a plug-in necessary for our hospital, but I have never coded in Objective-C. I have been looking at many examples but cannot see how to select a specific word into a file to change it.
I want to select the word MRSC that is written on a line beginning with 0008,0060 and change it to MR (to tell our server that this file was sent by a MRI machine, which is necessary to archive it). I know how I can change the word, using NSMutableArray, but I don't know how to select my MRSC. How can I do this?
Something along these lines, assuming ARC or GC memory management:
NSString *inputLine = ... ; // the line you've read in
if ([inputLine hasPrefix:#"0008,0060") // is it a line needing changing?
{
// replace MRSC with MR and store result in same variable
inputLine = [inputLine stringByReplacingOccurencesOfString:#"MRSC" withString:#"MR"];
}
You could also do it with mutable strings if you wish, it makes little difference in this case.

How to compare URLs when one is in /Users/john format and other in file://localhost/Users/etc

I am enumerating through directories which returns URLs in the form:
file://localhost/Users/john/Documents/static.gif
I want to check these results against URLs in the form of:
/Users/john
Specifically, I want to know if the first URL is contained in the second.
I've been going through the various NSURL methods and can't find a method that will allow me to convert one form into the other for easy comparison, or actually do the comparison for me.
You can use the path method to get the strings. The first URL will become #"/localhost/Users/john/Documents/static.gif" and second remains the same.
You can check where second URL contains the first using,
if ( [[URL1 path] hasPrefix:[URL2 path]] ) {
NSLog(#"Contained");
}

Finding curCapacity and maxCapacity Value, iPhone

A method which can be used for finding the battery percentage of your device is found here:
http://blog.coriolis.ch/2009/02/14/reading-the-battery-level-programmatically/comment-page-1/#comment-6085
I have tried it and it works great. If you have a closer look at the code there are two values curCapacity and maxCapacity. I want to be able to use these values in other calculations, yet when I try and do this I always get the error 'undeclared'.
Any ideas as to why?
where are you trying to use this variables? they are declared inside - (double) batteryLevel method so you cannot use them in other methods. if you want to use them in other places, declare them in your .h file. so you will be able to get access to these values not only from this method.