2014-09-22 68 views
0

我的應用程序中有一個用戶用於輸入預算名稱的文本字段。我將這些名稱存儲在數組中,以便用戶可以點擊「預設」按鈕以快速訪問以前使用的名稱。將空白文本添加到數組中的問題

如果用戶沒有輸入預算名稱,那麼名稱會自動設置爲「預算」,我不想將其存儲在預設列表中。因此,我想出了這個代碼:

presetNames = NSUserDefaults.standardUserDefaults().objectForKey("presets") as NSArray 
    presetNamesMutable = presetNames.mutableCopy() as NSMutableArray 

    if presetNamesMutable.containsObject(budgetName) == false && budgetName != "Budget" { 
     presetNamesMutable.insertObject(budgetName, atIndex: 0) 
     NSUserDefaults.standardUserDefaults().setObject(presetNamesMutable, forKey: "presets") 
    } 

所以基本上它檢查,以確保該名稱尚未在陣列中的名稱不是「預算」。

問題是,由於某種原因,如果文本字段留空,而不是忽略它,而不是將它添加到數組中,它似乎只是將「」添加到數組。我想不通爲什麼...

這是我的意思的例子:

enter image description here

這裏是代碼的其餘部分:

表視圖,用於顯示預設名稱:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return presetNamesMutable.count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell:PresetTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as PresetTableViewCell 
    // Configure the cell... 

    cell.presetNameLabel.text = presetNamesMutable.objectAtIndex(indexPath.row) as? String 
    return cell 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    var cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! 

    tempBudgetName = presetNamesMutable.objectAtIndex(indexPath.row) as String 
    presetTapped = true 
    NSUserDefaults.standardUserDefaults().setBool(presetTapped, forKey: "presetUsed") 
    self.navigationController?.popToRootViewControllerAnimated(true) 
    println(tempBudgetName) 
} 


// Override to support conditional editing of the table view. 
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    return true 
} 

// Override to support editing the table view. 
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 

    presetNamesMutable.removeObjectAtIndex(indexPath.row) 
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    NSUserDefaults.standardUserDefaults().setObject(presetNamesMutable, forKey: "presets") 
} 

override func viewWillAppear(animated: Bool) { 
    presetNames = NSUserDefaults.standardUserDefaults().objectForKey("presets") as NSArray 
    presetNamesMutable = presetNames.mutableCopy() as NSMutableArray 
} 

回答

0

答案在我發佈在我的問題中的代碼中很明顯。

if presetNamesMutable.containsObject(budgetName) == false && budgetNameLength != 0 { 

要:

if presetNamesMutable.containsObject(budgetName) == false && budgetName != "Budget" { 

所以我完全錯過了這樣的事實,我是不是名字爲「預算」我剛從改變了它。現在用此代碼修復:

presetNames = NSUserDefaults.standardUserDefaults().objectForKey("presets") as NSArray 
    presetNamesMutable = presetNames.mutableCopy() as NSMutableArray 

    if budgetNameLength == 0 { 
     budgetName = "Budget" 
    } 

    if presetNamesMutable.containsObject(budgetName) == false && budgetName != "Budget" { 
     presetNamesMutable.insertObject(budgetName, atIndex: 0) 
     NSUserDefaults.standardUserDefaults().setObject(presetNamesMutable, forKey: "presets") 

     println(presetNamesMutable) 
    }