我有一个问题可能不是专门针对实现而是提示/最佳实践的问题.
我正在研究Swift中的一个类,它以JSON格式从在线源获取数据.我希望在这个类中有特定的方法连接到在线源并以Dictionary类型返回结果.功能可能是这样的:
func getListFromOnline()->[String:String]{
var resultList : [String:String]=[:]
...
/*
Some HTTP request is sent to the online source with NSURLRequest
Then I parse the result and assign it to the resultList
*/
...
return resultList
}
Run Code Online (Sandbox Code Playgroud)
我在这个实现中得到的,没有多线程,resultList
显然是在从在线源获取之前返回的.毫不奇怪,它会导致程序失败.
任何想法或提示如何实现相反的目标?我在这个实例中对多线程有点困惑,因为我想稍后从另一个类异步调用这个公共方法,我知道如何做到这一点,但是我不知道如何让返回参数的方法在多个内部进行多线程处理本身.
或者根本不是多线程,我在这里没有看到明显的解决方案?
我有一个tableView.当点击/选择单元格时,单元格向下展开以显示多个资产.一个资产是文本字段.另一个是"提交"按钮.
当按下Submit按钮时,我需要从文本字段访问用户输入.我怎样才能做到这一点?
重要提示:由于已经选择/扩展了单元格,因此无法使用didSelectRowAtIndexpath.
目前我在customCell上有以下内容:
@IBAction func submitButtonPressed(sender: UIButton) {
// prepareForSegue is on mainVC
self.textString = self.textField.text
println(self.textString)
}
Run Code Online (Sandbox Code Playgroud)
在MainVC的"prepareForSegue"中,我有以下内容:
var cell : CustomCell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as! CustomCell
let path = self.tableView.indexPathForSelectedRow()!
println(cell.textField.text)
self.newText = cell.textString
self.newInt = self.newText.toInt()
println(self.newText)
println(self.newInt)
Run Code Online (Sandbox Code Playgroud)
以下代码正确prints(self.textString)
然而
cell.textString
,self.newText
并且self.newInt
始终如此nil
.
知道为什么吗?
我编写了一个简单的游戏,在第一次碰撞时我得到以下错误:
命中致命错误:在展开Optional值(lldb)时意外发现nil
这是我的代码:
func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ((firstBody.categoryBitMask & PhysicsCategory.Monster != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.naboj != 0)) {
projectileDidCollideWithMonster(firstBody.node as SKSpriteNode, monster: secondBody.node as SKSpriteNode)
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的表格视图。我希望第一行和最后一行有一张特殊的图片,显示一行的开头和结尾。但是,我得到了错误的识别信息。表视图认为中间的单元格是末端单元格。我相信IOS正在缓存行并在需要时加载它们,就像当我向上和向下滚动一些箭头改变图片时(加载后它们应该是静态的)。另外, 文档在这里说 知道如何解决此问题并识别最后一行和第一行,而不管缓存如何?(顺便说一句,位置是用于加载单元格的箭头,数组的第一个和最后一个位置是特殊图片应该在的位置)。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var stop = locations[indexPath.row]
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomRouteViewCell
cell.locationTitle.text = "\(stop)"
cell.downTime.text = BusRoute.getRecentDepartures(stop, direction: direction, name: name)
println(indexPath.row)
cell.indexRow = indexPath.row
if(cell.indexRow == 0)
{
cell.downImage.image = UIImage(named: "beginningRoute.png")
}
else if(cell.indexRow == locations.count - 1)
{
cell.downImage.image = UIImage(named: "endRoute.png")
}
return cell
}
Run Code Online (Sandbox Code Playgroud) 有没有办法在主页按钮上调用操作?我需要"你确定要退出应用程序"并等待是或否.我不需要任何代码.我只需要确保它是可能的.
谢谢
swift ×5
ios ×2
custom-cell ×1
nsindexpath ×1
segue ×1
sprite-kit ×1
tableview ×1
uitableview ×1