我刚尝试在iPhone上使用我的应用程序的更新版本,其上安装的版本目前在应用程序商店中。自上次更新以来,我添加了一些新功能,这似乎会导致应用程序崩溃。
基本上,我添加了一个新数组,用于存储以前版本中不存在的注释数据。
这是我在表格视图中的应用程序崩溃的代码:
cell.notesLabel.text = (notes.objectAtIndex(indexPath.row)) as? String
错误只是说明 - “线程1:断点1.1”
如何确保从以前版本的应用程序更新的用户不必删除并重新安装它?
全班:
import UIKit
var searchResults = NSArray()
var names: NSMutableArray = []
var dates: NSMutableArray = []
var values: NSMutableArray = []
var images: NSMutableArray = []
var unformatted: NSMutableArray = []
var notes: NSMutableArray = []
class ThirdViewController: UITableViewController, UIAlertViewDelegate {
@IBOutlet weak var addTransactionButton: UIBarButtonItem!
@IBOutlet weak var clearButton: UIBarButtonItem!
let backgroundLabel = UILabel()
var userLeftReview = false
var cellTapped = false
var cellHeight = 0
override func viewWillAppear(animated: Bool) {
var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)]
self.navigationController?.navigationBar.titleTextAttributes = attributes
enterButtonTapped = NSUserDefaults.standardUserDefaults().boolForKey("viewCheck")
if enterButtonTapped == false {
addTransactionButton.enabled = false
} else {
addTransactionButton.enabled = true
}
if var tempNames: NSArray = NSUserDefaults.standardUserDefaults().arrayForKey("names") {
names = tempNames.mutableCopy() as NSMutableArray
}
if var tempDates: NSArray = NSUserDefaults.standardUserDefaults().arrayForKey("dates") {
dates = tempDates.mutableCopy() as NSMutableArray
}
if var tempValues: NSArray = NSUserDefaults.standardUserDefaults().arrayForKey("values") {
values = tempValues.mutableCopy() as NSMutableArray
}
if var tempImages: NSArray = NSUserDefaults.standardUserDefaults().arrayForKey("images") {
images = tempImages.mutableCopy() as NSMutableArray
}
if var tempUnformatted: NSArray = NSUserDefaults.standardUserDefaults().arrayForKey("unformatted") {
unformatted = tempUnformatted.mutableCopy() as NSMutableArray
}
if var tempNotes: NSArray = NSUserDefaults.standardUserDefaults().arrayForKey("notes") {
notes = tempNotes.mutableCopy() as NSMutableArray
}
self.tableView.reloadData()
println(notes)
}
func clearAllTransactions() {
totalBudget = NSUserDefaults.standardUserDefaults().stringForKey("totalBudget")!
names.removeAllObjects()
values.removeAllObjects()
dates.removeAllObjects()
images.removeAllObjects()
unformatted.removeAllObjects()
notes.removeAllObjects()
newTransactionEntered = true
totalCreditCounter = 0.00
totalSpendingsCounter = 0.00
currencyDouble = 0.00
currentBudgetCalculation = 0.00
var defaults = NSUserDefaults(suiteName: "group.AffordIt")
defaults.setObject(totalBudget, forKey: "currentBudgetWidget")
defaults.setObject(newTransactionEntered, forKey: "new")
NSUserDefaults.standardUserDefaults().setDouble(totalCreditCounter, forKey: "creditCounter")
NSUserDefaults.standardUserDefaults().setDouble(totalSpendingsCounter, forKey: "spendingsCounter")
NSUserDefaults.standardUserDefaults().setDouble(currentBudgetCalculation, forKey: "currentCounter")
NSUserDefaults.standardUserDefaults().setObject(currencyDouble, forKey: "doubleCounter")
NSUserDefaults.standardUserDefaults().setObject(names, forKey: "names")
NSUserDefaults.standardUserDefaults().setObject(values, forKey: "values")
NSUserDefaults.standardUserDefaults().setObject(dates, forKey: "dates")
NSUserDefaults.standardUserDefaults().setObject(images, forKey: "images")
NSUserDefaults.standardUserDefaults().setObject(unformatted, forKey: "unformatted")
NSUserDefaults.standardUserDefaults().setObject(notes, forKey: "notes")
self.tableView.reloadData()
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
self.filterContentForSearchText(searchString)
return true
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if tableView == self.searchDisplayController?.searchResultsTableView {
return searchResults.count
} else {
if names.count == 0 {
if enterButtonTapped == false {
backgroundLabel.text = "Before you add any transactions, you must first set a budget. You can do this by tapping the 'Budget' tab."
} else {
backgroundLabel.text = "You haven't added any transactions yet. Tap the add button to add a new transaction."
}
backgroundLabel.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
backgroundLabel.numberOfLines = 0
backgroundLabel.textAlignment = NSTextAlignment.Center
backgroundLabel.sizeToFit()
backgroundLabel.hidden = false
backgroundLabel.font = UIFont(name: "Avenir", size: 17)
backgroundLabel.textColor = UIColor.lightGrayColor()
clearButton.enabled = false
self.tableView.backgroundView = backgroundLabel
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
self.tableView.scrollEnabled = false
return 0
} else {
backgroundLabel.hidden = true
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
self.tableView.scrollEnabled = true
clearButton.enabled = true
return names.count
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:CustomTransactionTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell
var arrowX: CGFloat = 0
var amountX: CGFloat = 0
if height == 480 || height == 568 {
arrowX = 260
amountX = 152
} else if height == 667 {
arrowX = 315
amountX = 217
} else {
arrowX = 354
amountX = 217
}
cell.notesLabel.alpha = 0
cell.paymentNameLabel.frame.origin.x = 20
cell.dateLabel.frame.origin.x = 20
cell.costLabel.frame.origin.x = amountX
cell.creditArrowImage.frame.origin.x = arrowX
cell.paymentArrowImage.frame.origin.x = arrowX
cell.selectionStyle = UITableViewCellSelectionStyle.None
if tableView == self.searchDisplayController?.searchResultsTableView {
cell.paymentNameLabel.text = (searchResults.objectAtIndex(indexPath.row)) as? String
var indexValue = names.indexOfObject(searchResults.objectAtIndex(indexPath.row))
cell.costLabel.text = (values.objectAtIndex(indexValue)) as? String
cell.dateLabel.text = (dates.objectAtIndex(indexValue)) as? String
cell.notesLabel.text = (notes.objectAtIndex(indexValue)) as? String
if images.objectAtIndex(indexValue) as NSObject == 0 {
cell.paymentArrowImage.hidden = false
cell.creditArrowImage.hidden = true
} else if images.objectAtIndex(indexValue) as NSObject == 1 {
cell.creditArrowImage.hidden = false
cell.paymentArrowImage.hidden = true
}
if notes.objectAtIndex(indexValue) as NSString == "" {
cell.notesLabel.text = "No notes to display."
}
//This works, unless there are two payments with the same name. To revert to previous version, remove tableView if statement and just keep code below the else.
} else {
cell.paymentNameLabel.text = (names.objectAtIndex(indexPath.row)) as? String
cell.costLabel.text = (values.objectAtIndex(indexPath.row)) as? String
cell.dateLabel.text = (dates.objectAtIndex(indexPath.row)) as? String
cell.notesLabel.text = (notes.objectAtIndex(indexPath.row)) as? String
if images.objectAtIndex(indexPath.row) as NSObject == 0 {
cell.paymentArrowImage.hidden = false
cell.creditArrowImage.hidden = true
} else if images.objectAtIndex(indexPath.row) as NSObject == 1 {
cell.creditArrowImage.hidden = false
cell.paymentArrowImage.hidden = true
}
if notes.objectAtIndex(indexPath.row) as NSString == "" {
cell.notesLabel.text = "No notes to display."
}
}
return cell
}