在swift 3.0中使用optional和不使用optional来声明非赋值变量有什么区别

时间:2016-10-09 05:52:47

标签: swift concatenation optional

我们可以在swift 3.0中声明nil变量如下所示

var aString : String?

然后我们可以像这样分配值

aString = "hello"

然后,如果我们concatenate,我们必须cast,如下所示

var newone: String = aString! + " stackoverflow"
  

但我们可以像下面那样做同样的事情,当我们concatenate时,我们   不需要Unwrap。为什么呢?

var bStirng : String

bStirng = "hello"
var newBString : String = bStirng + " StackOveflow"

使用optional的唯一优势是我们可以检查变量是否为nil

4 个答案:

答案 0 :(得分:3)

使用选项有很多优点,在我看来,主要优点是它会强制您每次都检查nil值。

但是,您正在以错误的方式使用选项(根据您显示的代码判断)。你不要只是说"今天天气好啊,让我把这个变量变成一个可选项!"我认为这就是为什么你认为使用它们只有一个优点。

有时,将变量设为可选是唯一有意义的,因为变量的本质就是这样。让我们说你正在写一个扫雷游戏。表示扫雷板上的图块的模型类可能具有hasMine: BoolxPos: IntyPos: Int等属性。它还有一个属性,用于存储用户点击磁贴numberOfMinesNearby: Int?时显示的数字。请注意,它被声明为可选整数。为什么?因为如果该图块具有我的图块,则当用户点击它时不会显示任何数字!它爆炸了!基本上,存储围绕该图块的地雷数量的想法是不适用的,因此其值为nil

另一次使用optionals的时候是你懒得为你的类创建一个初始化器,或者你想用其他方法进行初始化,比如viewDidLoad

class MyModel {
    var property: String?
    var anotherProperty: Int?
}

class MyViewController: UIViewController {
    var property: String?
    var anotherProperty: Int?
    override func viewDidLoad() {
        property = "hello"
        anotherProperty = 10
    }
}

如果您确定每次创建新的property对象时都会分配anotherPropertyMyModel,则可以将它们设为隐式解包的可选项:

class MyModel {
    var property: String!
    var anotherProperty: Int!
}

class MyViewController: UIViewController {
    var property: String!
    var anotherProperty: Int!
    override func viewDidLoad() {
        property = "hello"
        anotherProperty = 10
    }
}

这会导致可选地在您使用时被隐式强制解包。

我刚刚解释了可选项的两个优点 - 使代码有意义并节省编写初始化程序的时间。当你编写更多代码时,你肯定会发现更多。

答案 1 :(得分:2)

为了满足编译器,我们要么分配如下的值:

aString = "hello"

或者在我们不提前知道值或者是否有值的情况下,我们告诉编译器变量是可选的类型。

var aString : String?

这满足编译器的要求,允许值为nil或非nil而不会在代码中出现错误。

例如,假设您有一个类来表示用户的朋友及其stockImageID,以及他们是否有个人资料图片(在您可能需要的多种属性中):

class friend {
    var stockImageID: Int!
    var profilePicture: UIImage?
}

您可以在地址簿中搜索他的朋友(联系人)和图片联系人的照片:

myFriend.profilePicture = profilePictureFromAddressBook

然而,很多朋友都没有照片。对于那些朋友,你将使用他们的股票图像ID(每个朋友有一个,注意!告诉编译器每个朋友都有一个),这与你用来随机分配用户个人资料图片的股票图像库相关联。 / p>

能够使profilePicture成为可选项在这里非常有用,因为在代码的其他区域,您不会知道哪些朋友有profilePicture,哪些朋友不知道。作为一个选项,您可以通过执行以下操作安全而优雅地处理不知道这一点:

if let profilePicture = myFriend.profilePicture {
    print("display profile picture...")
}
else {
    let profilePicture = stockImageDictionary["myFriend.stockImageID"]
    print("display profile picture...")
}

希望这会有所帮助。随着使用该语言花费更多时间,选项的使用变得更加明显。他们真的很不错。

答案 2 :(得分:0)

答案 3 :(得分:0)

  • 使用?!使用变量意味着两者都是optional
  • !此标志表示隐式解包可选。
  • 如果我们对变量使用?符号,我们必须在使用时解包。
  • 在我的情况下,没有optional的变量不能为零。例如:
  

var valueOne : Int = 4 // this cannot be nil

  • 使用? menas的类型为optional
  

var valueTwo :Int? = 8 //type of this variable is Optional Int and   this can be nil. and also when we use this we have to unwrap it and should check whethe this is nil or not like below

     

valueOne + valueTwo!

if let newVal = valueTwo{
     valueOne + newVal //check the value is nil or not
}
  • 最后使用'!'。隐式解包可选。
  

var valueThree : Int! = 12 // type of this variable is implicitly unwrapped optional Int we do not need to unwrap when we do any operation with it like below. this also can be nil

     

valueOne + valueThree