将数组中的NSString与字符值(或整数)进行比较

时间:2012-11-06 03:14:39

标签: ios xcode core-data nsstring nsarray

我一直在尝试将xml返回的字符串“hey”与Xcode中的字符YES进行比较。但是,我似乎无法比较这两个值,因为objectForIndex方法崩溃了程序。我怎么能解决这个问题,以便我可以比较它们?

 -(void)viewDidLoad {     
     char *cStr = "YES";
     NSString *str3 = [NSString stringWithUTF8String:cStr];

     NSString *urlString = [NSString stringWithFormat:@"(censored)", _login];
     NSXMLParser *Parser = [[[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
     [Parser setDelegate:self];
     [Parser parse];

     if (([oneam objectForIndex:@"hey" == @"YES" ])) {
         [switch1 setOn:YES animated:YES];
     }
     else {
         [switch1 setOn:NO animated:YES];
     }
    }

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    if ( [elementName isEqualToString:@"location"]) {
        [oneam addObject:[[NSDictionary alloc] initWithDictionary:attributeDict]];
    }
}

1 个答案:

答案 0 :(得分:1)

if (([oneam objectForKey:@"hey" == @"YES" ])) {

这至少是一个问题。您不应该使用==来比较字符串。使用isEqualToString

如果您不确定从阵列中获取正确的内容,请将其分解并添加NSLog:

NSString* theObject = [oneam objectForKey:@"hey"];
NSLog(@"I got %@ out of the dictionary", theObject);
if ([theObject isEqualToString:@"YES"]) { ...