Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftyJSON & Swift 3: Cannot convert return expression of type 'Int32?' to return type > 'Int?'

We are upgrading to SwiftyJSON Swift 3 with CocoaPods config pod 'SwiftyJSON', '3.1.0'.

We are getting this error:

/Users/xxx/Documents/iOS/xxx/Pods/SwiftyJSON/Source/SwiftyJSON.swift:866:33: Cannot convert return expression of type 'Int32?' to return type 'Int?'

Error is on the return statement in SwiftyJSON.swift:

public var int: Int? {
    get {
        return self.number?.int32Value
    }
    set {
        if let newValue = newValue {
            self.object = NSNumber(value: newValue)
        } else {
            self.object = NSNull()
        }
    }
}

Anyone know what the issue is? Is this an issue with our CocoaPods config or with SwiftyJSON?

like image 314
Marcus Leon Avatar asked Oct 01 '16 15:10

Marcus Leon


People also ask

What is the use of SwiftyJSON?

SwiftyJSON is a library that helps to read and process JSON data from an API/Server. So why use SwiftyJSON? Swift by nature is strict about data types and wants the user to explicitly declare it. This becomes a problem as JSON data is usually implicit about data types.

What is Alamofire and SwiftyJSON?

Alamofire is an HTTP networking library written in Swift. SwiftyJSON makes it easy to deal with JSON data in Swift. Steps to setup the CocoaPods. Open Terminal. CocoaPods runs on ruby so update your system.

How do I add SwiftyJSON to Xcode?

Adding SwiftyJSON to the demo project Here's our path: Xcode > ( Xcode project name) > Targets > (Xcode project name). In the General tab, under the Frameworks and Libraries dropdown, we click on + and select Add package dependency. Then, we enter the package Git URL: https://github.com/SwiftyJSON/SwiftyJSON.git.

What is Jsonserialization in Swift?

An object that converts between JSON and the equivalent Foundation objects.


2 Answers

I Just replace one line of code with below code. Simple

public var int: Int? {
        get {
            return self.number?.intValue
        }
        set {
            if let newValue = newValue {
                self.object = NSNumber(value: newValue)
            } else {
                self.object = NSNull()
            }
        }
    }
like image 121
Mehul Avatar answered Nov 05 '22 12:11

Mehul


Realized the supported version of SwiftyJSON is 3.0.0 and not 3.1.0. Used 3.0.0 and the issue went away.

pod 'SwiftyJSON', '3.0.0'

like image 39
Marcus Leon Avatar answered Nov 05 '22 12:11

Marcus Leon