Swift变量使用optional(?)声明还是强制unwrap(!)不同?

时间:2018-08-26 01:55:04

标签: swift

有什么不同,当我们使用其中一个声明新的变量post fix时?或!

例如:

class Car{
   var gear : Int!
   var color : String?
}

我找不到关于此问题的任何资源

1 个答案:

答案 0 :(得分:1)

function GmailToDrive(){ //build query to search emails var query = ''; //filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:jpeg'; //'after:'+formattedDate+ for(var i in fileTypesToExtract){ query += (query === '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i])); } query = 'in:inbox has:nouserlabels ' + query; var threads = GmailApp.search(query); var label = getGmailLabel_(labelName); var parentFolder; if(threads.length > 0){ parentFolder = getFolder_(folderName); } var root = DriveApp.getRootFolder(); for(var i in threads){ var mesgs = threads[i].getMessages(); mesgs.reverse(); for(var j in mesgs){ //get attachments var attachments = mesgs[j].getAttachments(); for(var k in attachments){ var attachment = attachments[k]; var isDefinedType = checkIfDefinedType_(attachment); if(!isDefinedType) continue; var attachmentBlob = attachment.copyBlob(); var file = DriveApp.createFile(attachmentBlob); parentFolder.addFile(file); root.removeFile(file); } } threads[i].addLabel(label); threads[i].moveToTrash(); threads[i].refresh(); } } 可选Foo?隐式解包的可选

This section of the language guide解释了哪些隐式展开的可选内容:

  

有时候,从程序的结构中可以看出,在首次设置可选值后,该可选值将始终具有该值。在这种情况下,取消每次访问可选值的必要性就很有用,因为可以肯定地认为它始终都具有值。