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.
Related
I've been working on nutrition app for ios lately. I'm trying to write this algorithm
weight (lb) / [height (in)]2 x 703
in xcode with swift, but I'm beginner to use swift. I wrote this code
let calculate = ((textforweight.text) / ((textforheight.text)*(textforheight.text))*703)
and it gives me error like "Type of expression is ambiguous without more context" that. Can you help me for this. It might be so easy but I just don't know how to do it
UITextField .text property return a String value.So you need to convert this value to Int first
let weight = Int(textforweight.text!) ?? .zero
let height = Int(textforheight.text!) ?? .zero
Now You can Calculate you result,
let result = weight / height * height * 703
UITextField .text Property Documentation
I'm a beginner programmer, and I'm making a game at the moment. I haven't run into many errors like this, but I know it's really easy to fix.
Heres the code:
func randInRange(range: Range<Int>) -> Int {
return Int(arc4random_uniform(UInt32(range.endIndex - range.startIndex))) + range.startIndex }
Here is the constant I'm trying to work with:
let random = randInRange(self.frame.size.width * 0.3...self.frame.size.width * 0.6)
The error comes out as this: Binary operator '...' be applied to 2 CGFloat operands.
Your method randInRange is expecting a range of Integers, so you need to convert the result of your expression from CGFloat to Integer.
let random = randInRange(Int(self.frame.size.width * 0.3)...Int(self.frame.size.width * 0.6))
I am very new to Swift 2.0 and am pretty stuck here...
I have created a random number generator based off a user set range for low and high limits. This function worked great with my test function when I could set the variables for low and high to Uint32
Once I tried to store the low/high range in NSUserDefaults as an integer. Now when I try to load the values set by the user for the low and high to use in my function below it gives me the error that it needs to be a UInt32 value.
The problem is.... I cannot seem to figure out how to save them in NSUser as that type. Or unwrap it as that type coming out of the UserDefaults.
My specific error message is...
ERROR IS Binary Operator + cannot be applied to operands of type UInt32 and In
Thanks so much ahead of time for your help and time I truly appreciate it! You guys are awesome here!
Let me know if there is anything else you need from me :)
//CREATE SAVE SETTINGS FOR LOW AND HIGH RANGE
NSUserDefaults.standardUserDefaults().integerForKey("CatLowRange")
NSUserDefaults.standardUserDefaults().integerForKey("CatHighRange")
//Variables to load in values from NSUserDefaults on load
var catLowRange = 0 //Load In Low Range From NSUser
var catHighRange = 40 //Load In High Range From NSUser
//LOAD IN VALUES TO VARIABLES FROM NSUSERDEFAULTS
catLowRange = NSUserDefaults.standardUserDefaults().integerForKey("CatLowRange")
catHighRange = NSUserDefaults.standardUserDefaults().integerForKey("CatHighRange")
**//My Function To Generate RandNumber...**
*ERROR IS Binary Operator + cannot be applied to operands of type UInt32 and Int*
randomNumber = arc4random_uniform (catHighRange - catLowRange) + catLowRange
For anyone wondering... Thanks to Adam we have an answer... Here is the code broken down how I used his suggestion to make this thing work :)
//WORKING METHOD!?
//vars for use
let catLowRange : UInt32 = 0
let catHighRange : UInt32 = 40
var randNumGenerated : UInt32 = 0
//SAVEPOINTS (STEP 1 - save the UInt32s as int for saving)
NSUserDefaults.standardUserDefaults().setInteger(Int(catLowRange), forKey: "CatLowRange")
NSUserDefaults.standardUserDefaults().setInteger(Int(catHighRange), forKey: "CatHighRange")
//LOAD (STEP 2 - Take Int save as a UInt32 now)
let catLowRangeLoad = UInt32(NSUserDefaults.standardUserDefaults().integerForKey("CatLowRange"))
let catHighRangeLoad = UInt32(NSUserDefaults.standardUserDefaults().integerForKey("CatHighRange"))
//FUNCTION TEST
randNumGenerated = arc4random_uniform (catHighRangeLoad - catLowRangeLoad) + catLowRangeLoad
//CHECK
print ("catLowRange = \(catLowRange)")
print ("catLowRangeLoad = \(catLowRangeLoad)")
print ("catHighRange = \(catHighRange)")
print ("catHighRangeLoad = \(catHighRangeLoad)")
print ("randNumGen = \(randNumGenerated)")
Use this code (updated for swift 3):
let number : UInt32 = 8
UserDefaults.standard.set(NSNumber(value: number), forKey: "key")
let number2 = (UserDefaults.standard.value(forKey: "key") as? NSNumber)?.uint32Value
I am working on an application that needs to do some displaying and calculating of Double types in Swift. The struct that I created takes a Double as a parameter, and since it also displays it, I would like to keep the trailing zero at the end of the value. However, I cannot seem to keep the trailing zero from being truncated.
Here is what I have been trying in a Playground:
let numberString = "-3.60"
// "3.60"
let positiveString = numberString.stringByReplacingOccurrencesOfString("-", withString: "", options: nil, range: nil)
// 3.6
let positiveDouble = (positiveString as NSString).doubleValue
// "3.60"
let positiveDoubleString = NSString(format: "%0.02f", positiveDouble)
// 3.6
let positiveDoubleWithZeros = positiveDoubleString.doubleValue
I keep getting 3.6 as the result, no matter what I try. What is the obvious part of the conversion that I am missing?
You have to use NSNumberFormatter:
let formatter = NSNumberFormatter()
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 3
println(formatter.stringFromNumber(1.0000))
println(formatter.stringFromNumber(1.2345))
This example will print 1.00 for the first case and 1.234 for the second, the number of minimum and maximum decimal places can be adjust as you need.
I Hope that helps you!
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)))