मुझे एक त्रुटि संदेश मिल रहा है "नीचे दिए गए वर्ग के साथ स्विफ्ट से Optional
" अनपेक्षित करते समय अप्रत्याशित रूप से nil
मिला। लाइन पर त्रुटि होती है:वैकल्पिक मूल्य स्विफ्ट को अनदेखा करते समय अनपेक्षित रूप से पाया गया शून्य
(cell.contentView.viewWithTag(1) as UILabel).text = object["firstName"] as? String
मैं दुकानों
import UIKit
import Foundation
class dictionaryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var objects = NSMutableArray()
var dataArray = [["firstName":"Debasis","lastName":"Das","email":"[email protected]"],["firstName":"John","lastName":"Doe","email":"[email protected]"],["firstName":"Jane","lastName":"Doe","email":"[email protected]"],["firstName":"Mary","lastName":"Jane","email":"[email protected]"]]
@IBOutlet
var tableView: UITableView!
var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count;//self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
//cell.textLabel?.text = self.items[indexPath.row]
//return cell
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as UITableViewCell
let object = dataArray[indexPath.row] as NSDictionary
(cell.contentView.viewWithTag(1) as UILabel).text = object["firstName"] as? String
(cell.contentView.viewWithTag(2) as UILabel).text = object["lastName"] as? String
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
}
}
मुझे लगता है कि समस्या के साथ 'viewWithTag (_ :)' है, जो एक 'UIView रिटर्न' (वैकल्पिक 'UIView') क्या है?। यदि यह 'शून्य 'है, तो' text' संपत्ति पहुंच दुर्घटना का कारण बनती है। कोड को बदलने की कोशिश करें 'अगर टैग 1 = cell.contentView.viewWithTag (1) UILabel {...}' के रूप में दें और यह देखने के लिए वहां क्या होता है तो ब्रेकपॉइंट डालें। – Romain