我正在运行ruby 2.3(出于sqlite3兼容性的原因)。下面的代码是产生连贯问题的最小代码段。
我在方法中有以下一行:
def pull_value(id_item_hash)
...
...
pull = db.execute2 "Select Level from sometable where Id = :id OR Item = :item" ,id_item_hash[:id], id_item_hash[:item]
@pull=Integer(pull) #@pull=Integer(pull[1]) DOESN'T solve the problem
...
...
end
但是,@pull
返回一个Array
,该TypeError
会在下面的行中导致一个#here
pulled = x.pull_value(id_item_hash)
#so these lines never run
print "subtract how much?: "; subtracting = Integer(gets.chomp)
new_value = pulled - subtracting
:
Label
我的问题:我如何挑出列pull
中的第一个发现值,以便我的变量@IBOutlet weak var tableView: UITableView!
var articles: [Article]? = []
var source = "techcrunch"
override func viewDidLoad() {
super.viewDidLoad()
fetchArticles(fromSource: source)
}
func fetchArticles(fromSource provider: String){
let urlRequest = URLRequest(url: URL(string: "https://newsapi.org/v2/top-headlines?sources=\(provider)-cn&apiKey=8dc336917a8044f5ba7df7ff787a7ccb")!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
if error != nil {
print(error)
return
}
self.articles = [Article]()
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
if let articlesFromJson = json["articles"] as? [[String : AnyObject]] {
for articleFromJson in articlesFromJson {
let article = Article()
if let title = articleFromJson["title"] as? String, let author = articleFromJson["author"] as? String, let desc = articleFromJson["description"] as? String, let url = articleFromJson["url"] as? String, let urlToImage = articleFromJson["urlToImage"] as? String {
article.author = author
article.desc = desc
article.headline = title
article.url = url
article.imageUrl = urlToImage
}
self.articles?.append(article)
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch let error {
print(error)
}
}
task.resume()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "articleCell", for: indexPath) as! ArticleCell
cell.title.text = self.articles?[indexPath.item].headline
cell.desc.text = self.articles?[indexPath.item].desc
cell.author.text = self.articles?[indexPath.item].author
cell.imgView.downloadImage(from: (self.articles?[indexPath.item].imageUrl!)!)
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (self.articles?.count) ?? 0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let webVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "web") as! WebViewController
webVC.url = self.articles?[indexPath.item].url
self.present(webVC, animated: true, completion: nil)
}
let menuManager = MenuManager()
@IBAction func menuPressed(_ sender: Any) {
menuManager.openMenu()
menuManager.mainVC = self
}
可以单独将此值保存为整数? >
答案 0 :(得分:1)
尝试一下
id = id_item_hash[:id]
item = id_item_hash[:item]
pull = db.execute2("Select Level from sometable where Id = :id OR Item = :item")
# puts pull[1][0]
@pull = pull[1][0]