此示例涉及todo应用程序上下文,我有以下代码将todo添加到列表中:
let userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
var itemList:NSMutableArray = userDefaults.objectForKey("itemList") as NSMutableArray
let dataSet:NSMutableDictionary = NSMutableDictionary()
dataSet.setObject(titleTextField.text, forKey: "itemTitle")
dataSet.setObject(notesTextField.text, forKey: "itemNotes")
if ((itemList) != nil) {
let newList:NSMutableArray = NSMutableArray(
for dict:AnyObject in itemList{
newList.addObject(dict as! NSDictionary)
}
userDefaults.removeObjectForKey("itemList")
newList.addObject(dataSet)
userDefaults.setObject(newList, forKey: "itemList")
}
else{
userDefaults.removeObjectForKey("itemList")
itemList = NSMutableArray(
itemList.addObject(dataSet)
userDefaults.setObject(itemList, forKey: "itemList")
}
userDefaults.synchronize()
self.navigationController.popToRootViewControllerAnimated(true)
我如何在字典/数组中搜索以查找多个itemTitle
,这样如果用户创建了两次标题,它将向他们发送一条错误消息,通知他们他们已经创建了两次标题并且它必须在添加新待办事项之前更改?
答案 0 :(得分:0)
继承人我为解决这个问题所做的一切:
在保存待办事项列表的按钮中...
let userDefaults:NSUserDefaults = NSUserDefaults.standardUserDefaults()
let itemList:NSMutableArray? = userDefaults.objectForKey("itemList") as? NSMutableArray
if (itemList?.count > 0) {
array1 = (itemList?.valueForKey("itemTitle"))! as! [String]
if (array1.count > 0) {
for i in 0...array1.count-1 {
if array1[i] == titleText {
print("title is already in the to do list")
}
}
}
}
答案 1 :(得分:0)
使用谓词在SWIFT 2中搜索数组中的特定键
let myPredicate = NSPredicate(format: "education_name contains[c]%@",passedString)
let mySearchingArray = arrayHoldingData.filteredArrayUsingPredicate(myPredicate)