Passing NSSet and initialize it with values

958 Views Asked by At

in main function I have a set

    NSMutableSet *set1 = [[NSMutableSet alloc ]init];
    NSMutableSet *set2 = [[NSMutableSet alloc ]init];

and I want to have a functions that can "initialize" with some values.

Like(but not work) :

void initSet (NSMutableSet *set1, NSMutableSet *set2)
{
    NSArray *a1 = [NSArray arrayWithObjects: intNum(1), intNum(2), nil];
    NSArray *a2 = [NSArray arrayWithObjects:intNum(3), intNum(4), intNum(5), intNum(6), intNum(7), nil];

    set1 = [NSMutableSet setWithArray: a1];
    set2 = [NSMutableSet setWithArray: a2];
}
1

There are 1 best solutions below

0
On BEST ANSWER

The sets need to be passed as pointers to pointers. Secause regular pointers are passed by value, modifications to set1 and set2 do not change the values that you pass to initSet from the caller.

void initSet (NSMutableSet **set1, NSMutableSet **set2)
{
    NSArray *a1 = [NSArray arrayWithObjects: intNum(1), intNum(2), nil];
    NSArray *a2 = [NSArray arrayWithObjects:intNum(3), intNum(4), intNum(5), intNum(6), intNum(7), nil];

    *set1 = [NSMutableSet setWithArray: a1];
    *set2 = [NSMutableSet setWithArray: a2];
}

Call this function as follows:

NSMutableSet *s1, *s2;
initSet(&s1, &s2);