Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 402 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I convert Int32 to Int in Swift?

#1
It should be easy but I can only find the reverse conversion.
How can I convert Int32 to Int in Swift?
Unless the problem is different?

I have a value stored in Core Data and I want to return it as an Int.

Here is the code I am using, which does not work:

func myNumber () -> Int {
var myUnit:NSManagedObject
myUnit=self.getObject(“EntityName”) // This is working.

return Int(myUnit.valueForKey(“theNUMBER”)?.intValue!)
}
Reply

#2
Sometimes "?" make things twisted ,
adding "!" to Int32 and then convert it to int works

let number1 = someInt32!
let number2 = Int(number1)
Reply

#3
Swift 4.0 producing "Cannot invoke initializer for type 'Int' with an argument list of type '(() -> Int32)"

let number1: Int32 = 10
let number2 = Int(number1)

Simply do this

Int("\(Int32value)")

I'm unable to understand why swift is making things difficult.
Reply

#4
The error is your ? after valueForKey.

Int initializer doesnt accept optionals.

By doing `myUnit.valueForKey(“theNUMBER”)?.intValue!` gives you an optional value and the ! at the end doesnt help it.

Just replace with this:

return Int(myUnit.valueForKey(“theNUMBER”)!.intValue)

But you could also do like this if you want it to be fail safe:

return myUnit.valueForKey(“theNUMBER”)?.integerValue ?? 0

And to shorten you function you can do this:

func myNumber() -> Int {
let myUnit = self.getObject("EntityName") as! NSManagedObject

return myUnit.valueForKey("theNUMBER")?.integerValue ?? 0
}
Reply

#5
Am I missing something or isn't this ridiculously easy?

let number1: Int32 = 10
let number2 = Int(number1)
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through