2012-11-03 62 views
0

我有一個簡短的URL編碼NSStrings的方法。urlEncoding在iOS上失敗JSON

- (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));

幫助?

回答

0

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

你把一個NSArray其中NSString預期。首先將JSON數據序列化爲字符串是一個好主意。

爲什麼你的URL編碼的JSON數據,無論如何?

+0

首先,我只是回來這裏說哎呀我沒有序列化的陣列還和你打我吧。其次,我發佈的Web應用使用x-www-form-urlencoded,JSON數據是表單的一個字段。 – adamek

0

嘗試使用此

NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
         NULL, 
         (CFStringRef)unencodedString, 
         NULL, 
         (CFStringRef)@"!*'();:@&=+$,/?%#[]", 
         kCFStringEncodingUTF8);