有什么区别:和=在swift中 [英] What's the difference between : and = in swift
问题描述
很抱歉,如果标题相当令人困惑,但我很想知道这两行之间的区别:
Sorry if the title is rather confusing, but I'm curious to know the difference between these two lines:
var title = String()
var title: String
是否正在初始化一个,只有一个是声明?哪个更正确?
Is one being initialized and one only be declared? Which is more correct?
例如,如果我有一个结构,我应该使用另一个吗?
For example, if I have a struct should I use one of the other?
所以我问这个的原因是因为我正在学习如何从网址中获取一些JSON然后在我的应用中显示它。这样做的一种新方法是使用Decodable。所以,我在模型类中有一个结构,如下所示:
So the reason I ask this is because I'm learning about how to grab some JSON from a url and then display it in my app. One of the new ways of doing so is using Decodable. So, I have a struct in a model class like so:
struct Videos: Decodable {
var title = String()
var number_of_views : Int
var thumbnail_image_name: String
var channel: Channel
var duration: Int
}
在另一个课程中我有这个:
In another class I have this:
URLSession.shared.dataTask(with: url){(data,response,error) in
if(error != nil){
print(error!)
return
}
guard let data = data else { return }
do{
self.Videos2 = try JSONDecoder().decode([Videos].self, from: data)
//self.collectionView?.reloadData()
}catch let jsonErr{
print(jsonErr)
}
}.resume()
那么,我应该在结构中声明或初始化变量吗?我假设我应该像这样声明它们:
var title:String?
这是我的结构中正确的语法吗?
So, should I declare or initialize the variables in my struct? I'm assuming I should just declare them like so: var title: String? Would that be the correct syntax in my struct?
更新:
我明白这个问题比我最初提出的要宽泛得多。对此我感到很抱歉,但是非常感谢你所有的答案,这些答案为我澄清了很多。
UPDATE: I understand this question was more broad then I originally proposed it to be. I'm sorry about that, but thank you so much for all your great answers that clarified a lot up for me.
推荐答案
区别在于:
定义变量的类型,而 =
为变量指定一个实际值。
The difference is that :
defines the type of your variable, whereas =
assigns an actual value to the variable.
所以:
var title = String()
这调用 String
类型的初始值设定项,创建一个新的 String
实例。然后它将此值分配给 title
。 title
的类型被推断为 String
,因为您正在分配类型的对象字符串
到它;但是,您也可以将此行显式写为:
This calls the initializer of the String
type, creating a new String
instance. It then assigns this value to title
. The type of title
is inferred to be String
because you're assigning an object of type String
to it; however, you could also write this line explicitly as:
var title: String = String()
这意味着你要声明一个 title
类型<$ c $的变量c> String ,并为其指定一个新的 String
。
This would mean you are declaring a title
variable of type String
, and assigning a new String
to it.
var title: String
这只是说你要定义一个类型的变量字符串
。但是,您没有为其分配值。在使用它之前你需要为这个变量赋值,或者你会得到一个编译错误(如果这是一个属性而不仅仅是一个变量,你需要在你到达类型的末尾之前分配它) init()
方法,除非它后面的?
是可选的,在这种情况下它被隐式初始化为 nil
)。
This simply says you're defining a variable of type String
. However, you are not assigning a value to it. You will need to assign something to this variable before you use it, or you will get a compile error (and if this is a property rather than just a variable, you'll need to assign it before you get to the end of your type's init()
method, unless it's optional with ?
after it, in which case it gets implicitly initialized to nil
).
编辑:对于你的例子,我可能会使用声明所有变量
和:
,假设您的JSON为所有这些属性提供值。然后, Decodable
生成的初始化程序应在创建对象时设置所有属性。所以,类似于:
For your example, I'd probably declare all the variables using let
and :
, assuming that your JSON provides values for all of those properties. The initializer generated by Decodable
should then set all the properties when you create the object. So, something like:
struct Videos: Decodable {
let title: String
let number_of_views : Int
let thumbnail_image_name: String
let channel: Int
let duration: Int
}
这篇关于有什么区别:和=在swift中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!