Objective c 如何在目标C中设置转义序列?
我有一个小代码:Objective c 如何在目标C中设置转义序列?,objective-c,cocoa-touch,nsstring,Objective C,Cocoa Touch,Nsstring,我有一个小代码: NSString *size = [LabelNewContent stringByEvaluatingJavaScriptFromString:@"return Math.max( document.body.scrollHeight, document.documentElement.scrollHeight, document.body.offsetHeight, document.documentElement.offsetHeight, document.bo
NSString *size = [LabelNewContent stringByEvaluatingJavaScriptFromString:@"return Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
);"];
问题是xcode无法将括号和逗号作为一个完整的字符串来识别。使用
\
将其设置为多行字符串
NSString *size = [LabelNewContent stringByEvaluatingJavaScriptFromString:@"return Math.max(\
document.body.scrollHeight, document.documentElement.scrollHeight,\
document.body.offsetHeight, document.documentElement.offsetHeight,\
document.body.clientHeight, document.documentElement.clientHeight\
);"];
使用
\
使其成为多行字符串
NSString *size = [LabelNewContent stringByEvaluatingJavaScriptFromString:@"return Math.max(\
document.body.scrollHeight, document.documentElement.scrollHeight,\
document.body.offsetHeight, document.documentElement.offsetHeight,\
document.body.clientHeight, document.documentElement.clientHeight\
);"];
试试上面的事
尝试上述操作将代码替换为
NSString *size = [LabelNewContent stringByEvaluatingJavaScriptFromString:@"return %f",Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
)];
希望它能帮助您。将您的代码替换为
NSString *size = [LabelNewContent stringByEvaluatingJavaScriptFromString:@"return %f",Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
)];
希望它能对您有所帮助。原因:如果不转义回车符,就无法将字符串文字包装到多行上。原因:如果不转义回车符,就无法将字符串文字包装到多行上。这是不正确的
Math.max()
、document.body.etc
和document.documentElement.etc
都是JavaScript,因此应该在字符串中。是正确的。这是错误的Math.max()
、document.body.etc
和document.documentElement.etc
都是JavaScript,因此应该在字符串中。是正确的。