How would I create an arc4random thing? Like in previous versions I've seen
int blank = arc4random(5)
But how would I do it in the most recent version of Xcode?
let maxNumber = 10
let randomIndex = Int(arc4random_uniform(UInt32(maxNumber)))
Related
iOS 15, Swift 5.5
Sorry, stupid easy question??
I am working on an image processing app, and I have this.
let pixels = UnsafeMutableBufferPointer<UInt32>(start: rawData, count: width * height)
Which I want to break into two pieces, so I tried this.
let fullSet = width * height
let halfSet = fullSet / 2
let pix1 = UnsafeMutableBufferPointer<UInt32>(start: rawData, count: halfSet)
let pix2 = UnsafeMutableBufferPointer<UInt32>(start: (rawData + halfSet), count: halfSet)
I suspect my problem is that the UInt32 is in fact a UInt24 + UInt8, as in red+green+blue and alpha channel.
I count the colours in my pixel Set and I got 120,433... but if I convert to an array I end up with 57,142.
Ok,
I found the answer, thanks everyone for your comments. And yes, it is stupid obvious, you just use a subset.
let pix1 = pixels[(0..<pixels.count / 2)]
let pix2 = pixels[(pixels.count / 2)...]
I was trying too hard I think.
I'm translating the ActionScript code from this development tutorial into swift because I'm trying to teach myself BSP. I came across a line of code I don't fully understand.
var splitH:Boolean = FlxG.random() > 0.5;
Somehow, they are casting an integer as a boolean. What would be the swift equivalent of this line? It doesn't make any sense to me. How are they casting a random value as a boolean as well? Swift doesn't allow this sort of "cross-breeding".
This is my attempt at it so far:
var splitH = Int(arc4random_uniform(2) + 1)
var splitB = false
I split the line into two values because I don't know how to make this as one line. Is this the correct approach?
This doesn't work:
var splitH:Bool = Int(arc4random_uniform(2) + 1)
You can use the ternary operator here as well.
Something along the lines of
var splitH = arc4random_uniform(100) > 50 ? true : false
or simply
var splitH = arc4random_uniform(100) > 50
N.B.: Assuming FlxG.random() is returning uniform random between 0.0 and 1.0. If it has different distribution it won't work, ofc.
If we're talking about pre-4.2 Swift your line is equivalent to:
let splitH: Bool = arc4random_uniform(2) == 1
In Swift 4.2+ you may just use something like this:
let splitH: Bool = Double.random(in: 0.0 ..< 1.0) > 0.5
I am using the Charts framwork for Swift found here. I have a chart that I am trying to fix two issues shown in the chart below:
The first is trying to format my X values from doubles to Ints. I want the value to show 67 and not 67.0. I have tried the following to adjust it but
numbers still stay the same:
let format = NumberFormatter()
format.minimumIntegerDigits = 0
format.minimumFractionDigits = 0
let formatter = DefaultValueFormatter(formatter: format)
chartview.rightAxis.valueFormatter = (formatter as? IAxisValueFormatter)
I also have a problem where the buttom bars dont line up with the buttom of the graph. If you look at the graph above, it looks like 0 starts above the X axis and not at the orgin. Theres a little space below the bars. I would like the bottom of each bar to touch the X-Axis.
Please try like this :
let format = NumberFormatter()
format.minimumIntegerDigits = 0
format.minimumFractionDigits = 0
let formatter = DefaultValueFormatter(formatter: format)
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Label") // replace with your set
chartDataSet.valueFormatter = formatter
For lining Button bars. please try this:
barChartView.rightAxis.axisMinimum = 0.0
barChartView.leftAxis.axisMinimum = 0.0
I just want to know if is it possible to make a label or anything else who will be a integer and to display it to my gameview.
var pvElf = SKLabelNode(fontNamed:"Chalkduster")
pvElf.text = "100";
pvElf.fontSize = 45;
pvElf.position = CGPoint(x:370, y:600)
pvElf.zPosition = 2
addChild(pvElf)
I want pvElf to be an int but I don't find an SKIntNode or anything else. Because I want to decrease this number when I do an action. But if this is a label I can't.
Thank you very much !
Simply update the label's text with the Int (converted to a String).
let someInt = 4
pvElf.text = "\(someInt)"
I have a Dictionary [String:AnyObject] which contains some keys and values.
I want to increment a key to which value is of type Double.
I can do it this way:
let nr = dict["number"] as! Double
dict["number"] = nr + 10
But I dont like that way so Im wondering if there is another way
I tried this:
(dict["number"] as! Double) += 10
But that gives me an error:
Binary operator '+=' cannot be applied to operands of type '(Double)' and 'Double'
Why isn't this working?
Following is an alternative. If you want to avoid force unwrapping an optional:
dict["number"] = (dict["number"] ?? 0) + 10
You are close and in fact you can write to a dictionary using +=, the problem is your cast. For example we can do:
var dict = ["number" : 2]
dict["number"]! += 10
and now dict["number"] returns 12. Now this is creating a new value (12) and replacing it into the dictionary, it is just a clean way of looking at it.
The problem with your code is that the left side (dict["number"] as! Double) gives you a Double. So you have say (12), then the right side is a Double too (10). So your code ends up looking like (12) += 10 which you can clearly see as problematic, since this is equivalent to 12 = 12 + 10, yikes!
So that being said, you can use the my first solution if you are working with native Swift dictionaries, otherwise your solved solution above works too, just a bit longer.
Lastly, if you are really looking for a one liner that works with your exact situation you should be able to do something like:
dict["number"] = (dict["number"] as! Double) + 10
Another option:
dict["number", default: 0] += 10
The safe way of casting would be:
if let num = dict["number"] as? Double {
dict["number"] = num + 10
}