Wednesday, May 27, 2015

[__NSCFNumber length]: unrecognized selector sent to instance

If you get any of these runtime errors/warnings:
[__NSCFNumber length]: unrecognized selector sent to instance
restkit.object_mapping:RKMappingOperation.m: Coercing NSNull value to nil in shouldSetValue:atKeyPath: -- should be fixed
class User: NSObject {
    var userId: String?
    var userImageUrl: String?

}

Check whether server is sending JSON response not as String like
{
  "id":12345
  "avatar_url":""
}

Solution:

Such error causing properties should be defined as NSObject

class User: NSObject {
    var userId: NSObject?
    var userImageUrl: String?

}

Tuesday, May 19, 2015

Overriding isEqual in Swift

class MyClass: NSObject {

    var value = 5

    override func isEqual(object: AnyObject?) -> Bool {
        if let object = object as? MyClass {
            return value == object.value
        } else {
            return false
        }
    }

    override var hash: Int {
        return value.hashValue
    }
}
var x = MyClass()
var y = MyClass() 
x.isEqual(y) // true

Saturday, May 16, 2015

Instagram iOS SDK integration

Get it here
https://github.com/crino/instagram-ios-sdk

Errs:

{ "code": 403, "error_type": "OAuthForbiddenException", "error_message": "Implicit authentication is disabled" }

Follow these steps:

{ "code": 400, "error_type": "OAuthException", "error_message": "Redirect URI does not match registered redirect URI" }

Make sure REDIRECT URI is set to ig[clientId]://authorize
while registering application on Instagram website.




Tuesday, May 12, 2015

Adding Notifications in Swift

Post Notification:

NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)

Add Observer for Notification in ViewWillAppear:

          
NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOfReceivedNotification:", name:"NotificationIdentifier", object: nil)


Remove in ViewWillDisappear:
        
NSNotificationCenter.defaultCenter().removeObserver(self, name: "NotificationIdentifier", object: nil)Remove in ViewWillDisappear:
Selector Method:

func methodOfReceivedNotification(notification: NSNotification){
    //Action on Notification
}

String to Bool in Swift

To convert String to Bool in Swift, add following extension

extension String {
    func toBool() -> Bool{
        if self == "false" {
            return false
        }else{
            return true
        }
    }