Cast to a control using the control name as a string [duplicate] - iphone

This question already has answers here:
Syntax help - Variable as object name [duplicate]
(1 answer)
Objective C Equivalent of PHP's "Variable Variables" [duplicate]
(2 answers)
Closed 8 years ago.
I have several textfields (and labels) in my XIB and at some point in my application I build dynamicaly a string which contains a control name (i.e. one of the textfields).
How can I refer to the actual textfield using the string I created that holds the name of the textfield.
For example, I have txt1 , txt2, txt3, txt4 as UITextFields
and i have a string (str) that contains one of the fields above names (str ="txt3")
then i want to change the content of the uitextfield txt3 since str have "txt3"
at this point.
How do I cast from the string to the actual control?

If the UITextFields all have a backing property then you can use KVC to get a reference to the control and then you just use it as normal.
NSString *str = #"txt1";
UITextField *myTextField = [self valueForKey:str];
myTextField.text = #"what ever you want to update to";
Side note
Although you have not mentioned the source of str #JefferyThomas raises a very valid point about not trusting use input that has not been validated.

Related

Why I can't use string by index from array? [duplicate]

This question already has an answer here:
What is the point of cell indexing in MATLAB
(1 answer)
Closed 5 months ago.
I'm new in Matlab, can somebody tell me, why I can't use a string by index like this?
adresuSarasas = {
"C:\\ExperimentData\\volunteer01.txt",
"C:\\ExperimentData\\volunteer02.txt",
"C:\\ExperimentData\\volunteer03.txt",
"C:\\ExperimentData\\volunteer04.txt",
"C:\\ExperimentData\\volunteer05.txt",
"C:\\ExperimentData\\volunteer06.txt",
"C:\\ExperimentData\\volunteer07.txt",
"C:\\ExperimentData\\volunteer08.txt",
"C:\\ExperimentData\\volunteer09.txt",
"C:\\ExperimentData\\volunteer10.txt",
}
skirtukas=';';
pradzia=1;
V1=importdata(adresuSarasas(2), skirtukas, pradzia)
I get error on the last line. error: importdata: FNAME must be a string. Thanks.
cell arrays have different output when indexed differently.
adresuSarasas(2) returns a 1x1 cell array that contains your string, while your function expects a string, not a cell array.
adresuSarasas{2} returns the string inside the cell array which is what your function is expecting.
while later version of matlab have functions accept 1x1 cell arrays (so this code works on newer versions) it's still fruitful to know the difference when working with older versions.

How to declare keyword variable in Swift [duplicate]

This question already has an answer here:
Can we use keywords as parameter names in SWIFT?
(1 answer)
Closed 4 years ago.
I am a new iOS programming. I am creating a sample app which get data and set data using Firestore. For the collection contains some documents which and named those properties like extension... So in programming I need to create variable with that name extension in order to set data to Firestore but Xcode is warning me that I don't have to declare that variable name.
I have been trying to figure it out but not work. So, I decided to ask my own question if there any technique to delacre variable name which is warning by Xcode.
var `extension`: String?
var username: String?
init(`extension`: String, username: String) {
self.`extension` = `extension`
self.username = username
}
I think this will work by using ``.

Use the content of a cell as a label of a symbolic variable [duplicate]

This question already has answers here:
Create variables with names from strings
(6 answers)
Closed 5 years ago.
Can I do the following idea in Matlab command?
Assuming that
a = 'c1Tl';
class(a) will return cell.
How I can use the content of an as a cell variable which I can do
c1Tl = 3;
I try to use
sym(a) = 3;
to assign a variable to the content of 'a' but It is not my purpose.
Please help to solve my problem!
Thank you!
The best way to do that is to use a struct data type. You would do something like this:
a = {'c1Tl'};
%Lets make a struct called data which will store the values
data.(a{1}) = 3; % The a{1} accesses the string stored in a and uses it to make a field in the structure data
%To access your data, now you can use
data.c1Tl

Error trying to perform addition using dictionary in swift [duplicate]

This question already has answers here:
Dictionary error: Ambiguous reference to member '+' [duplicate]
(4 answers)
Closed 5 years ago.
teaching myself swift I am trying to understand how dictionaries work. Using playground. I have made a simple dictionary called "menu" that has a list of items with their name as keys and their price as values. Like so:
let menu = ["crisps": 2,
"oranges": 3,
"chicken": 8,
"meat": 12]
Then, I try to add the values of those items like so:
let costOfMeal = menu["crisps"]! + menu["oranges"]! + menu["chicken"]! + menu["meat"]!
This gives me the error: ambiguous reference to member '+'
not sure what's going on. Any input appreciated.
Thanks,
David
let costOfMeals = Array(menu.values).reduce(0, +)
You are trying to add up every key and value together! You should only add up the values. You know the dictionary is Key and Value, and you should only add up the Value's.
This is merely the compiler's automatic type casting getting confused by the multiple additions of unwrapped optionals.
You can help it along by adding an actual integer in the formula.
let costOfMeal = 0 + menu["crisps"]! + menu["oranges"]! + menu["meat"]! + menu["chicken"]!
Don't let it bother you as it has nothing to do with what you're trying to learn and your formula was correct (albeit not safe for production).

Checking all values of an array [duplicate]

This question already has answers here:
How to check if an element is in an array
(18 answers)
Closed 7 years ago.
Swift 2 - so, I have an array and a uitextfield that a user inputs a string, I want to check whether the textfield.text is equal to ANY of the values in the array, can I do this with one line of code rather than lots of if's and else if's?!?
This is a generic code that will do what you are looking for. The if statement checks to see if a given value is equal to something that is located in the array. Simply replace the arr.contains() with the output you have given for your UITextfield.text Try to do a little research before you post. I can see that you are new here, so here is a little bit of help.
var arr = [1,2,3,4]
if arr.contains(3) {
//do something
}