字符串在Objective-C比较数组 [英] Comparing a string to an array in objective-C

查看:104
本文介绍了字符串在Objective-C比较数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,
这里有一个很基本的问题,我敢肯定,你将能够快速回答。请不要在我的无知嘲笑。

我有一个字符串,我想比较字符串数组。只有当字符串不是阵列的一部分,我想执行的操作。我尝试以下code,这是行不通的。我不明白为什么,但我不能想办法正确地做到这一点。

请帮我出我的痛苦。

在此先感谢

Sjakelien

   - (无效)findRedundant:(* NSString的){ASTRING
#定义ALPHA_ARRAY [NSArray的arrayWithObjects:@A,@B,@C,零]
    NSUInteger F;
    为(F = 0; F&下; [ALPHA_ARRAY计数];˚F++)
    {
    * NSString的stringFromArray = [ALPHA_ARRAY objectAtIndex:F]
    如果([ASTRING isEqualToString:stringFromArray]){
    \t\t// 没做什么    }其他{
    \t\t//做一点事
    }    }}[个体经营findRedundant:@D];


解决方案

您code能够正常工作。它的可怕code,但它工作正常,则//什么也不做部分所要求的任何比赛和//做一些部分称为数组中的每个不匹配。我怀疑的问题是,你所期望的//什么也不做部分,如果没有匹配被执行一次,//做一些部分,如果有任何的比赛,这是不将案件执行一次。你可能想:

   - (无效)findRedundant:(* NSString的){ASTRING
#定义ALPHA_ARRAY [NSArray的arrayWithObjects:@A,@B,@C,零]
    BOOL发现= NO;
    NSUInteger F;
    为(F = 0; F&下; [ALPHA_ARRAY计数];˚F++){
        * NSString的stringFromArray = [ALPHA_ARRAY objectAtIndex:F]
        如果([ASTRING isEqualToString:stringFromArray]){
            找到= YES;
            打破;
        }
    }
    如果(找到){
        //你发现了
    }其他{
        //没有找到
    }
}

此外,您显然不明白宏,当你应该和不应该使用它们(一般,你永远不应该使用它们,除了极少数例外)。宏文本上取代的到您的code。这意味着,数组创建和初始化发生的每次您使用ALPHA_ARRAY。这是可怕的。

基本上不会再使用的#define(除了常数),直到你有你在做什么更深刻的把握。在这种情况下,你会为​​taebot描述创建数组:

 的NSArray * alphaArray = [NSArray的arrayWithObjects:@A,@B,@C,零]

接下来,如果你正在开发一个现代的平台(10.5或iPhone),您可以使用快速计数是更容易和更清晰的阅读:

   - (无效)findRedundant:(* NSString的){ASTRING
    NSArray的* alphaArray = [NSArray的arrayWithObjects:@A,@B,@C,零]
    BOOL发现= NO;
    对于(的NSString * stringFromArray在alphaArray){
        如果([ASTRING isEqualToString:stringFromArray]){
            找到= YES;
            打破;
        }
    }
    如果(找到){
        //你发现了
    }其他{
        //没有找到
    }
}

最后,你应该去通过的NSArray和NSString的文档阅读,看看你可以免费做什么,然后你会发现像containsObject的方法KiwiBastard指出,你可以重写你的例行为:​​

   - (无效)findRedundant:(* NSString的){ASTRING
    NSArray的* alphaArray = [NSArray的arrayWithObjects:@A,@B,@C,零]
    如果([alphaArray containsObject:ASTRING]){
        //你发现了
    }其他{
        //没有找到
    }
}

Dear all, here's a very basic question, that I'm sure you will be able to answer quickly. Please don't laugh at my ignorance.

I have a string, that I want to compare to an array of strings. Only if the string is not part of the array, I want to perform an operation. I tried the following code, that doesn't work. I do understand why, but I just can't think of a way to do it correctly.

Please help me out of my misery.

Thanks in advance

Sjakelien

-(void) findRedundant: (NSString *) aString {
#define ALPHA_ARRAY [NSArray arrayWithObjects: @"A", @"B", @"C", nil]   
    NSUInteger f;
    for (f = 0; f < [ALPHA_ARRAY count]; f++) 
    {
    	NSString * stringFromArray = [ALPHA_ARRAY objectAtIndex:f];
    	if ([aString isEqualToString:stringFromArray]) {
    		// do nothing

    	} else {
    		//do something
    	}

    }

}



[self findRedundant:@"D"];

解决方案

Your code appears to work fine. Its terrible code, but it works fine, the // do nothing section is called for any match and the // do something section is called for each mismatch in the array. I suspect the problem is that you are expecting the // do nothing section to be executed once if there is no match, and // do something section to be executed once if there is any match, which is not the case. You probably want:

-(void) findRedundant: (NSString *) aString {
#define ALPHA_ARRAY [NSArray arrayWithObjects: @"A", @"B", @"C", nil]
    BOOL found = NO;
    NSUInteger f;
    for (f = 0; f < [ALPHA_ARRAY count]; f++) {
        NSString * stringFromArray = [ALPHA_ARRAY objectAtIndex:f];
        if ([aString isEqualToString:stringFromArray]) {
            found = YES;
            break;
        }
    }
    if ( found ) {
        // do found
    } else {
        // do not found
    }
}

Also, you clearly do not understand macros and when you should and should not use them (generally, you should never use them, with very few exceptions). The macro is textually substitued in to your code. That means the array creation and initialization is happening every time you use ALPHA_ARRAY. This is terrible.

Basically, never use #define again (except for constants) until you have a much deeper grasp of what you're doing. In this case, you would create the array as taebot described:

NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil];

Next, if you are developing for a modern platform (10.5 or iPhone), you can use Fast Enumeration which is much easier and clearer to read:

-(void) findRedundant: (NSString *) aString {
    NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil];
    BOOL found = NO;
    for ( NSString* stringFromArray in alphaArray ) {
        if ([aString isEqualToString:stringFromArray]) {
            found = YES;
            break;
        }
    }
    if ( found ) {
        // do found
    } else {
        // do not found
    }
}

And finally, you should go read through the documentation on NSArray and NSString to see what you can do for free, and then you'll find methods like containsObject that KiwiBastard pointed out, and you can rewrite your routine as:

-(void) findRedundant: (NSString *) aString {
    NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil];
    if ( [alphaArray containsObject: aString] ) {
        // do found
    } else {
        // do not found
    }
}

这篇关于字符串在Objective-C比较数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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