如何将nsarray与整数进行比较

时间:2012-11-05 07:24:05

标签: ios xcode core-data

我试图将nsarray的值与整数进行比较。 nsstring“1dog”被插入到数组字典中。 Xcode拒绝让我比较数组。我该如何处理?

   if (dog == 1) {
        [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:@"doggy"]) {
        [dog addObject:[[NSDictionary alloc] initWithDictionary:attributeDict]];
    }
}

doggy是来自SQL查询的多个值的结果,例如:

$e = $doc->createElement( "doggy" );
    $e->setAttribute( '1', $row['1'] );

2 个答案:

答案 0 :(得分:0)

myArray = [NSMutableArray array];
[myArray addObject:[NSNumber numberWithInteger:1234]];

//..

int theNumber = [[myArray objectAtIndex:0] integerValue];

答案 1 :(得分:0)

根据我的理解,您要检查的是,您有'dog'数组,其中插入了NSString "1dog"。您希望使用条件检查此条件'dog'数组中是否有以1开头的值。

NSString *beginsWithRequirement = [NSString stringWithFormat:@"%d", 1];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] %@", beginsWithRequirement];
NSArray *subArray = [dog filteredArrayUsingPredicate:predicate];
[switch1 setOn:([subArray count] > 0) animated:YES];