我很困惑使用哪个以及何时使用.有经验法则吗?大多数情况下可以使用它们吗?任何特殊规则?
@property (nonatomic, retain) NSDate *theDateFromPicker;
@property (nonatomic, copy) NSDate *theDateFromPicker;
Run Code Online (Sandbox Code Playgroud)
在这种情况下哪个是最好的选择?
谢谢-Code
当使用返回块的方法时,它们可以非常方便.但是,当你必须将它们中的一些串在一起时,它会很快变得混乱
例如,您必须连续调用4个URL:
[remoteAPIWithURL:url1 success:^(int status){
[remoteAPIWithURL:url2 success:^(int status){
[remoteAPIWithURL:url3 success:^(int status){
[remoteAPIWithURL:url2 success:^(int status){
//succes!!!
}];
}];
}];
}];
Run Code Online (Sandbox Code Playgroud)
因此,对于每次迭代,我都会更深入一级,我甚至还没有处理嵌套块中的错误.
当存在实际循环时,它会变得更糟.例如,假设我想以100个块的形式上传文件:
- (void) continueUploadWithBlockNr:(int)blockNr
{
if(blocknr>=100)
{
//success!!!
}
[remoteAPIUploadFile:file withBlockNr:blockNr success:^(int status)
{
[self continueUploadWithBlockNr:blockNr];
}];
}
Run Code Online (Sandbox Code Playgroud)
这感觉非常不直观,并且非常快速地变得非常难以理解.
在.Net中,他们使用async和await关键字解决了所有这些问题,基本上将这些延续展开为一个看似同步的流程.
Objective C中的最佳实践是什么?
lambda continuations objective-c structured-programming objective-c-blocks
本课题引用了这个问题: 如何用块简化回调逻辑?
我的标题有这些typedef
typedef void (^StuffDoneBlock)(NSDictionary * parsedData);
typedef void (^StuffFailedBlock)(NSError * error);
Run Code Online (Sandbox Code Playgroud)
并在初始化
stuffDoneCallback = Block_copy(done);
StuffFailedCallback = Block_copy(error);
Run Code Online (Sandbox Code Playgroud)
在本文中,它说Block_copy是不必要的.但是它需要一个桥接演员.编译器消息如下:
error: cast of block pointer type 'StuffDoneBlock' (aka 'void (^)(NSDictionary *__strong)') to C pointer type 'const void *' requires a bridged cast [4]
stuffDoneCallback = _bridge(Block_copy(done));
^~~~~~~~~~~~~~~~
/Developer-4.2/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/usr/include/Block.h:60:61: note: instantiated from:
#define Block_copy(...) ((__typeof(__VA_ARGS__))_Block_copy((const void *)(__VA_ARGS__)))
^~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)