Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the difference and use-cases for va_list, CVaListPointer, AnyObject ..., CVarArgType?

Question

Can someone please explain the differences between these argument types? Furthermore, if possible please supply appropriate use-cases using code (it's worth a 1000 words).

Nota bene

Please let me know in the comments if there is any requirement for more information.

Background

I am attempting to understand any differences between the following constructs and understand the appropriate use-cases (with examples if there are any). I've searched SO, Google, et, al. (blogosphere) without finding a satisfactory answer.

In writing a data store object, I came across the following initializers for an NSPredicate:

NSPredicate-initializers

The Swift header file (1.2) notes:

CVaListPointer

A blog comment indicates the following usage of va_list in Objective-C

+ (void) log:(NSString *) format arguments:(va_list) argList
{
    [self logString: [NSString stringWithFormat: format arguments:
argList]];
}

+ (void) log:(NSString *) format, ...
{
    va_list argList;
    va_start(argList, format);
    {
  [self log: format arguments: argList];
    }
    va_end(argList);
}

CVarArgType is a protocol and can be used as follows according to the Swift header file.

CVarArgType

In my custom class I have the following use-case:

   /*  Finds an array of entity in the MOC if any exists using an optional
    *   predicate with an array of arguments
    *
    *   Usage:
    *   eg. var items = CustomEntity.findInStore(dbStore, predicate:
    *   NSPredicate(format: "(attributeName IN %@)", argumentArray:
    *   ["value1","value2","value3"])) as? [CustomEntity]
    */

EDIT:

Readers please note that the argumentArray parameter was incorrectly used as described in my custom class. To correctly use this initializer, note the following from Apple's documentation:

argumentArray-doc

Which leads me to this usage:

NSPredicate(format: "(%K != %@ AND %K = %@)", argumentArray: ["key1","value1","key2","value2"]) 
like image 369
Tommie C. Avatar asked Feb 20 '15 22:02

Tommie C.


1 Answers

va_list is a C type used for variable argument functions. You'll see this as a parameter in Objective-C code.

CVaListPointer is the swift equivalent of a va_list--wherever you see a function in Objective-C that takes va_list as an argument, you'll pass in a CVaListPointer in Swift.

objective-c: (NSPredicate *)predicateWithFormat:(NSString *)format arguments:(va_list)argList
swift: init(format format: String, arguments argList: CVaListPointer) -> NSPredicate

CVarArgType is a protocol that defines what kind of types can be included in a CVaListPointer, this includes all the primitives (Int, Float, UInt, etc) as well as COpaquePointer

The utility function withVaList takes a Swift array and transforms it into a CValListPointer, which is then passed to a callback. Note that the array passed in must contain only CVarArgType variables:

var format = "%@ != %@"
var args: [CVarArgType] = ["abc", "def"]

var s = withVaList(args) { (pointer: CVaListPointer) -> NSPredicate in
    return NSPredicate(format: format, arguments: pointer)
}

Swift also defines its own varadic parameter T..., which must be the last parameter in a function, it is passed into the function as [T] and is used like so.

var arg1: String = "abc"
var arg2: String = "def"
NSPredicate(format: format, arg1, arg2)

Use Swift's built in varadic parameters T... wherever possible. CValListPointer should only be used when you need to interface with Objective C and C APIs that accept va_list arguments.

like image 169
kellanburket Avatar answered Nov 15 '22 18:11

kellanburket