BAD INSTRUCTION arc4random_uniform - ios

I was trying to do something about percentages but i think i did something wrong.
let randomNum = arc4random_uniform(25) + 71
let wrongNumber = 100 - Int(randomNum)
let firstWrong = wrongNumber - 10
var first = arc4random_uniform(UInt32(firstWrong))
var second = arc4random_uniform(UInt32(wrongNumber) - first)
var third = arc4random_uniform(UInt32(wrongNumber) - (first + second))
let plus = (UInt32(wrongNumber) - (first + second + third)) / 3
first = first + plus
second = second + plus
third = third + plus
let total = randomNum + first + second + third
if (total < 100) {
first += (100 - total)
}
It sometimes gives this BAD INSTRUCTION error
And it usually works fine

Your logic is completely flawed, consider the first few lines:
let randomNum = arc4random_uniform(25) + 71 // values 71 ... 95
let wrongNumber = 100 - Int(randomNum) //values 5 ... 29
let firstWrong = wrongNumber - 10 // -5 ... 19
The maximum randomNum is 95. Then wrongNumber is 5 and firstWrong is -5.
Then
var first = arc4random_uniform(UInt32(firstWrong))
has to crash when casting -5 to an unsigned number.
Similar errors can happen on other lines if the values get into negatives.

Related

Handle Multiple UISlider

I have 4 UISlider with minimum value 0, Maximum value 10. I have one constant value(Ex:20).
My goal is sum of 4 slider value doesn't go above that constant value.
Ex: if i drag and set to maximum value of 1st and 2nd slider(i.e 10), Now the sum of 4 slider is 20.
I don't want to allow other 2 sliders(3rd,4th) to increase. if i decrease any slider(1st or 2nd) value means then i can allow them(2,3,4) to increase sliders values.
help me to achieve this.
You can see this screen shot
You can see this screen shot
In the #IBAction sliderValueChanged, calculate the sum of all sliders. If the sum is greater the the maximum, just reset the value of the "causing" slider (e.g. the one that is currently being dragged by the user) to the maximum allowed value.
// ...
let maxSum = 20
// ...
#IBAction func sliderValueChanged(_ sender: UISlider) {
let sum = slider1.value + slider2.value + slider3.value + slider4.value // better: use outlet collection
if (sum > maxSum) {
let overflow = sum - maxSum
sender.value = sender.value - overflow
}
}
Have a central method that all the sliders call for the valueChanged event.
In that method, sum the values of all the sliders. As the sum changes, adjust the max of all the sliders so that increasing any one slider can't exceed your sum total value. (Each slider's new max would be the the difference between it's current value and max_sum_total - current_sum_total.
So if you set all the sliders to 5, your method would adjust the max for all the sliders to 5.
If Slider A was at 10, Slider B was at 5, and the others were at 0,
Slider A max would be MIN((20 - (A+B+C+D) - A), 10) //5
Slider B max would be MIN((20 - (A+B+C+D) - B), 10) //10
...and so on.
You can use the below code for rough start. i am using single method to handle all 4 UISliders. you can refactor code as you want
#IBAction func touchDrag(_ sender: UISlider) {
switch sender {
case slider1:
let f1 = (sender.value + slider2.value)
let f2 = (slider3.value + slider4.value)
if f1 + f2 > 20 {
sender.value = 20 - (slider2.value + slider3.value + slider4.value)
}
case slider2:
let f1 = (slider1.value + sender.value)
let f2 = (slider3.value + slider4.value)
if f1 + f2 > 20 {
sender.value = 20 - (slider1.value + slider3.value + slider4.value)
}
case slider3:
let f1 = (slider1.value + slider2.value)
let f2 = (sender.value + slider4.value)
if f1 + f2 > 20 {
sender.value = 20 - (slider1.value + slider2.value + slider4.value)
}
default:
let f1 = (slider1.value + slider2.value)
let f2 = (slider3.value + sender.value)
if f1 + f2 > 20 {
sender.value = 20 - (slider1.value + slider2.value + slider3.value)
}
}
}

Need to restart numbering when it gets to 1 and jump to 99

How would I go about doing this? the code I have is
let myInt = Int(currentSaleLabel.text!)
let sale1Number = (myInt! - 1 + 100) % 100
let sale2Number = (sale1Number - 1 + 100) % 100
let sale3Number = (sale2Number - 1 + 100) % 100
let sale4Number = (sale3Number - 1 + 100) % 100
let sale5Number = (sale4Number - 1 + 100) % 100
let sale6Number = (sale5Number - 1 + 100) % 100
let sale7Number = (sale6Number - 1 + 100) % 100
let sale8Number = (sale7Number - 1 + 100) % 100
let sale9Number = (sale8Number - 1 + 100) % 100
let sale10Number = (sale9Number - 1 + 100) % 100
This works fine for numbers 0-99 but I need to eliminate the number 0 also.
One approach is to make a limit of 99 with modulo % operator, like this:
let sale1Number = (myInt! - 1 + 100) % 100
This decrements myInt, and wraps it to 99 when it gets negative. Adding 100 prior to applying % has no effect on numbers in range 0..99. Negative numbers above -100 will get processed correctly.
I am actually also trying to avoid the number 0
You can use a modified formula, like this:
let sale1Number = (myInt! - 2 + 100) % 100 + 1

Multiplication table in Swift ios

I am learning how to make a multiplication table in swift and used
override func viewDidLoad() {
let n = Int(str)!
while (i<=10) {
let st = "\(n) * \(i) = \(n * i)"
lbl.text = st
i += 1
}
this code. i have a label in which i want to show the table, but the problem is that only the last result which is say 2*10 = 20 is showing and not all the other value. i am confused what to do, please help what to do so that all the values are displayed.
Glad you've decided to learn Swift. You're on the right track, but as others have said, your final iteration of the loop is replacing the contents of lbl.text.
There are many ways to achieve what you want, but for problems like this I'd suggest starting off working in a playground rather than worrying about labels, viewDidLoad and suchlike.
Here's a nice Swift-y way to do what you want
let n = 12
let table = Array(0...10).map({"\(n) * \($0) = \(n * $0)"}).joinWithSeparator("\n")
print("\(table)")
Gives…
12 * 0 = 0
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120
To break that down…
// Take numbers 0 to 10 and make an array
Array(0...10).
// use the map function to convert each member of the array to a string
// $0 represents each value in turn.
// The result is an array of strings
map({"\(n) * \($0) = \(n * $0)"}).
// Join all the members of your `String` array with a newline character
joinWithSeparator("\n")
Try it for yourself. In Xcode, File -> New -> Playground, and just paste in that code. Good luck!
That's because every time the loop iterates, it overwrites the previous value in label.text. You need to append the new value to existing string value in label, as suggested by RichardG.
let n = Int(str)!
while (i<=10) {
let st = "\(n) * \(i) = \(n * i)"
lbl.text = lbl.text + " " +st //Append new value to already existing value in label.text
i += 1
}
There is also a possibility of UI issue. You have to provide number of lines to the label or it will mess up the display. It also needs to be of size enough to hold your data. A better option would be UITextView which is scrollable, if you are unwilling to handle cases for label height and width. But if you want to stick with UILabel, the following code will resize the label depending on text for you:
lbl.numberOfLines = 0; //You only need to call this once. Maybe in `viewDidLoad` or Storyboard itself.
lbl.text = #"Some long long long text"; //here you set the text to label
[lbl sizeToFit]; //You must call this method after setting text to label
You can also handle that by Autolayout constraints.
Easy way to do it with SWIFT 2.0
var tableOf = 2 //Change the table you want
for index in 1...10 {
print("\(tableOf) X \(index) = \(index * tableOf)")
}
OUTPUT
repeat-while loop, performs a single pass through the loop block first before considering the loop's condition (exactly what do-while loop does).
1) repeat...while Loop
var i = Int()
repeat {
print("\(i) * \(i) = \(i * 11)")
i += 1
} while i <= 11
2) While Loop
var i = Int()
while i <= 11
{
print("\(i) * \(i) = \(i * 11)")
i += 1
}
3) For Loop
for n in 1..<11
{
print("\(n) * \(n) = \(n * 10)")
}

UInt8 is not convertible to CGFloat error in iOS Swift

I am a beginner level programmer in iOS app development using Swift. Now I am facing the compile time issue "UInt8 is not convertible to CGFloat"
var numberOfAvatars:Int = 8
let count:Int = 1
let columns:Int = 3
let dimension:CGFloat = 84.0
var spacing:CGFloat = (avatarContentcroll.frame.size.width - columns * dimension)/(columns+1)
var scHeight:CGFloat = spacing + (numberOfAvatars/columns) * (dimension + spacing)
I've tried all the solutions out there and did many experiments. And I am not sure why still I am getting this error.
You have to do it explicitly as
var spacing:CGFloat = CGFloat((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = CGFloat(spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing))
or you can try converting every expression in CGFloat
var spacing:CGFloat = ((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing)
You must explicitly cast your Ints as CGFloats (via as), or construct them -- like so:
var spacing:CGFloat = ((avatarContentcroll.frame.size.width) - CGFloat(columns) * (dimension))/(columns+1)
var scHeight:CGFloat = spacing + (CGFloat(numberOfAvatars)/CGFloat(columns)) * (dimension + spacing)

Lua integer won't increment

I have this simple lua function designed to solve the problem of consecutive prime sum.
The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
this is my function:
function numOfConsecPrimes(limit)
a = allPrimes(limit/2)
length = table.getn(a)
sumSoFar = 0 innerSum = 0 finalSum = 0
pos = 1
items = 0 innerItems = 0 finalItems = 0
resetpos = pos
while resetpos < length do
pos = resetpos
resetpos = resetpos + 1
items = 0
sumSoFar = 0
while sumSoFar < limit and pos < length do
if isPrime(sumSoFar) == true then innerSum = sumSoFar innerItems = items end
print(sumSoFar)
sumSofar = sumSoFar + a[pos]
print(a[pos] .."->"..sumSoFar)
pos = pos + 1
items = items + 1
end
if innerItems > finalItems then finalItems = innerItems finalSum = innerSum end
end
end
But for some reason, sumSoFar just won't change. I'm printing it before and after the addition of a[pos] and it stays zero always. I'm printing a[pos] as you see and the values are fine. So what's going on?
If this is your exact code then you simply have a typo.
sumSofar = sumSoFar + a[pos]
Capitalize the f in the first sumSofar so it matches all the other ones.

Resources