Is there a Sinatra style routing library/class for Objective-C?
Is there an equivalent to Sinatra's style URL routing 开发者_JAVA百科for use with objective-C? I wish to map strings to functions with arguments to handle some internal navigation logic.
I would like to have a controller manage routes like what happens in Davis.js, Express, Sinatra etc. I know TTNavigationController has this mapping, but I am looking for something more lightweight and not tied to a UIKit (or similar) NavigationController.
Something which would would work like the following:
// in a controller
[Router map:@"/page/:name/" toSelector:@selector(loadPage:)];
// then elsewhere
[Router dispatch:@"/page/about/"];
I understand this is possible to create this myself, but before going down this route (excuse the pun) I would like to know if there is anything out there already. I created my own JavaScript implementation previously, then weeks later a number of superior libraries were created/publicised.
I have created an implementation of this on GutHub: https://github.com/rc1/RCRouter.
It is not yet battle tested but a proof of concept and is very likely needs some improvements. Any help to improve it will be much appreciated.
Here is the Example Usage usage:
#include "RCRouter.h"
-(id)init {
// map routes
[RCRouter map:@"/page/:title/" to:self with:@selector(page:)];
[RCRouter map:@"/color/:r/:g/:b/:a/" to:self with:@selector(color:)];
// dispatch routes
[RCRouter dispatch:@"/page/welcome/"];
[RCRouter dispatch:@"/color/255/255/255/255/"];
}
- (void)page:(NSDictionary*)params {
NSString *pageTitle = [params objectForKey:@"title"];
}
- (void)color:(NSDictionary*)params {
float red = [[params objectForKey:@"r"] floatValue];
float green = [[params objectForKey:@"g"] floatValue];
float blue = [[params objectForKey:@"b"] floatValue];
}
Delegate
Add delegate to the Router can also be added by using the following:
[RCRouter addDelegate:self];
The following optional delegate methods are available as part of :
- (BOOL)allow:(NSString*)route;
- (void)willDispatchRoute:(NSString*)route to:(id)object;
- (void)didDispatchRoute:(NSString*)route to:(id)object;
- (void)noRouteFor:(NSString*)route;
精彩评论