使用isEqualToString的问题:iOS

时间:2014-07-22 00:56:47

标签: ios objective-c plist unrecognized-selector

我试图获取一个来自plist的数组,并使用这些字符串来了解它们是否已经存在于上下文中。

但是这个错误正在发生

  

原因:' - [__ NSCFArray isEqualToString:]:无法识别的选择器发送到实例

这是代码..

- (NSArray*)loadPlist
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"AlreadyDownloaded" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSArray *plistArray = [[NSMutableArray alloc] initWithArray:[dict allValues]];

    return plistArray;
}


-(void)writeToList:(NSArray *)text
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"AlreadyDownloaded" ofType:@"plist"];
    NSArray *copy = [self loadPlist];
    NSMutableArray *list = [copy mutableCopy];



    [list addObjectsFromArray:text];

    NSArray *array  = [[NSArray alloc] initWithObjects:@"ObjectsIDS", nil];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

    for(NSString *ob in list)
    {

        [dict setValue:ob forKey:@"ObjectsIDS"];

    }
    [dict writeToFile:path atomically:YES];

}

-(bool)compareIDS:(NSString *)text
{
    NSArray *copy = [[NSArray alloc]initWithArray:[self loadPlist]];
    NSMutableArray *list = [copy mutableCopy];
    NSMutableString *st = [[NSMutableString alloc]init];


    for(NSString *ob in copy)
    {

        if([ob isEqualToString:text])
            return YES;


    }
    return NO;
}

谢谢你们!

最诚挚的问候。

2 个答案:

答案 0 :(得分:0)

从崩溃日志中我们怀疑NSString * ob是一个数组,而不是NSString,你最好设置一个断点为(NSString * ob in copy),po是对象ob。

答案 1 :(得分:-1)

使用

NSArray indexOfObject方法:

return [copy indexOfObject:text] != NSNotFound

for(int i = 0; i < copy.count; i++) {
     if([(NSString *) [copy objectAtIndex:i] isEqualToString:text]) return YES; 
}
return NO;