Wongzigii Wongzigii - 7 months ago 19
Swift Question

Variadic method in Swift

Objective C Code:

- (instancetype)initWithInts:(int32_t)int1, ... {
va_list args;
va_start(args, int1);
unsigned int length = 0;
for (int32_t i = int1; i != -1; i = va_arg(args, int)) {
length ++;
}
va_end(args);
...
...
return self;
}


This code is used to count the numbers of method's parameters.

Swift Code:

convenience init(ints: Int32, _ args: CVarArgType...) {
var length: UInt = 0
self.init(length: args.count)
withVaList(args, { _ in
// How to increase length' value in loop?
})
}


What's the best practise to use
withVaList
to loop through the argument list with a
CVaListPointer
? Help is very appreciated.

Answer

How about just

convenience init(args: Int...) {
  return args.count
}
Comments