Frage

I have following NSObject:

@interface finalObject : NSObject

@property NSString *name;
@property NSString *image;

which i'm declaring like this in my ViewController:

finalArray = [[NSMutableArray alloc] init];
object = [finalObject new];

Then i'm looping thename and the image into object.name and object.image and add this object to NSMutableArray called finalArray.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    if ([elementName isEqualToString:@"language"]) {
        thename = attributeDict[@"name"];
        theimage = attributeDict[@"image"];
        theId = attributeDict[@"id"];



        object.name = thename;
        object.image = theimage;

        NSArray *cols = @[thename, theimage, theId ];

        [finalArray addObject:object];

        [rows addObject: cols];
    }

now i need my searchbar.text to search through the finalarray, which i'm doing with this method:

-(void) searchThroughdata {

    self.filteredArray = nil;

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@",self.searchBar.text];

    self.filteredArray = [[finalArray filteredArrayUsingPredicate:predicate] mutableCopy];

}

The problem is that finalArray in:

[[finalArray filteredArrayUsingPredicate:predicate] 

should only contain the name and not the image. How can i do this?

at the moment i get this error:

'NSInvalidArgumentException', reason: 'Can't use in/contains operator with collection
<finalObject: 0x8c4f910> (not a collection)'

i've tried adding SELF.name to the "SELF contains [cd] %@"

War es hilfreich?

Lösung

The problem is that finalArray ... should only contain the name and not the image

You cannot do that with a predicate. You can filter the array on the "name" property of its elements:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains [cd] %@",self.searchBar.text];
NSArray *filteredArray = [finalArray filteredArrayUsingPredicate:predicate];

and then, if necessary, create a new array containing only the names of the objects:

NSArray *filteredNames = [filteredArray valueForKey:@"name"];

Note also, as already commented above, your code creates only a single object, which is repeatedly modified and added to the array. Since an array holds pointers to its elements, you will end with an array of identical objects.

The creation should be moved into

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    if ([elementName isEqualToString:@"language"]) {
        thename = attributeDict[@"name"];
        theimage = attributeDict[@"image"];
        theId = attributeDict[@"id"];

        FinalObject *object = [FinalObject new];
        object.name = thename;
        object.image = theimage;
        [finalArray addObject:object];

        // ...
    }
}

Update: If I understand your comment correctly, you should not create an array containing the names only. Just create the filtered array as

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains [cd] %@",self.searchBar.text];
NSArray *filteredArray = [finalArray filteredArrayUsingPredicate:predicate];

Then, in cellForRowAtIndexPath, you access the objects properties, for example:

FinalObject *object = filteredArray[indexPath.row];
cell.textLabel.text = object.name;
// ... do something with object.image ...
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top