Swift iOS convert ArraySlice to CGFloat - ios

I have an array with Int values. In this array I need to get the highest value. Until here I have no problem. But now I need to convert this value to a CGFloat.
let ordersPerHour = [hour0All, hour1All, hour2All, hour3All, hour4All, hour5All, hour6All, hour7All, hour8All, hour9All, hour10All, hour11All, hour12All, hour13All, hour14All, hour15All, hour16All, hour17All, hour18All, hour19All, hour20All, hour21All, hour22All, hour23All]
let maxOrdersPerHourVal = ordersPerHour.sort().suffix(1)
How can I convert ArraySlice to CGFloat? All I have tried failed :-(

let maxOrdersPerHourVal = ordersPerHour.sort.last!
or
let maxOrdersPerHourVal = ordersPerHour.max()
will get the max value in an array.
Then you can cast as normal var floatVal = CGFloat(maxOrdersPerHourVal) if you need to cast the value.

Don't use sort here. Just use max() (or maxElement() in older versions of Swift). That will return an Int rather than a slice.

Related

Xcode random variable doesn't work

I'm using Xcode 7.3.1 and Swift, and i'm trying to set a random number between 1 and 50 like that: variableName = random()%50
Then i have to move an ImageView in the Y axe of that random value:
imageviewName.center.y = imageviewName.center.y - variableName
But it gives me the following error: "cannot convert value of type 'int' to expected argument type 'CGFloat'.
So I declared the variable like that:
var RandomSquirrel2 = CGFloat() but it still doesn't wok.
How can I generate a random number in Swift?
You need to covert the Int to CGFloat
imageviewName.center.y = imageviewName.center.y - CGFloat(variableName)
You need to cast variableName to CGFloat. because random() returns an Int and not A CGFloat.
imageviewName.center.y = imageviewName.center.y - CGFloat(variableName)

swift forcing objective-c int to be assigned as Int32 then crashing

I have an objective c property that has been declared as
#property int xmBufferSize;
If I do sharedExample.xmBufferSize = 1024 it just works fine
but when I am trying to set an integer value for that property from another variable
var getThat:Int = dict["bufferSize"]!.integerValue
sharedExample.xmBufferSize = getThat
It can't do above
Cannot assign a value of type 'Int' to a value of type 'Int32'
If I force this to
sharedExample.xmBufferSize =dict["bufferSize"] as! Int32
It is crashing with Error
Could not cast value of type '__NSCFNumber' to 'Swift.Int32'
EDIT::::
Dict init, there are other objects in dict besides integers
var bufferSize:Int = 1024
var dict = Dictionary<String, AnyObject>() = ["bufferSize":bufferSize]
The value in dict is an NSNumber, which cannot be cast or directly converted to Int32. You can first obtain the NSNumber and then call intValue on it:
if let bufferSize = dict["bufferSize"] as? NSNumber {
sharedExample.xmlBufferSize = bufferSize.intValue
}
The if let … as? allows you to verify that the value is indeed an NSNumber, since (as you said) there can be other types of objects in dict. The then-branch will only execute if dict["bufferSize"] exists and is an NSNumber.
(Note: You can also try integerValue if intValue gives the wrong type, or convert the resulting integer – CInt(bufferSize.integerValue) – as needed. Swift doesn't do implicit conversions between different integer types, so you need to match exactly.)
You need to convert your NSNumber to Int32.
sharedExample.xmBufferSize = dict["bufferSize"]!.intValue
Use type conversion, not as:
sharedExample.xmBufferSize = Int32(dict["bufferSize"] as! Int)
That should work.

Cast from 'Int?' to unrelated type 'NSNumber' always fails

When I try to do the line below, I dont get a warning (not an error). What is this am I am doing something bad?
I am trying to cast the integer earningsSoFar to a NSNumber because I want to get the .stringValue out of it.
I want to understand what is the warning mean here and how to do this right.
self.tv_salaryNumber.text = (earningsSoFar as! NSNumber).stringValue
You can cast Int to NSNumber in this way
let a:Int? = 10
let b = a! as NSNumber
So,in your code,just try
self.tv_salaryNumber.text = (earningsSoFar! as NSNumber).stringValue
Also,as zeneak said,you can make it easier in his way

Fast method to cast [Float] to [CGFloat]?

I'm having a brain cramp this afternoon. This should be easy.
I did read the docs.
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html
It's easy to convert single instances of Float <> CGFloat but I'm looking for a fast method to cast a LARGE array > 500,000 elements of [Float] to [CGFloat].
var sphereRadiusFloat:[Float] = [0.0,1.0,2.0]
var sphereRadiusCGFloat:[CGFloat] = []
sphereRadiusCGFloat = sphereRadiusFloat as CGFloat
The error is
CGFloat is not convertible to [CGFloat]
I also tried
sphereRadiusCGFloat = CGFloat(sphereRadiusFloat)
which gives error
Could not find an overload operator for 'init' that accepts supplied
arguments.
You can use map to do it as follow:
sphereRadiusCGFloat = sphereRadiusFloat.map{CGFloat($0)}

Is it possible to convert a JSValue into an NSNumber?

This will show the following error: 'JSValue' is not convertible to 'NSNumber'. If it's not possible to convert, how should I go about getting the JSValue and assigning it to my NSNumber variable?
var sentences:NSNumber = getSentences.callWithArguments([])
According to the header file JSValue.h, you need to call toNumber() on your JSValue object. So it's probably:
var sentences:NSNumber = getSentences.callWithArguments([]).toNumber()
You can try:
if let sentences = getSentences.callWithArguments([]) as? NSNumber {
// consume sentences here
}
The if let structure is probably the simplest access to it since it may not actually BE a number, If that doesn't work, you'll have to go back to JavaScriptCore and call JSValueToNumber:
let sentences = getSentences.callWithArguments([])
let nSentences = JSValueToNumber(context, sentences, nil)

Resources