NSCharacter Set使用int,但我需要未分配的短片? [英] NSCharacter Set uses int's but i need unassigned short?

查看:236
本文介绍了NSCharacter Set使用int,但我需要未分配的短片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MWFeedParser 向我的应用添加Feed。现在框架传递了日期,我有一些警告,主要是由于代码类型较旧。

I am using MWFeedParser to add a feed into my app. Now the framework passes date's and I it has a few warnings mainly due to older type of code.

现在剩下4个警告,这些都是相同的,技术上我可以修复它们并删除它们,以便警告消失,但随后我得知应用程序无法正常工作。

Now there are 4 warnings left which are all the same and technically I can fix them and remove them so that the warnings are gone, but then I get left with the app not working properly.

有关的代码是:

    // Character sets
NSCharacterSet *stopCharacters = [NSCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat:@"< \t\n\r%C%C%C%C", 0x0085, 0x000C, 0x2028, 0x2029]];

现在警告的位是:

\t\n\r%C%C%C%C", 0x0085, 0x000C, 0x2028, 0x2029]];

警告是:


格式指定类型' unsigned short'但参数的类型为'int'

Format specifies type 'unsigned short' but the argument has type 'int'

所以我换成了:

\t\n\r%i%i%i%i", 0x0085, 0x000C, 0x2028, 0x2029]];

确实删除了警告并给了我完美的代码:-)(没有警告或错误)

which indeed removed the warnings and gave me perfect code:-) (no warnings or errors)

当我运行应用程序时,它没有解析日期,也无法打开链接。
我不确定这是否是C的东西,但现在它肯定在我的知识领域之外。是否有人可以帮助我解决这个问题,并且仍然可以在应用程序中工作?

When I then ran the app it did not parse the date and it was not able to open the link. I am not sure if this a is C thing, but right now it is definitely outside of my knowledge field. Is there anyone who can help me that can fix this problem, and still have it working in the app??

提前谢谢: - )

     - (NSString *)stringByConvertingHTMLToPlainText {

// Pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// Character sets
NSCharacterSet *stopCharacters = [NSCharacterSet characterSetWithCharactersInString:@"< \t\n\r\x0085\x000C\u2028\u2029"];    
NSCharacterSet *newLineAndWhitespaceCharacters = [NSCharacterSet characterSetWithCharactersInString:@"< \t\n\r\205\014\u2028\u2029"];


NSCharacterSet *tagNameCharacters = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"];

// Scan and find all tags
NSMutableString *result = [[NSMutableString alloc] initWithCapacity:self.length];
NSScanner *scanner = [[NSScanner alloc] initWithString:self];
[scanner setCharactersToBeSkipped:nil];
[scanner setCaseSensitive:YES];
NSString *str = nil, *tagName = nil;
BOOL dontReplaceTagWithSpace = NO;
do {

    // Scan up to the start of a tag or whitespace
    if ([scanner scanUpToCharactersFromSet:stopCharacters intoString:&str]) {
        [result appendString:str];
        str = nil; // reset
    }

    // Check if we've stopped at a tag/comment or whitespace
    if ([scanner scanString:@"<" intoString:NULL]) {

        // Stopped at a comment or tag
        if ([scanner scanString:@"!--" intoString:NULL]) {

            // Comment
            [scanner scanUpToString:@"-->" intoString:NULL]; 
            [scanner scanString:@"-->" intoString:NULL];

        } else {

            // Tag - remove and replace with space unless it's
            // a closing inline tag then dont replace with a space
            if ([scanner scanString:@"/" intoString:NULL]) {

                // Closing tag - replace with space unless it's inline
                tagName = nil; dontReplaceTagWithSpace = NO;
                if ([scanner scanCharactersFromSet:tagNameCharacters intoString:&tagName]) {
                    tagName = [tagName lowercaseString];
                    dontReplaceTagWithSpace = ([tagName isEqualToString:@"a"] ||
                                               [tagName isEqualToString:@"b"] ||
                                               [tagName isEqualToString:@"i"] ||
                                               [tagName isEqualToString:@"q"] ||
                                               [tagName isEqualToString:@"span"] ||
                                               [tagName isEqualToString:@"em"] ||
                                               [tagName isEqualToString:@"strong"] ||
                                               [tagName isEqualToString:@"cite"] ||
                                               [tagName isEqualToString:@"abbr"] ||
                                               [tagName isEqualToString:@"acronym"] ||
                                               [tagName isEqualToString:@"label"]);
                }

                // Replace tag with string unless it was an inline
                if (!dontReplaceTagWithSpace && result.length > 0 && ![scanner isAtEnd]) [result appendString:@" "];

            }

            // Scan past tag
            [scanner scanUpToString:@">" intoString:NULL];
            [scanner scanString:@">" intoString:NULL];

        }

    } else {

        // Stopped at whitespace - replace all whitespace and newlines with a space
        if ([scanner scanCharactersFromSet:newLineAndWhitespaceCharacters intoString:NULL]) {
            if (result.length > 0 && ![scanner isAtEnd]) [result appendString:@" "]; // Dont append space to beginning or end of result
        }

    }

} while (![scanner isAtEnd]);

// Cleanup
[scanner release];

// Decode HTML entities and return
NSString *retString = [[result stringByDecodingHTMLEntities] retain];
[result release];

// Drain
[pool drain];

// Return
return [retString autorelease];

}

推荐答案

这是一个完全混乱



这是一个完全混乱的原因是因为你遇到编译器错误 C规范中的任意限制。

This is a total mess

The reason this is a total mess is because you are running into a compiler bug and an arbitrary limitation in the C spec.

滚动到底部进行修复。


格式指定类型'unsigned short'但参数的类型为'int'

Format specifies type 'unsigned short' but the argument has type 'int'

我的结论是这是Clang中的编译器错误。忽略此警告肯定安全,因为(unsigned short)参数始终提升为(int)然后才传递给vararg函数。这是C标准中的所有内容(它也适用于Objective C)。

My conclusion is that this is a compiler bug in Clang. It is definitely safe to ignore this warning, because (unsigned short) arguments are always promoted to (int) before they are passed to vararg functions anyway. This is all stuff that is in the C standard (and it applies to Objective C, too).

printf("%hd", 1); // Clang generates warning. GCC does not.
                  // Clang is wrong, GCC is right.

printf("%hd", 1 << 16); // Clang generates warning.  GCC does not.
                        // Clang is right, GCC is wrong.

这里的问题是编译器看起来都不够深。

The problem here is that neither compiler looks deep enough.

请记住,实际上不可能将传递给 printf(),因为它必须升级到 int 。 GCC从不对常量发出警告,Clang忽略了这样一个事实,即你传递一个常量并且总是发出警告,因为类型错误。这两个选项都是错误的。

Remember, it is actually impossible to pass a short to printf(), because it must get promoted to int. GCC never gives a warning for constants, Clang ignores the fact that you are passing a constant and always gives a warning because the type is wrong. Both options are wrong.

我怀疑没人注意到 - 因为你为什么要将常量表达式传递给 printf()无论如何?

I suspect nobody has noticed because -- why would you be passing a constant expression to printf() anyway?

在短期内,您可以使用以下黑客:

In the short term, you can use the following hack:

#pragma GCC diagnostic ignored "-Wformat"



通用字符名称



您可以使用 \ uXXXX 表示法。除非您不能,因为编译器不会让您以这种方式使用 U + 0085 。为什么?参见C99第6.4.3节:

Universal character names

You can use \uXXXX notation. Except you can't, because the compiler won't let you use U+0085 this way. Why? See § 6.4.3 of C99:


通用字符名称不应指定短标识符小于 00A0 除了 0024 $ ), 0040 @ )或 0060 '),也不在 D800 DFFF 的范围内。

A universal character name shall not specify a character whose short identifier is less than 00A0 other than 0024 ($), 0040 (@), or 0060 (), nor one in the range D800 through DFFF inclusive.

这排除了 \ u0085

< a href =http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2170.html\"rel =noreferrer>有一项建议要修复规范的这一部分。

你真的想要一个常量字符串,不是吗?使用此:

You really want a constant string, don't you? Use this:

[NSCharacterSet characterSetWithCharactersInString:
  @"\t\n\r\xc2\x85\x0c\u2028\u2029"]

这取决于来源这一事实编码是UTF-8。别担心,这不会很快改变。

This relies on the fact that the source encoding is UTF-8. Don't worry, that's not going to change any time soon.

\ xc2 \ x85 in字符串是 U + 0085 的UTF-8编码。两者中出现 85 是巧合。

The \xc2\x85 in the string is the UTF-8 encoding of U+0085. The appearance of 85 in both is a coincidence.

这篇关于NSCharacter Set使用int,但我需要未分配的短片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆