transformerUsingForwardBlock

transformerUsingForwardBlock

你好,我遇到了使用地幔可可足类的警告
它是“不建议使用reversibleTransformerWithForwardBlock:由+ transformerUsingForwardBlock:reverseBlock代替”

我的代码:

 + (NSValueTransformer *)conditionDescriptionJSONTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSArray     *values) {
    return [values firstObject];
} reverseBlock:^(NSString *str) {
    return @[str];
}];
}

因此,我要问的是如何更新此代码以消除错误?

我是新手,所以非常感谢您的帮助:]

最佳答案

如果您不想还原到以前的版本...

旧方法:

+ (NSValueTransformer *)dateJSONTransformer{

     return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSString *str){
                  return [NSDate dateWithTimeIntervalSince1970:str.floatValue];
            } reverseBlock:^(NSDate *date) {
                  return [NSString stringWithFormat:@"%f", [date timeIntervalSince1970]];
            }];
}

新方法:
+ (NSValueTransformer *)dateJSONTransformer{

     return [MTLValueTransformer transformerUsingForwardBlock:^(NSString *str, BOOL *success, NSError **error){
                  return [NSDate dateWithTimeIntervalSince1970:str.floatValue];
            } reverseBlock:^(NSDate *date, BOOL *success, NSError **error) {
                  return [NSString stringWithFormat:@"%f", [date timeIntervalSince1970]];
            }];
}

简而言之:

reversibleTransformerWithForwardBlock替换:transformerUsingForwardBlock
更改:^(NSString *str)^(NSString *str, BOOL *success, NSError **error)
更改:^(NSDate *date)^(NSDate *date, BOOL *success, NSError **error)

10-08 05:53