va_list、CVaListPointer、AnyObject ...、CVarArgType 的区别和用例是什么? [英] What are the difference and use-cases for va_list, CVaListPointer, AnyObject ..., CVarArgType?

查看:53
本文介绍了va_list、CVaListPointer、AnyObject ...、CVarArgType 的区别和用例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

有人可以解释这些参数类型之间的区别吗?此外,如果可能,请使用代码提供适当的用例(值得 1000 字).

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).

注意

如果对更多信息有任何要求,请在评论中告诉我.

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

背景

我试图了解以下结构之间的任何差异并了解适当的用例(如果有示例,请附上示例).我搜索过 SO、Google 等.(博客圈)没有找到满意的答案.

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.

在编写数据存储对象时,我遇到了以下 NSPredicate 初始化器:

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

Swift 头文件 (1.2) 注释:

The Swift header file (1.2) notes:

一篇博客评论指出以下 va_list 在 Objective-C 中的用法

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 是一个协议,可以根据 Swift 头文件如下使用.

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

在我的自定义类中,我有以下用例:

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]
    */

读者请注意,argumentArray 参数没有按照我的自定义类中的描述使用.要正确使用此初始化程序,请注意 Apple 文档中的以下内容:

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:

这让我想到了这种用法:

Which leads me to this usage:

NSPredicate(format: "(%K != %@ AND %K = %@)", argumentArray: ["key1","value1","key2","value2"]) 

推荐答案

va_list 是用于可变参数函数的 C 类型.您将在 Objective-C 代码中将其视为参数.

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

CVaListPointerva_list 的 swift 等价物——只要你在 Objective-C 中看到一个以 va_list 作为参数的函数,你将在 Swift 中传入一个 CVaListPointer.

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 是一个协议,它定义了CVaListPointer 中可以包含哪些类型,这包括所有原语(IntFloat, UInt, etc) 以及 COpaquePointer

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

实用函数 withVaList 接受一个 Swift 数组并将其转换为 CValListPointer,然后将其传递给回调.注意传入的数组必须只包含CVarArgType变量:

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 还定义了自己的可变参数 T...,它必须是函数中的最后一个参数,它作为 [T] 传递给函数,并且像这样使用.

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)

尽可能使用 Swift 内置的可变参数 T....CValListPointer 仅在您需要与接受 va_list 参数的 Objective C 和 C API 接口时使用.

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.

这篇关于va_list、CVaListPointer、AnyObject ...、CVarArgType 的区别和用例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆