Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 返回数组并检查是否为nil的正确方法_Ios_Arrays_Swift - Fatal编程技术网

Ios 返回数组并检查是否为nil的正确方法

Ios 返回数组并检查是否为nil的正确方法,ios,arrays,swift,Ios,Arrays,Swift,我在学习Swift时遇到了问题。我得到了一个包含两个变量的类,这些变量很好,但我想返回[CGColor]数组,以便在UIView中生成不同的背景渐变 守则的一部分: class Xxx{ var _output : String! var _backgroundGradient : [CGColor]! //checking if not nil var output : String!{ if _output == nil{

我在学习Swift时遇到了问题。我得到了一个包含两个变量的类,这些变量很好,但我想返回[CGColor]数组,以便在UIView中生成不同的背景渐变

守则的一部分:

class Xxx{
    var _output : String!
    var _backgroundGradient : [CGColor]!

    //checking if not nil
    var output : String!{
        if _output == nil{
            _output = ""
        }
        return _output
    }

var backgroundGradient : [CGColor]{
    if _backgroundGradient?.isEmpty == false{
        print("There are objects!")
    } else{
        print("There are no objects")
        _backgroundGradient = [CGColor]()
    }
    return _backgroundGradient
}
正如您所看到的,_输出是正常的,如果它是nil,那么我分配空字符串。我尝试过对数组做类似的事情,如果它是空的,那么创建空数组,但不是nil

此外,在代码中还有一个附加特定值的函数:

(...)
     self._backgroundGradient.append(UIColor(red:1.00, green:0.79, blue:0.04, alpha:1.00).cgColor)
     self._backgroundGradient.append(UIColor(red:0.97, green:0.41, blue:0.02, alpha:1.00).cgColor)
     print("GRADIENT inside func -> \(self._backgroundGradient)")
(...)
正如我在Xcode的底部控制台中看到的,它正确地显示了:

GRADIENT inside func -> Optional([<CGColor 0x6000000b1340> [<CGColorSpace 0x6000000359e0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 1 0.79 0.04 1 ), <CGColor 0x6080000b15e0> [<CGColorSpace 0x6000000359e0> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 0.97 0.41 0.02 1 )])
以及控制台的输出:

MainVC GRADIENT -> Optional([])

问题必须是var背景梯度中的返回。每一个其他变量,如string、int等,都像一个符咒,数组是一个问题。

问题是您没有调用将颜色添加到backgroundGradient数组的函数

创建Xxx对象后,需要更改此代码段以进行函数调用

(...)
var classXxx : Xxx!

    override func viewDidLoad() {
        super.viewDidLoad()

        classXxx = Xxx()

        // Call function to add colors to the backgroundGradient array
        classXxx.addColors() // Change 'addColors()' to the name of the function which contains the lines of code quoted below

        let gradient: CAGradientLayer = CAGradientLayer()

        gradient.colors = classXxx.backgroundGradient
        print("MainVC GRADIENT -> \(gradient.colors)")
(...)
--

--

编辑:

可以通过使用默认值初始化变量来简化类

例如:

class Xxx {
    var output : String = ""
    var backgroundGradient : [CGColor] = [
        UIColor(red:1.00, green:0.79, blue:0.04, alpha:1.00).cgColor,
        UIColor(red:0.97, green:0.41, blue:0.02, alpha:1.00).cgColor
    ]
}

当您执行
Xxx()
操作时,您正在使用自己的数组创建一个新的类实例,该数组的开头是空的。@dan so Xxx()创建的backgroundGradient数组是空的吗?但是我可以在不创建classXxx=Xxx()的情况下检索backgroundGradient吗?你能帮我解决这个问题吗?哦,上帝,这意味着我唯一做错的事情就是在错误的地方调用函数。它现在正在工作,但是有没有更好的方法呢?谢谢你的帮助!我不知道你到底想要什么。你想让classXxx总是从拥有这两种颜色开始吗?
func addColors() {
    (...)
         self._backgroundGradient.append(UIColor(red:1.00, green:0.79, blue:0.04, alpha:1.00).cgColor)
         self._backgroundGradient.append(UIColor(red:0.97, green:0.41, blue:0.02, alpha:1.00).cgColor)
         print("GRADIENT inside func -> \(self._backgroundGradient)")
    (...)
}
class Xxx {
    var output : String = ""
    var backgroundGradient : [CGColor] = [
        UIColor(red:1.00, green:0.79, blue:0.04, alpha:1.00).cgColor,
        UIColor(red:0.97, green:0.41, blue:0.02, alpha:1.00).cgColor
    ]
}