我正在尝试创建一个无限循环,其中一段代码将永远执行.
我发现的所有循环文档都警告不要创建一个无限循环,但没有一个工作的例子.
如果我有一段代码:
{ puts "foo"
puts "bar"
sleep 300 }
Run Code Online (Sandbox Code Playgroud)
我将如何永远地运行这个街区?
我目前有一个子视图,它创建并添加到ViewDidLoad()中的UIView中.我正在尝试使用UIGestureRecognizers来检测点击并取消隐藏特定按钮.我目前的代码:
override func viewDidLoad() {
super.viewDidLoad()
architectView = CustomClass(frame: self.view.bounds)
self.view.addSubview(architectView)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
gestureRecognizer.delegate = self
architectView.addGestureRecognizer(gestureRecognizer)
}
func handleTap(gestureRecognizer: UIGestureRecognizer) {
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
handleTap()函数是一个简单的测试,用于查看是否正在识别抽头.按下时,此代码不会触发UIAlert?我错过了什么?
我在自定义UIView.to中正常运行UITapGestureRecognizer时遇到问题.我创建了一个视图:CategoryViewButton,它在init中添加了一个UITapGestureRecognizer:
class CategoryViewButton: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleTap))
self.addGestureRecognizer(tapGesture)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func handleTap() {
print("Hello again")
}
}
Run Code Online (Sandbox Code Playgroud)
直接在视图控制器中添加时,此手势识别器可以正常工作.但是,当我将CategoryViewButton添加为另一个自定义视图的子视图时,不会调用手势识别器方法.我的子视图:
class CategoryView: UIView, CategoryButtonDelegate {
var button : CategoryViewButton?
override init(frame: CGRect) {
super.init(frame: frame)
button = CategoryViewButton(frame: CGRect(x: 10, y: 0, width: 40, height: 25))
self.addSubview(button!)
self.bringSubview(toFront: button!)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} …Run Code Online (Sandbox Code Playgroud) 我对使用UITableView的switch语句感到困惑.我正在尝试设置我的numberOfRowsInSection函数,但是我的switch语句出现错误" 二元运算符'〜='不能应用于'Bool'和'Int' ' 类型的操作数:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case section == 0:
return provinces.count
case section == 1:
return territories.count
default:
return 0
}
}
Run Code Online (Sandbox Code Playgroud)
我很困惑因为section被声明为Int,但错误似乎表明它是一个BOOL.当我使用if-else语句时,它编译得很好:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return provinces.count
} else {
return territories.count
}
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
我正在尝试创建一个UIView子类.在初始化中,我设置了我希望子类具有的最小和最大y轴原点值.此视图将以编程方式,用户手势或动画上下移动.我希望我可以使用willSet属性用户:
override var frame: CGRect {
willSet {
if newValue.origin.y > maximumOriginY {
self.frame.origin.y = maximumOriginY
}
if newValue.origin.y < minimumOriginY {
self.frame.origin.y = minimumOriginY
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,它似乎没有做任何事情.我尝试过使用DidSet属性观察器,但这会在设置后重置原点,从而导致口吃动画.关于如何使用属性观察者或其他方式的任何想法?谢谢
我目前有一个测试应用程序来自学Core Location.这是一个带有按钮的应用程序,可以在按下按钮时显示您的位置,也可以在位置服务关闭时显示警报.
我得到一个致命的错误(在解包选项值时意外地发现nil)当位置服务打开并且我尝试打印字符串时.我在文档中找不到CLLocationManager.location.coordinate返回可选值的任何地方.我将包含代码段.谢谢!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
if CLLocationManager.authorizationStatus() == .NotDetermined {
locationManager.requestWhenInUseAuthorization()
}
}
@IBAction func testLocation(sender: AnyObject) {
switch CLLocationManager.authorizationStatus() {
case .AuthorizedAlways, .AuthorizedWhenInUse:
printCurrentCoordinates()
case .Restricted, .Denied:
disabledLocationSeriveAlert()
default:
println("Failed")
}
func printCurrentCoordinates() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
println("\(locationManager.location.coordinate.latitude)")
println("\(locationManager.location.coordinate.longitude)")
}
func disabledLocationSeriveAlert() {
let alert = UIAlertController(title: "Unable to retrieve location", message: "Enable location services in system settings", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil) …Run Code Online (Sandbox Code Playgroud)