我有一套
NSMutableSet *set1 = [[NSMutableSet alloc ]init];
NSMutableSet *set2 = [[NSMutableSet alloc ]init];
我希望有一个可以用一些值“初始化”的函数。
喜欢(但不是工作):
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];
}
答案 0 :(得分:0)
需要将集合作为指针传递给指针。通过值传递常规指针,对set1
和set2
的修改不会更改从调用方传递给initSet
的值。
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];
}
按如下方式调用此函数:
NSMutableSet *s1, *s2;
initSet(&s1, &s2);