我正在寻找创建一个类别,用许多简单的iOS API替换委托方法和回调块.与NSURLConnection上的sendAsyc块类似.有两种技术是无泄漏的,似乎工作正常.每个人的利弊是什么?有没有更好的办法?
选项1.使用类别在NSObject上实现委托的回调方法,并使用外部回调块作用域.
// Add category on NSObject to respond to the delegate
@interface NSObject(BlocksDelegate)
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end
@implementation NSObject(BlocksDelegate)
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Self is scoped to the block that was copied
void(^callback)(NSInteger) = (id)self;
// Call the callback passed if
callback(buttonIndex);
[self release];
}
@end
// Alert View Category
@implementation UIAlertView (BlocksDelegate)
+ (id) alertWithTitle:(NSString*)title
message:(NSString*)message
clickedBlock:(void(^)(NSInteger))buttonIndexClickedBlock
cancelButtonTitle:(NSString*)cancelButtonTitle
otherButtonTitles:(NSString*)otherButtonTitles
{
// Copy block passed in to the Heap and will stay alive …Run Code Online (Sandbox Code Playgroud) 您可以使用不同的加载器加载辅助条目吗?
例如,我正在尝试为脱机缓存创建服务工作者.我已经排除了包含服务工作者的文件夹,并将其包含在另一个文件夹中.没有调用service-worker.js(它只是在静态index.html中手动加载).服务工作者入口点正在拾取React和热门加载器,而不仅仅是通过Babel.这是我配置的重要部分.
entry: {
app: "app.js",
'service-worker': 'persistence/service-worker.js'
},
module: {
loaders:[{
test: /\.js|\.jsx/,
loaders: ["react-hot", "jsx?harmony", "babel"],
exclude: /persistence/
}, {
test: /service\-worker\.js/,
loaders: ["babel"],
include: /persistence/
}]
Run Code Online (Sandbox Code Playgroud)