2015-01-13 108 views
0

我有一個用於post方法的字符串。如果字符串像"hello.."比值傳遞方法。但是如果字符串比"hello world"比不採取郵寄方式。如果我在字符串中使用空格,那麼它不起作用。請幫助我。如何在字符串ios中添加空白空間

- (void) constructURL 
{ 
    NSMutableString* curl = [[NSMutableString alloc] init]; 

    [curl appendString:@"XXXXX"]; 
    [curl appendString:[NSString stringWithFormat:@"?eventid=%@", eventID]]; 
    [curl appendString:[NSString stringWithFormat:@"&ForumID=%@", forumID]]; 
    [curl appendString:[NSString stringWithFormat:@"&parentPostID=%@", parentPostID]]; 
    [curl appendString:[NSString stringWithFormat:@"&userid=%@", userID]]; 
    [curl appendString:[NSString stringWithFormat:@"&PostContent=%@", postContent]]; 

    url = [NSURL URLWithString:curl]; 
} 
+0

使用stringByAddingPercentEscapesUsingEncoding上的[無法創建有效的NSURL]串 – the1pawan

+0

可能重複(http://stackoverflow.com/questions/ 4637799/not-able-to-create-valid-nsurl) – Larme

回答

0

閱讀這些線,可能會對你有所幫助。從蘋果中提取的文檔https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/

Working with URLs 
stringByAddingPercentEscapesUsingEncoding: 
Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. 

Declaration 
SWIFT 
func stringByAddingPercentEscapesUsingEncoding(_ encoding: UInt) -> String? 
OBJECTIVE-C 
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding 
Parameters 
encoding  
The encoding to use for the returned string. If you are uncertain of the correct encoding you should use NSUTF8StringEncoding. 
Return Value 
A representation of the receiver using encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. Returns nil if encoding cannot encode a particular character. 

Discussion 
It may be difficult to use this function to "clean up" unescaped or partially escaped URL strings where sequences are unpredictable. See CFURLCreateStringByAddingPercentEscapes for more information. 

Import Statement 
import Foundation 

Availability 
Available in OS X v10.3 and later. 
See Also 
– stringByAddingPercentEncodingWithAllowedCharacters: 
– stringByReplacingPercentEscapesUsingEncoding: 
– stringByRemovingPercentEncoding 

你的問題的回答是:

- (void) constructURL 
{ 
    NSMutableString* curl = [[NSMutableString alloc] init]; 

    [curl appendString:@"XXXXX"]; 
    [curl appendString:[NSString stringWithFormat:@"?eventid=%@", eventID]]; 
    [curl appendString:[NSString stringWithFormat:@"&ForumID=%@", forumID]]; 
    [curl appendString:[NSString stringWithFormat:@"&parentPostID=%@", parentPostID]]; 
    [curl appendString:[NSString stringWithFormat:@"&userid=%@", userID]]; 
    [curl appendString:[NSString stringWithFormat:@"&PostContent=%@", postContent]]; 
    curl = [curl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    url = [NSURL URLWithString:curl]; 
} 
+0

非常感謝你的幫助。它工作得很好。 – user3110050

1

我想你需要到您的字符串編碼是這樣的:

NSString* encodedString = [@"Hello World" stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; 
相關問題