Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.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 如何在CGRectMake函数中使用变量-尝试以类似表的结构输出数据_Ios_Swift - Fatal编程技术网

Ios 如何在CGRectMake函数中使用变量-尝试以类似表的结构输出数据

Ios 如何在CGRectMake函数中使用变量-尝试以类似表的结构输出数据,ios,swift,Ios,Swift,我试图通过实际创建和堆叠标签来输出帐户的“朋友”列表。我从MySQL数据库(可通过NSURL与PHP脚本交互访问)中获取了帐户的好友,并将其加载到一个数组中。目标是在数组中循环,并创建一个标签,该标签在每次迭代中的位置略低于前一个标签。我遇到的问题是,当我将一个变量传递到CGRectMake函数以创建标签时,它将不接受int,因为它不是“CGFloat”类型。我觉得我已经尝试了几乎所有的方法,但我永远无法正确地转换数据类型。以下是我的代码: import UIKit class viewFri

我试图通过实际创建和堆叠标签来输出帐户的“朋友”列表。我从MySQL数据库(可通过NSURL与PHP脚本交互访问)中获取了帐户的好友,并将其加载到一个数组中。目标是在数组中循环,并创建一个标签,该标签在每次迭代中的位置略低于前一个标签。我遇到的问题是,当我将一个变量传递到CGRectMake函数以创建标签时,它将不接受int,因为它不是“CGFloat”类型。我觉得我已经尝试了几乎所有的方法,但我永远无法正确地转换数据类型。
以下是我的代码:

import UIKit

class viewFriendsViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let friendsArray = ["billy", "joe", "bob", "jim"]
        var inc = 0
        for index in friendsArray {
            let label: UILabel = UILabel()
            let yPos: CGFloat = Float(inc * 10)
            label.frame = CGRectMake(50, 50, 200, yPos)
            label.backgroundColor = UIColor.blackColor()
            label.textColor = UIColor.whiteColor()
            label.textAlignment = NSTextAlignment.Center
            label.text = "\(index)"
            self.view.addSubview(label)
            inc = inc + 1 // I have not been able to play with the label yet to determine how to move it's vertical position because I cannot run the program due to the casting error.
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

}
试试这个

var inc = 0
    for index in friendsArray {
        let label: UILabel = UILabel()

        //type conversion is 
        let yPos = CGFloat(inc * 10)

        label.frame = CGRectMake(50,yPos,200,50)  // if you want to change the Y-Coodinates use in Y frame not in height
        label.backgroundColor = UIColor.blackColor()
        label.textColor = UIColor.whiteColor()
        label.textAlignment = NSTextAlignment.Center
        label.text = "\(index)"
        self.view.addSubview(label)
        inc = inc + 1 
    }
以上选择不适用于此尝试

  var inc : CGFloat = 0
    for index in friendsArray {
        let label: UILabel = UILabel()

        label.frame = CGRectMake(50,inc,200,50)  // if you want to change the Y-Coodinates use in Y frame not in height
        label.backgroundColor = UIColor.blackColor()
        label.textColor = UIColor.whiteColor()
        label.textAlignment = NSTextAlignment.Center
        label.text = "\(index)"
        self.view.addSubview(label)
        inc = inc * 10  //  inc + 10
    }

我用
CGFloat()
而不是
Float()
对它进行了测试,它可以工作:

var inc = 0
var ypos: CGFloat = CGFloat(inc * 10)
var rect: CGRect = CGRectMake(50, ypos, 200, 200)
另请注意,您当前使用的
ypos
位于
高度的位置。记住是的

CGRectMake(<xPos>, <yPos>, <width>, <height>)
CGRectMake(,)

您应该利用swift中CGRect的新符号,而不是使用传统的c宏,这些宏允许与Int、Double和Float一起使用:

CGRect(x: 50, y: 200, width: 20, height: 40)

请注意,这些帧现在是使用用于结构的标准swift init函数创建的。

仍然收到一个错误,即INT不能转换为CGFloat。谢谢你的帮助好吗?试试这个:
CGRect(x:50.0,y:200.0,宽度:20.0,高度:40.0)