Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Restkit with Swift

Tags:

swift

restkit

I am trying to use RestKit in my Swift based project. I do not seem to be able to use Swift primitive data types, like Int, Double, Bool and so on, except for String, Array and Dictionaries (which presumably is due to the fact, that they are toll-free bridged to NSString, NSArray and NSDictionary)

With objective-c I could define properties in my objects as being primitive datatypes as properties, which are assigned. In Swift I can only use objects (String, NSNumber, Array, Dictionary), otherwise the app crashes with "setValue:forUndefinedKey: this class is not key value coding-compliant for the key aBool".

Example: Here's how my object would look like in Objective-C:

@interface TestObject : NSObject

@property (strong, nonatomic) NSString *aString;
@property (assign, nonatomic) BOOL aBool;
@property (assign, nonatomic) CGFloat aFloat;

@end

and the "equivalent" in Swift:

class TestObject:NSObject {

    var aString:NSString?
    var aBool:Bool?
    var aFloat:Double?

}

That crashes and while I do understand WHY it crashes, I would like to know, whether there is another workaround, than using NSNumber for Booleans, Integers and Floats, in the same way it works in Objective-C?

(If any of the RestKit developers are reading this: First of all thanks for your work and then: Are there any plans regarding Swift support / port to Swift?)

like image 667
alex da franca Avatar asked Feb 25 '15 10:02

alex da franca


2 Answers

I am doing an app in Swift, and we started work in the summer of 2014, when Swift was quite new. I started using RestKit to handle REST communication with a server, and I have spent at least a full month with configuring RestKit for my use.

While I do not know your use case, I have found it easier to use Alamofire and SwiftyJSON for the work instead. It has saved me tons of hours. It also means that I am manually doing the conversion from dynamic types to the types in my entity objects, but the time spent doing that is far less than the time I previously spent trying to configure RestKit.

If this is not a viable way for you, you might want to have a look at the RKValueTransformer classes available in RestKit; in your RKObjectMapping you can specify your own transformers for individual properties. But again, I think the time is better spent with Alamofire and SwiftyJSON. They are native Swift libraries, after all.

like image 110
Joachim Bøggild Avatar answered Nov 07 '22 05:11

Joachim Bøggild


For anyone who can't drop RestKit in a Swift project, this solution might be helpful.

In my case, the app was crashing because RestKit couldn't map "true"/"false" strings from the JSON to a Bool type in Swift.

I solved it adding another String attribute in my class and updating the wanted boolean value on its didSet method. In your case, this would be:

@objc class TestObject:NSObject {

    @objc var aString:NSString?
    @objc var aBool:Bool?
    @objc var aFloat:Double?

    @objc private var aBoolString: String? {
        didSet {
            self.aBool = aBoolString.lowercased() == "true"
        }
    }
}

Hope this helps!

like image 20
Rouger Avatar answered Nov 07 '22 06:11

Rouger