可选和隐式解包的可选项有什么区别?

时间:2016-07-21 05:52:00

标签: ios swift optional

考虑这两个声明:

compile 'com.facebook.android:facebook-android-sdk:4.+' 

两种情况下的结果都是 public void Update(MouseState mouse, GameTime gameTime,Matrix matrix) { if (mouse.LeftButton == ButtonState.Pressed) { mousePos = new Vector2(mouse.X, mouse.Y); Vector2.Transform(mousePos, matrix); oldPos = testObjPos; Vector2.Transform(oldPos, matrix); Difference = mousePos - oldPos; Difference.Normalize(); } testObjPos += Difference * (float)gameTime.ElapsedGameTime.TotalSeconds * 20; if (testObjPos.X > mousePos.X - 1 && testObjPos.X < mousePos.X + 1 && testObjPos.Y > mousePos.Y - 1 && testObjPos.Y < mousePos.Y + 1) Difference = new Vector2(0, 0); } 。那有什么区别?

1 个答案:

答案 0 :(得分:0)

隐式展开的可选项可以(将)初始化nil 但是您告诉编译器在您第一次使用它之前它将被设置为一个值。如果您在初始化之前尝试使用它,就像我在下面那样,那么每次都会崩溃。

enter image description here