2015-01-13 72 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]; 
相关问题