Swift - 如何更改get-only属性的值

时间:2016-09-17 07:05:49

标签: objective-c swift xcode opengl swift2

您好我是swift和OpenGL的新手。我按照目标C编写的教程,并将其转换为Swift 2.0

这是代码

float radius = self.view.bounds.size.width/3; 
GLKVector3 center = GLKVector3Make(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 0);
GLKVector3 P = GLKVector3Subtract(touchPoint, center);

P = GLKVector3Make(P.x, P.y * -1, P.z);

float radius2 = radius * radius;
float length2 = P.x*P.x + P.y*P.y;

if (length2 <= radius2)
    P.z = sqrt(radius2 - length2);
else
{
    P.z = radius2 / (2.0 * sqrt(length2));
    float length = sqrt(length2 + P.z * P.z);
    P = GLKVector3DivideScalar(P, length);
}

这是我的Swift Code

    let radius: CGFloat = self.view.bounds.size.width/3
    let center: GLKVector3 = GLKVector3Make(Float(self.view.bounds.size.width / 2), Float(self.view.bounds.size.height/2), 0.0)
    var P: GLKVector3 = GLKVector3Subtract(touchPoint, center)

    P = GLKVector3Make(P.x, P.y * -1, P.z)

    let radius2 = radius * radius
    let length = P.x * P.x + P.y * P.y

    if(Float(length) <= Float(radius2)){
        P.z = sqrt(Float(radius2) - Float(length)) //the error is here
    } else {
        //other code
    }

我无法改变P.z的价值,它说

  

&#34;无法分配属性:&#39; z&#39;是一个只获取财产&#34;

提前谢谢

1 个答案:

答案 0 :(得分:0)

您需要创建一个新的GLK3DVectorMake。似乎它是桥接使用Swift中的Struct。

Struct是不可变的,除非它们在内部实现中发生变异。要克服的一种方法是创建具有正确属性的新GLK3DVectorMake。它是一种广泛使用的技术,用于修改CGRect,CGPoint和任何结构类型。

    private void button1_Click(object sender, EventArgs e)
    {
         m = textBox1.Text.Substring(0, 2);
        s = textBox1.Text.Substring(3, 2);
        sec = Convert.ToInt32(s);

        minut = Convert.ToInt32(m);
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {

        sec++;
        if(sec>59)
        {
            sec = 1;
            minut++;
        }
        if(minut>59)
        {
            minut = 0;
        }

        label1.Text = minut.ToString() + ":" + sec.ToString();
        //second = second + 1;
        //if (second >= 10)
        //{
        //    timer1.Stop();
        //    MessageBox.Show("Exiting from Timer....");
        //}
    }


}