Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of NSSet's objectsPassingTest function please?

I'm going nuts here. For some reason I can't find a single, complete example of how to use the objectsPassingTest: function of NSSet (cocoa). Before anyone points me to the pages about blocks, I've seen those. The example given only shows how to declare the function, not the block that operates it, and when I tried their example with my own code it didn't work. I just want to see a couple of working examples of how the function might be used, then I'll be able to work it out for myself.

like image 293
Ash Avatar asked Mar 01 '11 08:03

Ash


People also ask

What are nested objects?

Nested objects are the objects that are inside an another object. In the following example 'vehicles' is a object which is inside a main object called 'person'. Using dot notation the nested objects' property(car) is accessed.

How do I access nested objects?

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation.

How do you call nested objects in react?

The structure of an object in ReactJS can be nested many times and can get complicated quickly. If we want to access all the values of nested objects then we have to use recursion to access each and every level of that object. And it can get more complicated according to the nesting of the object.

How do I create a nested object in JavaScript?

const obj = { code: "AA", sub: { code: "BB", sub: { code: "CC", sub: { code: "DD", sub: { code: "EE", sub: {} } } } } }; Notice that for each unique couple in the string we have a new sub object and the code property at any level represents a specific couple. We can solve this problem using a recursive approach.


2 Answers

Here is a quick example. Hope it helps.

    NSSet *set = [NSSet setWithObjects:@"1",@"2",@"3",@"4",@"5",nil];

    NSLog(@"%@",set); // Output (3,1,4,2,5) ... all objects

    NSSet *o = [set objectsPassingTest:^(id obj,BOOL *stop){
        NSString *so = (NSString *)obj;
        int intval = [so intValue];
        // accept objects less or equal to two
        BOOL r = (intval <= 2);
        return r;
    }];

    NSLog(@"%@",o); // Output (1,2) only objects smaller or equal  to 2
like image 148
fsaint Avatar answered Sep 27 '22 04:09

fsaint


I never used blocks. But I guess this is how it works.

NSSet *set = [NSSet setWithObjects:@"FooBar", @"Foo", @"Bar", @"Baz", nil];

NSSet *fooSet = [set objectsPassingTest:^(id obj, BOOL *stop) {
    BOOL testResult = NO;
    NSString *objStr = (NSString *)obj;
    if ([objStr hasPrefix:@"Foo"]) {
        testResult = YES;
    }
    if ([objStr hasSuffix:@"Bar"]) {
        testResult = YES;
    }
    return testResult;
}];

This will create a set with @"FooBar", @"Foo" and @"Bar", because only those pass the test (ie return YES).

like image 4
Matthias Bauch Avatar answered Sep 26 '22 04:09

Matthias Bauch