开发应用程序时,我使用扩展来隐藏代码中对特定库的依赖关系。我希望方法名称与库中的方法名称完全相同。
这种方法有助于大大减少对外部库的依赖(要将其与其他库交换,我只需要重写桥中的函数)。但是,在扩展的情况下有点棘手,因为我使用此代码得到无限循环:
// Library implementation, File1.swift
import Foundation
extension Date {
func test(otherDate: Date) -> String {
return String()
}
}
// App reference to the library, different module, File2.swift
import Foundation
import Library
extension Date {
func test(otherDate: Date) -> String {
// How to reference "Library" here?
return test(otherDate: otherDate)
}
}
Run Code Online (Sandbox Code Playgroud)
添加前缀时,该方法工作正常:
// Works correctly:
import Foundation
import Library
extension Date {
func prefix_test(otherDate: Date) -> String {
return test(otherDate: otherDate)
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 中有什么方法可以从特定模块调用扩展方法,类似于选择一个类:
// …Run Code Online (Sandbox Code Playgroud) fatalError("Descriptive Error")我的应用程序中有很多行。我想找到实际触发错误的行。
但是,我无法通过Crashlytics获得此类信息。这是我得到的:
#0. Crashed: com.apple.main-thread
0 libswiftCore.dylib 0x10173cd24 specialized _fatalErrorMessage(_:_:file:line:flags:) + 128
1 libswiftCore.dylib 0x101561118 _ArrayBuffer._checkInoutAndNativeTypeCheckedBounds(_:wasNativeTypeChecked:) + 240
2 libswiftCore.dylib 0x10157b318 Array.subscript.getter + 112
3 AppName 0x10097d520 ClassName3.select(_:) (ClassName3.swift:61)
4 AppName 0x1008ee03c ClassName2.init(_:) (ClassName2.swift:28)
5 AppName 0x1008edca0 ClassName2.__allocating_init(_:) (ClassName2.swift:24)
6 AppName 0x1008603c4 ClassName4.makeController() (ClassName4.swift:19)
7 AppName 0x1008d6c88 ClassName.ClassName6(_:didSelect:) (ClassName.swift:108)
8 AppName 0x1008d6e04 protocol witness for ClassName6Delegate.ClassName6(_:didSelect:) in conformance ClassName (<compiler-generated>)
9 AppName 0x100855de0 ClassName6.text(card:) (ClassName6Item.swift:71)
10 AppName 0x100855f04 protocol witness for ClassName5.text(card:) in conformance ClassName6 (<compiler-generated>)
11 AppName …Run Code Online (Sandbox Code Playgroud) 我正在将UICollectionView与AutoSizing单元格一起使用,其外观类似于UITableView。
正如前面的问题中提到的:q1,q2采用“自动调整大小”单元需要preferredLayoutAttributesFitting(_:)在的帮助下实现应用调整大小的方法systemLayoutSizeFitting(_ targetSize:)。
但是,在最近的WWDC会话(高性能自动布局)中,有人提到使用起来systemLayoutSizeFitting(_ targetSize:)很昂贵,因为它会创建一个新的自动布局引擎,然后将其丢弃,要求它解决所有约束,而不缓存先前计算的结果。
在我的应用中,我已经按照答案中提出的方式实现了自动调整大小。但是,在类似表格的列表(许多小高度的行)中,滚动性能非常糟糕。
显然,呼吁systemLayoutSizeFitting(_ targetSize:)在UICollectionViewCell刚刚计算出它的尺寸是昂贵而费力的CPU(因为规模估计和主线程的实际布局走位都)上。
建议同时实现以下结果的方法是什么
UICollectionView Width minus margins?同时具有AutoSizing单元和高滚动性能的推荐策略是什么?
我使用以下代码滚动到 UICollectionView 的顶部:
scrollView.scrollRectToVisible(CGRect(origin: .zero, size: CGSize(width: 1, height: 1)), animated: true)
Run Code Online (Sandbox Code Playgroud)
但是,在 iOS 11 和 12 上,scrollView 仅滚动到顶部,而不会显示UINavigationBar(whenprefersLargeTitle已设置为true.)的大标题。
我想要达到的结果:
给出以下代码:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, world!")
.padding()
Text("https://lvmh.com")
.foregroundColor(.black)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
期望的结果
后续问题
我想对电子邮件字符串应用相同的行为,例如:
Text("r.topchii@corp.lvmh.com")
Run Code Online (Sandbox Code Playgroud)
不应突出显示。
考虑以下综合场景:
import Combine
let publisher1 = PassthroughSubject<Int, Never>().eraseToAnyPublisher()
let publisher2 = PassthroughSubject<Int, Never>()
publisher1.sink { value in
publisher2.send(value)
}
Run Code Online (Sandbox Code Playgroud)
我们有 2 个发布者,我想将 的任何值传播publisher1到publisher2. 我所展示的代码可以完成这项工作,但我感兴趣的是是否有一种更清晰的声明性方法。
注: 和publisher1均为publisher2同一类型。
问题的详细信息
publisher2是“核心”类公开的 API 的一部分。“核心”类与“子”类具有“具有”关系,而“子”类又具有publisher1API。
在“核心”类的生命周期中,“子”类可以多次分配和释放。这对于“核心”类的订阅者来说应该是透明的,他们不需要订阅publisher2.
代码:
import UIKit
import Combine
class ChildClass {
let publisher1 = PassthroughSubject<Int, Never>().eraseToAnyPublisher()
}
class CoreClass {
let publisher2 = PassthroughSubject<Int, Never>()
private var childClass: ChildClass?
init() {
allocateChildClass()
}
func allocateChildClass() {
let child = …Run Code Online (Sandbox Code Playgroud) 我有一个4的UIImageVIew数组和可变大小的图像数组:[UIImage].
我想遍历视图数组并为每个视图分配4个最后的图像.
let photos = images.suffix(4)
for (index, view) in views.enumerate() {
if index <= photos.count - 1 {
view.image = photos[index]
} else {
view.image = nil
}
}
Run Code Online (Sandbox Code Playgroud)
如果中的元素不超过4个,则代码可以正常工作images.然后我崩溃了:fatal error: ArraySlice index out of range.
我index在for循环中打印出来,似乎在索引时发生了崩溃0.views.count并在同一个循环中photos.count返回4.
因此,访问0非空数组的第一个元素时出错.
在Swift 1.2中,这段代码完美无缺:
let photos = suffix(images, 4)
for (index, view) in enumerate(views) {
if index <= photos.count - 1 {
view.image …Run Code Online (Sandbox Code Playgroud) 我想要一个协议:
protocol CameraButtonDelegate: class {
func cameraButtonDidPress(_ sender: UIButton)
}
Run Code Online (Sandbox Code Playgroud)
因此,我可以将任何客户端分配给按钮,例如:
cameraButton.addTarget(delegate, action: #selector(cameraButtonDidPress), for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)
但是,它不能编译,因为我必须在中指定特定功能action,例如:
cameraButton.addTarget(delegate, action: #selector(AAPLViewController.cameraButtonDidPress), for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)
如果我想通过一个按钮将多个客户作为目标,该如何解决此问题?
我有以下与 SwiftUI 一起使用的代码:
import Foundation
public struct Trigger {
public var value = false
public mutating func toggle() {
value = true
let responseDate = Date().advanced(by: 3)
OperationQueue.main.schedule(after: .init(responseDate)) {
moveBack()
}
}
private mutating func moveBack() {
value = false
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误:
转义闭包捕获变异的“self”参数
我知道将结构更改为类可以解决这个问题,但是有没有办法在结构中的转义闭包中实际捕获变异的 self ?
I have a function with a completion handler, returning one parameter or more.
In a client, when executing a completion handler, I'd like to have an unowned reference to self, as well as having access to the parameter passed.
Here is the Playground example illustrating the issue and the goal I'm trying to achieve.
import UIKit
struct Struct {
func function(completion: (String) -> ()) {
completion("Boom!")
}
func noArgumentsFunction(completion: () -> Void) {
completion()
}
}
class Class2 { …Run Code Online (Sandbox Code Playgroud) swift ×10
ios ×8
closures ×2
arrays ×1
autolayout ×1
class ×1
combine ×1
crash ×1
crash-dumps ×1
crashlytics ×1
delegation ×1
enumeration ×1
module ×1
publisher ×1
scrollview ×1
struct ×1
swift2 ×1
swiftui ×1
text ×1
uibutton ×1