This question already has an answer here:
swift tuple has unexpected print result
(1 answer)
Closed 8 years ago.
Decimal values in tuple give garbage value
let qwerty = ("Rachit", 5.55)
println(qwerty)
It will give output (Rachit, 1.28416751252943e-313)
while
println(qwerty.1)
It will give output 5.55
Why?
Its a Bug . You Can Refer
This Link
Mainly we don't use whole tuple as you used.We used its Values using .0/.1 or using somename
Like
let http200Status = (statusCode: 200, description: "OK")
println("The status code is \(http200Status.statusCode)")
// prints "The status code is 200"
println("The status message is \(http200Status.description)")
// prints "The status message is OK”
Related
This question already has answers here:
How to know if a number is odd or even in Swift?
(5 answers)
Closed 11 months ago.
im absolutely new to development, and trying to learn swift.
Right now i know how to make random number, and my next step is:
Im trying to understand how to check if my random number (127) could be divided by 2 without decimals ?
I have no idea how to do it.
There is a specific API isMultiple(of:) in Standard Library for this purpose
let random = Int.random(in: 0..<100)
let isEven = random.isMultiple(of: 2)
You can use operator % - remainder operator
example:
if randomNumber % 2 == 0 {
print("\(randomNumber) is even")
} else {
print("\(randomNumber) is odd")
}
This question already has answers here:
"The argument type 'String?' can't be assigned to the parameter type 'String'" when using stdin.readLineSync()
(3 answers)
Closed 1 year ago.
I'm new to dart and having a hard time figuring out things. so, I just need some help completing the program below. I have no idea where I'm going wrong.
All I'm getting is an error related to null safety
question :-
Write a program to obtain a number N and increment its value by 1 if the number is divisible by 4 otherwise decrement its value by 1.
import 'dart:io';
void main(){
String? input = stdin.readLineSync();
int number = int.parse(input);
}
This is all that came to my mind, I know the logic, but I'm stuck at getting the user input and converting it.
As the comment suggest, in this answer is explained how Dart will handle if stdin.readLineSync does give a null value. So this should work, notice the ! at the end of stdin.readLineSync.
import 'dart:io';
void main() {
var input = stdin.readLineSync()!;
var number = int.parse(input);
}
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:
BigInteger equivalent in Swift?
(6 answers)
Closed 6 years ago.
I'm getting an error while trying to save a large number which is converted from a string value, I know its larger than CUnsignedLongLong but is there any method to solve this issue?
var strVal = "999999999999999999999"
var n : CUnsignedLongLong = CUnsignedLongLong(strVal)! // Getting error on this line
fatal error: unexpectedly found nil while unwrapping an Optional value
You got error because it is out of the bounds of CUnsignedLongLong.
Max value of CUnsignedLongLong is 18,446,744,073,709,551,615
and you trying to assign value 999,999,999,999,999,999,999
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