gpt4 book ai didi

ios - 如何缩短可能包括的 NSString HFS+ 文件名允许的表情符号最大长度

转载 作者:行者123 更新时间:2023-12-03 16:56:52 25 4
gpt4 key购买 nike

苹果文档说:

[...] current file systems such as HFS+ (used by Mac OS X) allow you to create filenames with a 255-character limit [...] symbols may actually take up to the equivalent of nine English characters to store [...] This should be considered when attempting to create longer names.

如何限制 NSString 的长度,使其真正短于 255 个字符,即使它包含可能需要多个字符来存储的符号?

我在下面添加了我当前的实现。如果我添加例如表情符号到字符串中,虽然 length 回答结果字符串将远小于 255,但它仍然太长,无法被 NSSavePanel 接受作为文件名。

NSRange stringRange = {0, MIN([fileName length], 255)};
stringRange = [fileName rangeOfComposedCharacterSequencesForRange:stringRange];
fileName = [fileName substringWithRange:stringRange];

最佳答案

根据@JoshCaswell的建议,我确实修改了this answer类似的问题。它显然确实有效(我写了几个测试),但对我来说似乎很奇怪。这么明显的任务不可能这么复杂地实现吧?

// filename contains the NSString that should be shortened
NSMutableString *truncatedString = [NSMutableString string];
NSUInteger bytesRead = 0;
NSUInteger charIdx = 0;

while (bytesRead < 250 && charIdx < [fileName length])
{
NSRange range = [fileName rangeOfComposedCharacterSequencesForRange:NSMakeRange(charIdx, 1)];
NSString *character = [fileName substringWithRange:NSMakeRange(charIdx, range.length)];
bytesRead += [character lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
charIdx = charIdx + range.length;
if (bytesRead <= 250)
[truncatedString appendString:character];
}

关于ios - 如何缩短可能包括的 NSString HFS+ 文件名允许的表情符号最大长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31733100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com