【问题标题】:Strings with spaces failed to convert as NSURLs - Mantle带空格的字符串无法转换为 NSURL - Mantle
【发布时间】:2015-08-20 23:18:32
【问题描述】:

想知道我们是否可以解决在 Mantle 中将带空格的字符串转换为 NSURL 失败的问题?

我遇到了 Mantle 错误:

错误域=MTLTransformerErrorHandlingErrorDomain Code=1 "无法将字符串转换为 URL" UserInfo=0x7ff9e8de4090 {MTLTransformerErrorHandlingInputValueErrorKey=https://x.com/dev-pub-image-md/x-img/02020-x yy z@2X.png, NSLocalizedDescription=无法将字符串转换为 URL, NSLocalizedFailureReason=输入 URL 字符串 @987654322 @yy z@2X.png 格式错误}

类文件下方;

.h -

#import "Mantle.h"

@interface Place : MTLModel <MTLJSONSerializing>

@property (strong, nonatomic) NSString *placeId;
@property (strong, nonatomic) NSURL *logoURL;

@end

.m -

#import "Place.h"

@implementation Place

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{@"placeId": @"placeId",
             @"logoURL":@"circleImage"
             };
}

+ (NSValueTransformer *)logoURLJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLURLValueTransformerName];
}

@end

提前致谢!

【问题讨论】:

    标签: ios github-mantle


    【解决方案1】:

    发生这种情况是因为您的字符串不是 URL 结尾编码的(URL 不能有空格)。

    首先 - 使用以下方法对您的字符串进行 URL 编码。来源:Stackoverflow

    - (NSString *)urlencodeString:(NSString*)string {
        NSMutableString *output = [NSMutableString string];
        const unsigned char *source = (const unsigned char *)[self UTF8String];
        int sourceLen = strlen((const char *)source);
        for (int i = 0; i < sourceLen; ++i) {
            const unsigned char thisChar = source[i];
            if (thisChar == ' '){
                [output appendString:@"+"];
            } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || 
                       (thisChar >= 'a' && thisChar <= 'z') ||
                       (thisChar >= 'A' && thisChar <= 'Z') ||
                       (thisChar >= '0' && thisChar <= '9')) {
                [output appendFormat:@"%c", thisChar];
            } else {
                [output appendFormat:@"%%%02X", thisChar];
            }
        }
        return output;
    }
    

    然后将其转换为 URL。

    在您的特定场景中,您正在使用 Mantle JSON 转换器。所以你可以做的是;

    + (NSValueTransformer *)logoURLJSONTransformer {
        return [MTLValueTransformer transformerUsingReversibleBlock:^id(NSString *str, BOOL *success, NSError *__autoreleasing *error) {
            if (success) {
                NSString *urlEncodedString  = [self urlencodeString:str];
                return [NSURL URLWithString:urlEncodedString];
            }else{
                return @"";
            }
    
        }];
    }
    

    【讨论】:

      【解决方案2】:

      你可以试试这个

      NSString *baseurlString = [NSString stringWithFormat:@"your_url_here"];
      NSString *cleanedUrl = [baseurlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
      

      然后将这个cleanedUrl 用于您的工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-01
        相关资源
        最近更新 更多