我有一个带有按钮的 SwiftUI 组件,我想在不同的地方使用它。执行按钮时,应根据视图执行不同的操作。
一种解决方案是我将 ViewModel 传递给组件。但是,该解决方案无法很好地适应不同的 ViewModel。
我觉得使用回调的替代方法也不太好。因为组件的层次结构非常深。
我的想法是创建一个 CommandAction 类。但是,我在方法签名上失败了。
我的想法就是这堂课。
class CommandActions {
// MARK: Lifecycle
init(action: @escaping (( _ parameter1: String) async throws -> String)) {
self.action = action
}
// MARK: Internal
let action: ( _ parameter1: String) async throws -> String
}
Run Code Online (Sandbox Code Playgroud)
该函数应该被执行。
private func doSomeAction(parameter1: String) async throws -> String {
await Task.sleep(seconds: 1)
return "Some Result"
}
Run Code Online (Sandbox Code Playgroud)
当我想开始上课时,我失败了。
CommandActions(action: doSomeAction(parameter1: "test"))
Run Code Online (Sandbox Code Playgroud)
失败的是:Cannot convert value of type 'String' to expected argument type '(String) …
我为我的 Android 应用程序实现了通用链接。到目前为止一切正常。我点击通用链接,将立即重定向到该应用程序。但是,我一直使用 debug 来进行Digital Asset Links
. 对于该版本,我需要签名配置或密钥库文件。
为了签署我的应用程序,我总是使用创建的xxxx.jks
文件。如果我指定这一点,则会发生错误:(Error occured while trying to get the SHA-256 fingerprint
参见屏幕截图)。
该错误是非常没有意义的。这就是为什么我有点困惑。我使用了正确的文件吗?如何获得更好的错误描述?我总是使用此文件来签署我的应用程序以获取新版本。这不会造成任何问题。
我已经从 Play Console 的应用程序签名证书中获取了 SHA-256 指纹。这确实不能正常工作。每当您单击我的通用链接时,应用程序都会询问:Should the link be opened with Chrome or with 'MyApp'?
我很感谢您的意见。
android keystore kotlin android-keystore digital-assets-links
我使用NSLengthFormatter显示正确的实体,如米,脚,码等.
该方法有效,但我没有得到正确数量的小数位数.
let distance = getDistanceAsDouble() //636.62084358567 in meters
let format = String(format: "%.1f",distance) //636.6
if let formattedDouble = Double(format) {
let lengthFormatter = NSLengthFormatter()
let formattedDistance = lengthFormatter.stringFromMeters(formattedDouble)
lbl.text = "\(formattedDistance)" // printed: 697.936 yd
}
Run Code Online (Sandbox Code Playgroud)
我用Google搜索,但我找不到如何配置NSLengthFormatter.NSNumberFormatter具有类似numberStyle
或的属性maximumFractionDigitis
,但NSLengthFormatter没有...或?
我试图施放formattedDistance
到Double,我可以String(format: ""...
再次使用.这不起作用,因为formattedDistance
包含"yd"(或其他实体).
我该如何解决这个问题?
用户移动地图时会调用以下函数.
有可能获得视口吗?视口是北/东和纬度的经度和纬度.和lon.南/西.
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool){
print("The \(mapView.centerCoordinate)")
print("the maprect \(mapView.visibleMapRect.origin)")
print("the maprect \(mapView.visibleMapRect.size)")
//I find just this attributes for mapView
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何从我的数据中获取视口.我在www中找不到任何东西.
目的地是我使用谷歌分析记录坐标,另一个工具评估数据.
有人有想法吗?非常感谢
该应用程序仅支持肖像模式。应显示两个问题,但它们会相应轮换。因此,无论玩家坐在哪里,都可以清楚地看到问题。
不管怎样,旋转时边界框似乎没有旋转。
ZStack {
HStack {
GeometryReader { proxy in
Text(challenge)
.rotationEffect(Angle(degrees: 90), anchor: .center)
.frame(maxWidth: proxy.size.height, minHeight: proxy.size.width)
.foregroundColor(.red)
.font(.largeTitle)
.background(Color.blue)
}
Spacer()
GeometryReader { proxy in
Text(challenge)
.rotationEffect(Angle(degrees: 270), anchor: .center)
.frame(maxWidth: proxy.size.height, minHeight: proxy.size.width)
.foregroundColor(.red)
.font(.largeTitle)
.background(Color.blue)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试过很多不同的事情。我省略了GeometryReader
并使用“fixedSize()”。然后我看到屏幕上出现了一行字。
我也尝试过这个解决方案,但它没有按预期工作。
我的结果始终是不使用全宽度的文本。(重叠只是另一个错误,但我一定会控制住它)。
我编写了自己的Button,Textfield,...类。在“自定义类”的情节提要中,将类设置为UIElement。这很好。
现在,我需要以编程方式添加的工具栏。当我在ViewController中添加工具栏时,一切都很好。但是我想像这样创建自己的工具栏类。
class MyOwnToolbar : UIToolbar {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//never called
self.backgroundColor = UIColor.redColor()
self.tintColor = UIColor.greenColor()
self.barTintColor = UIColor.blueColor()
}
override init(frame: CGRect) {
//error: super.init isn'T called on all paths before returning from initiliazer
}
Run Code Online (Sandbox Code Playgroud)
在我的ViewController中,我尝试这样调用:
fromToolBar = MyOwnToolBar() //call nothing?
fromToolBar = MyOwnToolBar(frame: CGRectMake(0,0,0,0)) //doesn't work because init(frame: CGRECT) doesnt work
Run Code Online (Sandbox Code Playgroud)
我的ViewController中的旧代码有效:
self.untilToolBar = UIToolbar(frame: CGRectMake(0,0,0,0))
untilToolBar?.backgroundColor = redColor
untilToolBar?.tintColor = greenColor
untilToolBar?.barTintColor = blueColor
Run Code Online (Sandbox Code Playgroud)
因此,我可以使用有效的解决方案,但我想理解为什么我的代码无法正常工作。因此,也许有人有解决方案或良好的联系。
我想创建一种动态方式来返回按参数 xy 排序的列表。该列表可以按降序、升序、ID、用户名、邮件等排序。
我以字符串形式接收此参数。所以例如sort=-username
减号表示列表降序。排序参数是用户名。
所以我回来了 user.OrderByDescending(o => o.Username).ToList();
目前,在长 if-else 构造的帮助下,我检测到需要哪种排序。
我希望我可以在对象参数的函数的帮助下替换排序字符串参数。
伪代码
//input for example: sort=-username
Boolean isAscending = isAscending(sort) //return true or false
var parameter = isSortStringInsideObject(sort) //
if (isAscending) {
user.OrderBy(o => o.parameter).ToList();
} else {
user.OrderByDescending(o => o.parameter).ToList();
}
Run Code Online (Sandbox Code Playgroud)
所以参数可以是对象中的每个参数。
我是 .net 核心的新手。所以我希望我没有制定一个乌托邦式的要求。