Swift用户默认值-数组

时间:2018-09-11 10:14:24

标签: arrays swift userdefaults

在以下代码上获取错误

  

二进制运算符'=='不能应用于类型[[String]?]的操作数   和“字符串”

Sub Search_SentMail()

Dim myOlApp As New Outlook.Application
Dim MyNameSpace As Outlook.NameSpace
Dim MyInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myItem As Object

Set MyNameSpace = myOlApp.GetNamespace("MAPI")
Set MyInbox = MyNameSpace.GetDefaultFolder(olFolderInbox)
Set myOutbox = MyNameSpace.GetDefaultFolder(olFolderSentMail)
Set myitems = myOutbox.Items

Dim junk As Outlook.Folder
Set junk = MyNameSpace.GetDefaultFolder(olFolderInbox)
Set junk = junk.Folders("CN")
Count = 0
For Each myItem In myitems
If TypeOf myItem Is Outlook.MailItem Then
If Right(myItem.To, 4) = ".cn'" Then
myItem.Move junk
Count = Count + 1
End If
End If
Next myItem

Debug.Print Count
Set MyNameSpace = Nothing
Set MyInbox = Nothing
Set myOutbox = Nothing
Set myitems = Nothing
Set junk = Nothing
End Sub

1 个答案:

答案 0 :(得分:2)

首先,有一种专用的方法array(forKey用于检索数组。

一个空数组isEmpty,您无法将其与一个空String

进行比较

如果要加载存储的数组或在找不到对象的情况下分配一个空数组,请使用

func loadDefaults() {
    // a variable name userDefaults for the array is confusing
    if let storedArray = UserDefaults.standard.array(forKey: "storedArray") as? [String] {
        persons = storedArray
    } else {
        persons = []
    }
}

或更短

func loadDefaults() {
    persons = UserDefaults.standard.array(forKey: "storedArray") as? [String] ?? []
}