2012-11-08 112 views
1

无论出于何种原因,我似乎无法弄清楚我无法让我的代码正确比较两个字符串。我有一个对象数组(配置文件包含一个字符串:组),我只是想删除这个配置文件对象,如果它的组不匹配一个字符串。比较字符串不工作 - 真的很奇怪

这是我有:

Profile对象.h文件中:

@property (nonatomic, copy) NSString *group; 

是翻翻阵列和倾倒出来的不正确的配置文件控制器文件 - 它的h文件:

@property (nonatomic, copy) NSString *buttonSelected; 

这*当用户所选择的按钮的代码简单地检查发送方标签,并分配适当的字符串像这样buttonSelected设置:

[controller setbuttonSelected:@"My Button Has Been Selected"]; 

问题是当当我尝试的比较这buttonSelected和阵列对象 - 这是与一组属性的简档对象:

_profileArray = [xmlParser profiles]; 

for(int i=0; i<[_profileArray count]; i++){ 
    NSLog(@"Comparing button: %@ and group: %@", [self buttonSelected], [[_profileArray objectAtIndex:i] group]); 

    if([[self buttonSelected] isEqualToString:[[_profileArray objectAtIndex:i] group]]) NSLog(@"Equal"); 
} 

的_profileArray具有3个对象,只有那些两个其组合实际上等于buttonSelected。所以我应该只有系统输出2“Equal”,但它从不输出“Equal”。

+1

尝试使用“比较”方法之一,因为isEqualToString也比较Unicode字符(因为我不知道如何填充字符串,它可能是问题) – giorashc

+0

我很高兴你提出这个应用程序读入时XML文件确实为字符串添加了一堆垃圾 - 我还没有弄清楚为什么 - 但是我确实需要清理干净的数据并将“干净”的数据重置回对象。我已经离开了小组,所以我回去补充了这一点。我仍然有同样的问题。 – CodeMoto

+0

看起来好像即使通过执行此操作清除Profile属性之后: NSString * cleanGroup = [[_profileArray组] 配置文件信息仍然有\ n和一堆空白。为什么会继续这样呢? – CodeMoto

回答

0

你为什么不开始在更容易调试的部分打破你的代码,然后通过它。我会做这样的事情。

NSString *buttonSelectedToCompareAgainst = [self buttonSelected]; 
NSLog(@"button selected was: %@", buttonSelectedToCompareAgainst); 

NSString *profileGroupString; 
NSComparisonResult compareResult; 
for(Profile *profile in _profileArray){ 
NSLog(@"Checking profile: %@", profile); 

profileGroupString = [profile group]; 
NSLog(@"Checking profile group: %@", profileGroupString); 

compareResult = [buttonSelectedToCompareAgainst caseInsensitiveCompare:profileGroupString]; 

NSLog(@"compare result: %d", compareResult); 
} 
+0

compareResult返回为1 - 尽管我找不到这意味着什么。 – CodeMoto

+1

这意味着另一个字符串比比较字符串“大”。在这里检查NSComparisonResult:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/c_ref/NSComparisonResult – giorashc

0

想通了。我正在清理不同阵列中的空白等,而不是我用来比较字符串的那些空白空间,因此比较从未像他们应该那样出现。清洁阵列后,我使用的一切工作正常。