문제

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];
}
도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top