【问题标题】:stringByAddingPercentEscapesUsingEncoding adds unexpected characters?stringByAddingPercentEscapesUsingEncoding 添加了意外字符?
【发布时间】:2013-05-06 21:37:11
【问题描述】:

我很难让我的NSURL 工作,当我在转换为URL 之前创建最终字符串时,它会在字符串末尾添加不需要的字符,为什么会发生这种情况以及我该如何解决是吗?

这是我的代码:

NSString *remotepathstring = [[NSString alloc] init];
remotepathstring=newdata.remotepath;
NSLog(@"remotepathstring = %@",remotepathstring);

NSString *remotepathstringwithescapes = [remotepathstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"remotepathstring = %@",remotepathstringwithescapes);

remotepathURL =[NSURL URLWithString:remotepathstringwithescapes];
NSLog(@"RemotePathUrl=%@",remotepathURL);

日志输出如下:

"remotepathstring = http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf‎"

"remotepathstring = http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf%E2%80%8E"

"RemotePathUrl=http://nalahandthepinktiger.com/wp-content/uploads/nalah-sheet-5.pdf%E2%80%8E"

【问题讨论】:

    标签: nsstring nsurl nsstringencoding


    【解决方案1】:

    序列%E2%80%8E 是一个Unicode LEFT-TO-RIGHT MARK。这存在于您原来的remotepathstring 中,但在通过NSLog 打印出来时不可见。

    问题变成了:newdata.remotepath 首先是如何填充的?在某个地方,听起来您需要对输入字符串进行一些额外的清理以去除这样的字符。

    与核心问题无关,您似乎是 Objective-C 的新手。这段代码是多余和浪费的:

    NSString *remotepathstring = [[NSString alloc] init];
    remotepathstring=newdata.remotepath;
    

    您创建了一个字符串,只是立即将其丢弃并用另一个替换。如果您不使用 ARC,则会出现额外的泄漏问题!而是这样做:

    NSString *remotepathstring = newdata.remotepath;
    

    【讨论】:

    • newdata.remotepath 由 sqlite 数据库填充,如下所示:newdata.remotepath = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(st,1)];
    • 然后,您需要在这些字符串进入您的数据库或退出时对其进行清理
    • 有推荐的方法吗?当它们被检索到时,我可以做到。
    • 没有什么特别想到的。这不是我以前遇到过的事情,这表明您首先需要问自己这些字符串是如何进入您的数据库的
    猜你喜欢
    • 2016-08-04
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多