-[Uithreadsafenode Canperformaction:Withsender:]: Unrecognized Selector Sent to Instance

How to add a method to a class at runtime in swift

You could add a compare method to NSNull like this:

Objective-C:

#import <objc/runtime.h>

static inline NSComparisonResult compareNulls(id self, SEL _cmd, NSNull *other) {
if([other isKindOfClass:[NSNull class]]) {
return NSOrderedSame; // Nulls are always the same.
}

return NSOrderedDescending;
}

@implementation NSNull (Comparisons)

+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
const char *encoding = [[NSString stringWithFormat:@"%s@:@", @encode(NSComparisonResult)] UTF8String];
class_addMethod([self class], @selector(compare:), (IMP)compareNulls, encoding);
});
}

@end

Swift:

// Add this code to your AppDelegate.swift file:
import ObjectiveC

fileprivate func compareNulls(_ self: AnyObject, _ _cmd: Selector, _ other: AnyObject) -> ComparisonResult {
if other is NSNull {
return .orderedSame
}

return .orderedDescending
}

fileprivate func addNSNullCompareImplementationIfNecessary() {
let sel = NSSelectorFromString("compareNulls:")
guard class_getMethodImplementation(NSNull.self, sel) == nil else {
return
}

let types = "i@:@"
class_addMethod(NSNull.self, sel, imp_implementationWithBlock(compareNulls), types)
}

// Add this line to your -didFinishLaunching: function:
addNSNullCompareImplementationIfNecessary()

This is only a temporary solution that will stop the crashes.

I would nevertheless encourage you to a) file a bug report, and b) continue investigating why this happened - clearly having an NSNull in this case wasn't expected by Parse...

How can we Route something like {calendar}/{controller}/{action}/{id}

Firstly, even by 'hiding' the calendar ID behind a string there is still nothing to stop a user from trying to guess an appropriate ID. Whatever solution you use will need to have security built in to the actions to ensure the appropriate data is only shown to authorized users.

To get the calendar - just pass the string value into your action method and decode it there.

routes.MapRoute(
"CalendarRoute",
"{calendar}/{controller}/{action}/{id}",
new {
calendar = "Empty",
controller = "Frontend",
action = "Index",
id = UrlParameter.Optional }
);

public ActionResult Index(string calendar, int? id) {
// decode the calendar into something useful
...
}


Related Topics



Leave a reply



Submit