评估以下代码,我希望单次打印Hello World. 相反,它会导致无限循环。有人可以解释为什么吗?
let array = ["what"]
for text: String? in array {
print("Hello World")
}
Run Code Online (Sandbox Code Playgroud)
(删除可选?显然使它只打印一次)
我在一个非常简单的单视图Swift应用程序中有这个代码ViewController:
var audioPlayer = AVAudioPlayer()
@IBAction func playMyFile(sender: AnyObject) {
let fileString = NSBundle.mainBundle().pathForResource("audioFile", ofType: "m4a")
let url = NSURL(fileURLWithPath: fileString)
var error : NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: url, error: &error)
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
if (audioPlayer.isEqual(nil)) {
println("There was an error: (er)")
} else {
audioPlayer.play()
NSLog("working")
}
Run Code Online (Sandbox Code Playgroud)
我添加了import AVFoundation并且audioPlayer是一个全局变量.当我执行的代码,它打印"工作",所以它使通过没有错误,但没有播放声音.该设备没有沉默.
我正在尝试对圆形SVG进行非常简单的修改.脚本代码应该改变圆的半径,但似乎什么也没发生.(使用相同的格式,我可以更改颜色,但没有圆圈的其他元素).
<!DOCTYPE html>
<html>
<body>
<svg xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="50" fill="red" id="cir"/>
</svg>
<script>
document.getElementById("cir").r = 2000;
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我想启用 Peek&Pop,并包括适用于 iPhone 6s 及以上用户的 3D Touch 操作,但我还想启用长按以将非 3D Touch 用户输入到操作表中。
我了解如何使用 peek 和 pop 委托方法,并且了解如何创建长按手势。我需要知道的是如何连接长按动作以显示带有动作的预览预览。
我有一个协议,充当一个视图和另一个视图之间的委托.
协议看起来像这样:
protocol MyProtocol: class {
func functionOne()
}
Run Code Online (Sandbox Code Playgroud)
协议的实现方式View2如下:
extension View2:MyProtocol {
func functionOne() {
print("Hello World"
}
}
Run Code Online (Sandbox Code Playgroud)
现在我希望这个方法被一个按钮的目标调用View1.所以在View1我有一条线:
myButton(self, action: #selector(delegate?.functionOne), forControlEvents: .TouchUpInside)
Run Code Online (Sandbox Code Playgroud)
但是按钮行错误"Use of unresolved identifier 'functionOne'",这是我以前在其他问题中没有见过的错误.
如果我functionTwo进入View1并设置目标functionTwo,然后functionTwo通过直接调用实现,一切都有效delegate?.functionOne.但这似乎非常不优雅.
我正在使用连接到分页UIScrollView的UIPageControl(所有以编程方式).我想只允许用户在页面中滑动.
UIPageControl当前正在UIScrollViewDelegate中正确更新scrollViewDidScroll.但是当你点击UIPageControl时,启用的点会向前跳一个(这是主屏幕所需的行为,但我不希望这个点击提升功能).
我想阻止这种行为,但我找不到一种方法来轻松禁用它.
swift ×5
ios ×4
3dtouch ×1
avfoundation ×1
cocoa-touch ×1
delegation ×1
html5 ×1
javascript ×1
long-press ×1
protocols ×1