如何检查作为UDF参数的数组中的值数? [英] How do I check the number of values in an array that is a UDF parameter?

查看:99
本文介绍了如何检查作为UDF参数的数组中的值数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift的新手(大约一个月前就知道了),并且正在创建一个自定义UICollectionViewCell类,我还需要三种类型的UIButton和其他对象,但是我不想编写每一种出来,所以我要创建一个函数。但是,由于我有三个按钮,因此该函数必须为每个参数恰好包含三个数组。有人可以帮我提供包含注释的代码吗?谢谢!

I am new to Swift (I learned it about a month ago) and I'm creating a custom UICollectionViewCell class, and I need three types of UIButtons and other objects as well, but I don't want to write each one out, so I am creating a function. However, since I have three buttons, the function must take exactly three arrays for each parameter. Can someone please help me with the code where I have included the comments? Thank you!

var buttonArray: [UIButton] = []
var checkBoxArray: [UIView] = []
var labelArray: [UILabel] = []

func initializeStuff(objectType: AnyObject, xOriginCgFloat: [CGFloat], yOriginCgFloat: [CGFloat], width: [CGFloat], height: [CGFloat])  {

    if xOriginCgFloat != Array() || yOriginCgFloat != Array() || width != Array() || height != Array() {
        fatalError("Each CgFloat parameter must be an array.")
    } else {
        if AnyObject.self == UIButton.self {
            if condition { //condition: each array must contain exactly three values
                for index in 0..<3 {
                    var button = UIButton(frame: CGRect.init(origin: CGPoint.init(x: xOriginCgFloat[index], y: yOriginCgFloat[index]), size: CGSize.init(width: width[index], height: height[index])))

                    buttonArray.append(button)
                }
            } else {
                fatalError("Each array for a button object must have exactly three values.")
            }

        } else if AnyObject.self == UIView.self {
            if condition2 { //condition2: each array must contain exactly two values
                for index in 0..<2 {
                    var checkBox = UIView(frame: CGRect.init(origin: CGPoint.init(x: xOriginCgFloat[index], y: yOriginCgFloat[index]), size: CGSize.init(width: width[index], height: height[index])))

                    checkBoxArray.append(checkBox)
                }
            } else {
                fatalError("Each array for a view object must have exactly two values.")
            }

        } else if AnyObject.self == UILabel.self {
            if condition3 { //condition3: each array must contain exactly two values
                for index in 0..<2 {
                    var label = UILabel(frame: CGRect.init(origin: CGPoint.init(x: xOriginCgFloat[index], y: yOriginCgFloat[index]), size: CGSize.init(width: width[index], height: height[index])))

                    labelArray.append(label)
                }
            } else {
                fatalError("Each array for a view object must have exactly two values.")
            }

        } else {
            fatalError("Incorrect type of object; AnyObject parameter must be a UIButton, UIView, or UILabel.")


        }
    }
}


推荐答案

这里发生了很多事情,首先,您检查参数是否为数组的方式不会工作:

There's a lot going on here. First off, the way you're checking if your parameters are arrays won't work:

if xOriginCgFloat != Array() {

Array()声明一个类型为 Array 的新对象,并检查xOriginCgFloat是否等于它,而是要检查xOriginCgFloat是否为 Array 类型,可以这样做:

Array() declares a new object of type Array and checks if xOriginCgFloat is equal to it. Instead, you want to check if xOriginCgFloat is of the Array type, which you can do like this:

if xOriginCgFloat is Array {

但是!不需要这样做,您的函数声明中指定了 xOriginCgFloat:[CGFloat] ,这意味着要运行该函数, xOriginCgFloat 必须通过的值是 Array CGFloat s。无需检查。

But! you don't need to do this. Your function declaration specifies xOriginCgFloat: [CGFloat], which means that for the function to run, the value passed for xOriginCgFloat must be an Array of CGFloats. No need to check.

确定xt up:当您说

OK next up: when you say

if AnyObject.self == UIButton.self {

您可能认为您正在检查是否为 ObjectType 传递的值是 UIButton 。但是你不是。这实际上是在检查类 AnyObject 是否与类 UIButton 相同,不是。因此,它将始终返回false。相反,您可以像这样执行所需的检查:

you probably think you're checking if the value passed for ObjectType is UIButton. But you're not. That's actually checking if the class AnyObject is the same as the class UIButton, which it isn't. So that will always return false. Instead, you can perform the check you're looking for like this:

if objectType === UIButton.self {

如果您通过 UIButton.self 会成功 objectType 当您调用该函数时,我认为这是您的意图。

this will succeed if you pass UIButton.self for objectType when you call the function, which I assume is your intention.

最后,您的实际问题是:想知道您的 xOriginCgFloat 数组有多少个元素,可以使用 xOriginCgFloat.count

Lastly, your actual question: if you want to know how many elements your xOriginCgFloat array has, you can use xOriginCgFloat.count:

if xOriginCgFloat.count == 2 {

这可以工作,但是不是理想的方法,因为它不支持编译时检查。如果您在程序中的某个位置传递了错误数目的值的数组,则编译器将无法捕获它。在程序运行之前,也许是在非常糟糕的时候,您才知道。

This will work, but it's not an ideal approach, as it doesn't support compile-time checking; if somewhere in your program you pass an array with the wrong number of values, the compiler can't catch it. You won't find out until the program is running, and maybe at a very bad time.

相反,如果您知道要查找的值的数量,使用 tuple 。您可以这样声明一个函数:

Instead, if you know the number of values you're looking for, use a tuple. You can declare a function like this:

func initializeStuff(xOriginCgFloat: (CGFloat, CGFloat)) {

并这样称呼它:

initializeStuff(xOriginCgFloat: (5.0, 6.7))

它期望一个正好有两个 CGFloats 在内,因此,如果您尝试用其他任何方式调用,它将在IDE中显示错误。当您调用函数时,不需要检查,因为可以确保您确实要得到所需的值。

It expects a tuple with exactly two CGFloats inside, so it will show an error in the IDE if you try to call with anything else. When your function is called, you don't need to check as you're guaranteed exactly the values you wanted.

元组的缺点是您不能真正使用for循环对其进行迭代(实际上您可以,但这很麻烦),因此您必须手动执行操作。由于元组中只有两个或三个项目,所以这可能不是一个坏方法。例如:

The downside of tuples is that you can't really iterate them with a for loop, (actually you can, but it's messy) so you have to do things manually. Since you only have two or three items in your tuple this might not be a bad approach. For example:

let xOriginCgFloat:(CGFloat, CGFloat) = (4, 6)
var floats = [CGFloat]()
floats.append(xOriginCgFloat.0)
floats.append(xOriginCgFloat.1)
for float in floats {
    // process xOriginCgFloat member here
}

您可以在 Apple的文档

这篇关于如何检查作为UDF参数的数组中的值数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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