[英]How to compare string objects in objective-c?
Actually I want to populate an array to fill up my table view, but the problem is I have to compare two strings and if they match I wanted to add that string in the array. 实际上,我想填充一个数组以填充表格视图,但是问题是我必须比较两个字符串,如果它们匹配,我想将该字符串添加到数组中。 Here is my code:
这是我的代码:
for (NSDictionary *item in facilityZoneData) {
NSString *zoneFacilityID = [NSString stringWithFormat:@"%@",[item objectForKey:@"FacilityId"]];
if ([detailFacility isEqualToString:zoneFacilityID ]) {
NSLog(@"object added");
}
}
The detailFacility is NSString object which is declared in the header file. detailFacility是在头文件中声明的NSString对象。 The problem is it is not at all comparing the string.
问题是根本不比较字符串。 I know that there are surely some values which are equal.
我知道肯定有一些相等的值。 tell me if I am missing something
告诉我我是否想念一些东西
Your question is not clear. 您的问题不清楚。 To compare strings you use
比较您使用的字符串
NSString isEqualToString: NSString
Like you did. 像你一样。 Before your if-statement NSLog the detailFacility and zoneFacilityID objects to see what they contain.
在if语句之前,请先记录detailFacility和zoneFacilityID对象,以查看它们包含的内容。 Cheers
干杯
yea..i had the same problem a few days ago 是的..我几天前有同样的问题
i made a work-around. 我做了一个解决方法。 Do this:
做这个:
if ([detailFacility UTF8String] == [zoneFacilityID UTF8String] ){ ...}
for some reason nsstring compassion is comparing addresses and pointers...try this and tell me if it works 出于某种原因nsstring同情比较地址和指针...尝试一下并告诉我是否有效
i dont know why but that works for me :/ 我不知道为什么,但这对我有用:/
if it doesnt work..just use c function: 如果它不起作用..只需使用c函数:
if(!strcmp([detailFacility UTF8String],[zoneFacilityID UTF8String])){
<are equal>
}else{
<aren't equal>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.