0

我有一个 URL 编码 NSStrings 的简短方法。

- (NSString *)urlEncodeValue:(NSString *)strToEncode
{
    NSLog(@"Testing strToEncode: %@", strToEncode);
    NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)strToEncode, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8));
    NSLog(@"Testing strToEncode: %@", strToEncode);
    NSLog(@"Testing urlEncode: %@", encodedString);
    return encodedString;
}

NSLog 行用于调试,当我解决此问题时将被注释掉。

尝试使用字符串 ,Testing URLencode % "hmm"可以按预期工作。

2012-11-03 06:44:02.140 FoodyU[23223:c07] Testing strToEncode: Testing URLencode % "hmm" 
2012-11-03 06:44:02.141 FoodyU[23223:c07] Testing strToEncode: Testing URLencode % "hmm" 
2012-11-03 06:44:02.142 FoodyU[23223:c07] Testing urlEncode: Testing%20URLencode%20%25%20%22hmm%22%20

尝试使用以下 JSONarray 不会:

2012-11-03 06:44:02.142 FoodyU[23223:c07] Testing strToEncode: (
        {
        dataMode = fastUpload;
        dateCreated = "373599360.708794";
        dateModified = "373599414.702938";
        dateSynced = "373632241.82217";
        entityName = CommodityTypes;
        myName = " Commodity Type";
        sortKey = 99;
        username = adamekPhoneDev;
        uuidKey = "338A0507-355F-4DF5-97A4-C0F1FF651D6F";
    },
        {
        dataMode = fastUpload;
        dateCreated = "373599366.851905";
        dateModified = "373599382.473983";
        dateSynced = "373632241.82217";
        entityName = CommodityTypes;
        myName = "Anoth Type";
        sortKey = 90;
        username = adamekPhoneDev;
        uuidKey = "6C64E7C6-4C57-4DFC-BECA-D2AB2339E204";
    }
)
2012-11-03 06:44:02.143 FoodyU[23223:c07] -[__NSArrayM length]: unrecognized selector sent to instance 0x9946ae0
2012-11-03 06:44:02.145 FoodyU[23223:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x9946ae0'
*** First throw call stack:
(0x1ffb012 0x1690e7e 0x20864bd 0x1feabbc 0x1fea94e 0x1f76090 0x1feba1d 0x284d8 0x28a52 0x251cb 0x21c18 0x219b5 0x6878d5 0x687b3d 0x108ee83 0x1fba376 0x1fb9e06 0x1fa1a82 0x1fa0f44 0x1fa0e1b 0x24267e3 0x2426668 0x5d865c 0x224d 0x2175)
libc++abi.dylib: terminate called throwing an exception

在转换工作之前记录字符串;它是一个格式良好的 JSON 字符串。然后它从内部某处崩溃并显示该错误消息NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)strToEncode, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8));

帮助?

4

2 回答 2

0

[__NSArrayM length]: unrecognized selector sent to instance 0x9946ae0

你把 a放在预期的NSArray地方。NSString最好先将 JSON 数据序列化为字符串。

无论如何,您为什么要对 JSON 数据进行 URL 编码?

于 2012-11-03T11:16:36.530 回答
0

尝试使用这个

NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                        NULL,
                        (CFStringRef)unencodedString,
                        NULL,
                        (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                        kCFStringEncodingUTF8 );
于 2012-11-03T11:22:40.713 回答