基本上我想要的是类属性的临时别名,以提高可读性.
我遇到以下代码描述的情况,我看不到一个简单的解决方案.我想避免的是y复制突变然后复制回来.重命名y会大大降低实际算法的可读性.
Swift编译器是否足够智能,不能实际分配新内存,我怎么能知道呢?
如果没有,如何防止复制?
class myClass {
var propertyWithLongDescriptiveName: [Float]
func foo() {
var y = propertyWithLongDescriptiveName
// mutate y with formulas where y corresponds to a `y` from some paper
// ...
propertyWithLongDescriptiveName = y
}
// ...
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有动态填充行的 UITableView,但顶部还有一个部分,其中包含一个始终相同的特殊单元格(具有不同的标识符)。
我在这个单元格中添加了两个按钮,它们确实可以工作,但反应很差。也就是说,大约0.25秒后才会出现突出显示。
我正在使用以下稍微定制的按钮:
import UIKit
class HighlightingButton: UIButton {
override var isHighlighted: Bool {
didSet {
if isHighlighted {
backgroundColor = UIColor.lightGray
} else {
backgroundColor = UIColor.white
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
用户获得点击按钮的清晰反馈非常重要。然而,由于缓慢的突出显示,这并不令人满意,尽管事件似乎被快速触发(通过打印一些输出来判断)。
在普通视图中,此突出显示按钮按预期工作,并且突出显示会随着我的点击而快速闪烁。
UITableViewCell 的事件处理中是否存在导致这种缓慢的原因?
更新
我创建了一个简约的示例项目来演示该问题。没有任何手势识别器,但仍然存在非常明显的延迟。
下面的代码有效,并为我提供了一个可拖动的注释视图。但是,我注意到注释视图从一开始就不能拖动,而只能在手指在注释视图上停留片刻之后才能拖动。当直接进入拖动运动时,拖动不会影响注释视图,而是平移地图。这当然不像拖放的感觉。无论是在设备上还是在模拟器中。
ViewController(MapView 的委托)
override func viewDidLoad() {
/// ...
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(addPin))
mapView.addGestureRecognizer(gestureRecognizer)
}
func addPin(gestureRecognizer: UIGestureRecognizer) {
if gestureRecognizer.state != UIGestureRecognizerState.Began {
return
}
for annotation in mapView.annotations {
mapView.removeAnnotation(annotation)
}
let touchLocationInView = gestureRecognizer.locationInView(mapView)
let coordinate = mapView.convertPoint(touchLocationInView, toCoordinateFromView: mapView)
let annotation = DragAnnotation(coordinate: coordinate, title: "draggable", subtitle: "")
mapView.addAnnotation(annotation)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKindOfClass(DragAnnotation) {
let reuseIdentifier = "DragAnnotationIdentifier"
var annotationView: MKAnnotationView!
if let dequeued = …Run Code Online (Sandbox Code Playgroud) 在DateFormatter如下面的结构的初始化中使用a 时,缓存该格式化程序的最简洁方法是什么?
struct Entry {
let date: Date
// ...
init?(string: String) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
if let date = dateFormatter.date(from: string) {
self.date = date
} else {
return nil
}
// ...
}
}
Run Code Online (Sandbox Code Playgroud)