我的代码中有一个我无法弄清楚的问题。当我运行我的应用程序时,有时它会加载我的手机,有时它会立即崩溃。我相信我的问题出在我的 cellForRowInIndexPath 上。
我的错误在 cellForRowInIndexPath 行:
cellDrawer.insertDescriptionLabel(cell, text: offer.title)
这是我的错误:
Thread 1: EXC_BAD_ACCESS(code=1,address=0x0)
这是我的代码:
import UIKit
class ProdutsViewController: UIViewController, UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating{
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableViewFooter: MyFooter!
var searchResults:[Offer] = []
var resultSearchController = UISearchController()
var loading = false
lazy var offers:[Offer]? = {
print("lazy of offers\n")
var restConnection = RestFulConnection()
restConnection.fetchDataFromServer()
return DataBaseChecker.getDataFromDatabase()
}()
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad\n")
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
// Reload the table
self.tableView.reloadData()
self.tableViewFooter.hidden = true
loadSegment()
}
class MyDataProvider {
class func getInstance() -> MyDataProvider {
print("getInstance of MyDataProvider\n")
return MyDataProvider() //return a new instance since class vars not supported yet
}
func requestData(listener:([Offer]) -> ()) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
//simulate delay
sleep(1)
print("requestData of MyDataProvider\n")
//generate items
var restConnection = RestFulConnection()
restConnection.fetchDataFromServer()
var arr = DataBaseChecker.getDataFromDatabase()
//call listener in main thread
dispatch_async(dispatch_get_main_queue()) {
listener(arr)
}
}
}
}
func scrollViewDidScroll(scrollView: UIScrollView) {
print("scrollViewDidScroll\n")
let currentOffset = scrollView.contentOffset.y
let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
if (maximumOffset - currentOffset) <= 40 {
print("IF of scrollViewDidScroll\n")
loadSegment()
}
}
func loadSegment() {
print("loadSegment\n")
if (!self.loading) {
self.setLoadingState(true)
print("IF of loadSegment\n")
MyDataProvider.getInstance().requestData(
{(offers:[Offer]) -> () in
print("block of requestData on method loadSegment\n")
for offer:Offer in offers {
print("for of loadSegment\n")
if(DataBaseChecker.isItNew(offer)){
self.offers?.append(offer)
}
}
self.tableView.reloadData()
self.setLoadingState(false)
})
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
print("didReceiveMemoryWarning\n")
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! UITableViewCell
print("CellForRowAtIndexPath\n")
// var offer:Offer?
// if (self.resultSearchController.active) {
// offer = searchResults[indexPath.row]
// } else {
// offer = offers?[indexPath.row]
// }
if(offers != nil){
var offer:Offer = offers![indexPath.row]
print("IF of offers not nil, offers = \(offer.title)\n")
var cellDrawer = CellDrawer()
if (cell.viewWithTag(1) == nil){
cellDrawer.createWhiteContentInCell(cell)
}
cellDrawer.insertImageInCell(offer.images, cell: cell)
cellDrawer.insertBlackContentInCell(cell)
cellDrawer.insertDescriptionLabel(cell, text: offer.title)
cellDrawer.insertLocalLabel(cell, text: "String Sample")
cellDrawer.insertOldPriceLabel(cell, number: offer.oldPrice)
//cellDrawer.insertFromPriceLabel(cell, text: "a partir de")
cellDrawer.insertNewPriceLabel(cell,number:offer.newPrice)
}
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("NumberOfRowsInSection\n")
if(offers != nil){
print("IF offers not nil of NumberOfRowsInSection\n")
if (self.resultSearchController.active) {
print("IF2 of NumberOfRowsInSection\n")
return searchResults.count
} else {
print("ELSE of NumberOfRowsInSection, offers count = \(offers!.count)\n")
return offers!.count
}
}
return 0
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
print("numberOfSectionsInTableView\n")
if(offers != nil && offers!.count >= 1){
print("if of numberOfSectionsInTableView\n")
return 1
}
return 0
}
func setLoadingState(loading:Bool) {
print("setLoadingState\n")
self.loading = loading
self.tableViewFooter.hidden = !loading
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
print("heightForRowAtIndexPath\n")
return 235
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
print("updateSearchResultsForSearchController\n")
if(offers != nil){
print("if of updateSearchResultsForSearchController")
searchResults.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "title CONTAINS[c] %@", searchController.searchBar.text)
let array = NSArray(array: offers!)
array.filteredArrayUsingPredicate(searchPredicate)
self.searchResults = array as! [Offer]
self.tableView.reloadData()
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
print("prepareForSegue\n")
if segue.identifier == "detailsSegue" {
print("IF of prepareForSegue\n")
let DetailTVC = segue.destinationViewController as! DetailsTVC
if sender as! UITableView == self.resultSearchController.active {
print("IF2 of prepareForSegue\n")
let indexPath = self.tableView.indexPathForSelectedRow()!
let offer = self.searchResults[indexPath.row]
DetailTVC.offer = offer
} else {
print("Else of prepareForSegue\n")
let indexPath = self.tableView.indexPathForSelectedRow()!
let offer = self.offers![indexPath.row]
DetailTVC.offer = offer
}
}
}
}
希望任何人都可以提供帮助。
最佳答案
如果 offer.title 不为 nil,则 cellDrawer 可能正在对不再有效的单元格执行某些操作。
设置断点并在调试器中使用 po(打印对象)检查 offer.title 属性。
抽屉试图加载标题时,报价数组正在更新也可能是竞争条件。
关于iOS 应用因 EXC_BAD_ACCESS(代码=1,地址=0x0)而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30466283/
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru