This question already has an answer here:
Swift: Converting a string into a variable name
(1 answer)
Closed 7 years ago.
I'm using a single action to handle 10 UISwitches by sending a tag value to it. I have a number of integers initialised when launched thus;
int switch_1 = 0;
int switch_2 = 0; etc
When a particular switch is switched on, I want to set the integer to the corresponding integer 'variable' with a 1
So, if switch 2 with tag 2 is turned on, it puts a '1' in the corresponding int 'switch_2' as an integer.
I am getting a string with the right name via 'stringWithFormat' by appending the tag value but don't know how to write the 1 to the corresponding integer variable from it.
Any help would be greatly appreciated. Essentially, I want to write an integer to a 'variable' name with the same name as the generated string value.
Thanks
You can use an array where each index represents your switch, so it would be like following:
int swtichArray[10] = {0}; // if you have 10 switches
While in your action where you handling action of multiple buttons, you could do that:
switchArray[tagOfSwitch] = 1; // if tags are staring from 0 onwards 9
Related
I wanna know How to convert a number in one String,in lua script,but i know If do this
Var = 10 ,
then the "var" Will be equal to 10
But my question is not simple I mean Literally transform the number into a String but keep having its value
Example
Var = 10
So if I add 10 + 5 is equal to 15
And what i want is :
Var + 5 = 15
So the "var" despite being a String works as a number
(I want to do this because I can't pull my variable to be displayed in a message (Var) along with another message that will be between "Your number is:"
It’s quite simple, just wrap your variable like this:
var x = tonumber(x)
print(type(x))
-- Output : number.
This converts string x representing a number in base b [2..36, default: 10] to a number, or nil if invalid; for base 10, it accepts full format (e.g. "1.5e6").
This question already has answers here:
What is the reduce() function doing, in Swift
(4 answers)
Closed 9 months ago.
I am reading iOS 13 Programming Fundamentals with Swift, got to the part about reduce() and I think I understand it more or less, but then there is reduce(into:) and this piece of code:
let nums = [1,2,3,4,5]
let result = nums.reduce(into: [[],[]]) { temp, i in
temp[i%2].append(i)
}
// result is now [[2,4],[1,3,5]]
So this code takes an array of Int and splits it into 2 arrays, even and odd. The problem is that I have no idea what's happening inside the brackets {}.
In the case of reduce, the first parameter is the first one of the iteration and then the closure is supposed to process all the items one after the other, similar to map() but more powerful (here one loop is enough to get the two arrays but with map() I would need 2 loops, according to the book).
I cannot understand the syntax here anyway, especially what does "temp" stand for and that use of "in". And how is "append()" appending the value to the proper array??
Inside the closure, "temp" is the result format which is [[][]] and "i" is each number. As you said it processes all numbers in a loop. When % is used it returns the division remainder, so for the odd numbers like "1,3,5", it returns "1" and for the even numbers "0", which means that "temp" appends these values to the array in these respective indexes.
So if we debug and replace the variables for constants the results would be:
temp[1].append(1) //1%2 = 1/2 left 1 [[][1]]
temp[0].append(2) //2%2 = 2/2 left 0 [[2][1]]
temp[1].append(3) //3%2 = 3/2 = 1 left 1 [[2][1,3]]
temp[0].append(4) //4%2 = 4/2 left 0 [[2,4][1,3]]
temp[1].append(5) //5%2 = 5/2 = 2 left 1 [[2,4][1,3,5]]
According to the documentation the closure is called sequentially with a mutable accumulating value initialized that when exhausted, is returned to the caller.
This question already has answers here:
Adding to an existing value in Erlang
(2 answers)
Closed 7 years ago.
I know that a record in Erlang cannot be changed once it has been set. I am attempting to use a record to increase a value.
add_new_num() ->
Number = random:uniform(6),
STR = #adder{value = 7},
New = add(STR, Number).
add(#adder{value =V} = Adder, Value) ->
Adder#adder{value = V + Value}.
When running add_new_num() I will always get 7 + Number. This is not what I want. I want to get it to do the following.
add_new_num() -> 7 + Number = Val
add_new_num() -> Val + Number = Val2
add_new_num() -> Val2 + Number = Val3
...
How can I achieve this?
There are various ways to do this. Think about where you want to store the value: Erlang doesn't have "static variables" like C, so the function itself cannot remember the value.
You could pass the current record as an argument to add_new_num, and get the updated record from its return value. You could keep a process running, and send messages to query it for the current value and to ask it to increase the value. Or you could store the value in an ETS table, or even Mnesia.
This question already has answers here:
Search for an item in a Lua list
(12 answers)
Lua find a key from a value
(3 answers)
Closed 9 years ago.
I have this table:
maps = {4707191, 4747722, 1702169, 3994471, 4708958, 4008546, 4323335, 4516043, 4612295, 3469987, 4337892, 238378, 3088188, 329627, 3526384, 433483}
How can I make a script so if 1702169 (for example) is picked from the table, it prints ''That's the number''?
The easiest way to do what (i think) you want is with pairs() function. This is a stateless iterator which you can read more about here: http://www.lua.org/pil/7.3.html
If you simply want to scan through the entire table and see if it contains a value, then you can use this simple code:
local maps = {4707191, 4747722, 1702169, 3994471, 4708958, 4008546, 4323335, 4516043, 4612295, 3469987, 4337892, 238378, 3088188, 329627, 3526384, 433483}
local picked = 1702169
for i, v in pairs(maps) do
if v == picked then
print("That's the number")
break
end
end
The above code will iterate through the whole table where i is the key and v is the value of the table[key]=value pairs.
I am slightly unclear about your end goal, but you could create this into a function and/or modify it to your actual needs. Feel free to update your original post with more information and I can provide you with a more specific answer.
This question already has an answer here:
Integers not properly returned from a property list (plist) array in Objective-C
(1 answer)
Closed 8 years ago.
I am trying to take a small integer (eg 4) from a pList and put it into a managed object for later manipulation. However, by the time I come to take it out of the managed object and put it into an NSInteger it has changed completely. 4 has become 237371328 !
The number is stored as "Number" in the pList and integer 16 in the managed object.
I have two fields: timesAsNumber which is integer 16 and timesUsed which is string (my current work around!).
The lines of code involved are:
NSArray *usageFetchResults = [self.objectContext executeFetchRequest:request error:&error];
NSLog(#"Here is the usageArray: %#, with error: %#",usageFetchResults, error);
This gives the log result:
data: {\n feature = video;\n timesAsNumber = 4;\n timesUsed = 4;\n})"
), with error: (null)
So the logger knows the value of timesAsNumber is 4.
NSLog(#"timesAsNumber straight from the Managed Object: %#", [currentUseData valueForKey:#"timesAsNumber"]);
Produces result: timesAsNumber straight from the Managed Object: 4 so still no problem.
However,
NSInteger timesUsedAsInt = [currentUseData valueForKey:#"timesAsNumber"];
NSLog(#"times As Number now reads: %ld", (long)timesUsedAsInt);
Produces the result: times As Number now reads: 237371328
I need to have the number as an integer for manipulation and my workaround of storing as string and converting to and fro is hardly elegant!
I teach High School computing so I know about storage of floating point numbers: excess-127, twos complement etc. I assume the problem arises from the different ways the integer is stored in the pList, the managed object and the NSInteger. However, I cannot figure out what those storage methods are so that I can work with them.
Any help gratefully received.
Tim.
When you are storing a number in plist or coreData, it is stored as NSNumber. So you access the value as NSInteger myInt = myNumber.intValue. When you are converting it back, use NSNumber *myNumber = [NSNumber numberWithInt:myInt]. That large number (237371328) could appear because you forgot to convert the NSNumber to int. Hope this helps.
Edit:
Try:
NSInteger timesUsedAsInt = [[currentUseData valueForKey:#"timesAsNumber"] intValue];
NSLog(#"times As Number now reads: %ld", (long)timesUsedAsInt);