2012-11-03 5 views
0

URL 인코딩 NSString에 대한 간단한 방법이 있습니다.iOS에서 JSON으로 urlEncoding을 수행하지 못했습니다.

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

당신은 NSString가 예상되는 NSArray를 가하고 있습니다. 먼저 JSON 데이터를 문자열로 직렬화하는 것이 좋습니다.

왜 JSON 데이터를 URL 인코딩합니까?

+0

먼저 사용하고 당신은 나를 이겼다. 둘째, 내가 게시 할 웹 응용 프로그램은 x-www-form-urlencoded를 사용하고 JSON 데이터는 양식의 한 필드입니다. – adamek

0

봅니다 난 그냥 아직 배열을 직렬화하지 않았다 아차 말을 여기로오고,이

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