[英]Keep getting a fatal error that says “unexpectedly found nil while unwrapping an Optional value” in iOS
I run my code on Xcode and it build fine but when I tap a button to change views it crashes with this error. 我在Xcode上运行我的代码并且它构建正常但是当我点击一个按钮来更改视图时它会因此错误而崩溃。
var selectedAccount : ACAccount!
var tweets : NSMutableArray?
var imageCache : NSCache?
var queue : NSOperationQueue?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = selectedAccount.accountDescription
queue = NSOperationQueue()
queue!.maxConcurrentOperationCount = 4
func retrieveTweets() {
tweets?.removeAllObjects()
if let account = selectedAccount {
let requestURL = NSURL(string: "https://api.twitter.com/1.1/statuses/home_timeline.json")
let request = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: SLRequestMethod.GET, URL: requestURL, parameters: nil)
request.account = account
request.performRequestWithHandler()
{
responseData, urlResponse, error in
if(urlResponse.statusCode == 200)
{
var jsonParseError : NSError?
self.tweets = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.MutableContainers, error: &jsonParseError) as? NSMutableArray
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
if let tweetCount = self.tweets?.count {
return tweetCount
}
else
{
return 0
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TweetCell", forIndexPath: indexPath) as TweetCell
return cell
}
When it crashes it it posts an green line over the line "self.navigationItem.title = selectedAccount.accountDescription" 当它崩溃时,它会在“self.navigationItem.title = selectedAccount.accountDescription”行上发布一条绿线。
I'm not sure why its saying its associated with this line. 我不确定为什么它说它与这条线有关。
Please help 请帮忙
SC SC
Make sure you remove any warning messages here. 请务必在此处删除任何警告消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.