我在我的macOS命令行工具项目中添加了一些JSON文件,但我似乎无法通常的方式找到它们.
if let path = Bundle.main.path(forResource: "Data", ofType: "json")
{
if let contents = try? String(contentsOfFile: path)
{
print (contents)
}
else { print("Could not load contents of file") }
}
else { print("Could not find path") }
Run Code Online (Sandbox Code Playgroud)
在基于视图的应用程序中使用时,此代码可以正常工作,但始终在命令行工具中打印"无法找到路径".
有谁知道我做错了什么?
PS:完全清楚我应该使用guard这个语句,但代码不在函数中,只是在main.swift中讨论,直到我弄明白这一点.
我正在尝试为数组中的每个元素协调几个完成处理程序.
代码基本上是这样的:
var results = [String:Int]()
func requestData(for identifiers: [String])
{
identifiers.forEach
{ identifier in
service.request(identifier, completion: { (result) in
result[identifier] = result
})
}
// Execute after all the completion handlers finish
print(result)
}
Run Code Online (Sandbox Code Playgroud)
因此,Array中的每个元素都通过带有完成处理程序的服务发送,并且所有结果都存储在一个数组中.完成所有这些处理程序后,我希望执行一些代码.
我试图这样做 DispatchQueue
var results = [String:Int]()
func requestData(for identifiers: [String])
{
let queue = DispatchQueue.init(label: "queue")
identifiers.forEach
{ identifier in
service.request(identifier, completion: { (result) in
queue.sync
{
result[identifier] = result
}
})
}
// Execute after all the completion handlers finish
queue.sync
{
print(result) …Run Code Online (Sandbox Code Playgroud) 我extension Array的形式是:
extension Array
{
private func someFunction(someClosure: (() -> Int)?)
{
// Do Something
}
func someOtherFunction(someOtherClosure: () -> Int)
{
someFunction(someClosure: someOtherClosure)
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了错误:Passing non-escaping parameter 'someOtherClosure' to function expecting an @escaping closure.
这两个闭包确实是非转义的(默认情况下),并且显式地添加@noescape以someFunction产生警告,指示这是Swift 3.1中的默认值.
知道我为什么会收到这个错误吗?
我正在尝试在其父视图底部的视图中设置动画。通过设置偏移动画可以相对容易地做到这一点,如下所示:
struct ContentView: View {
@State var isShowingBanner = true
var bannerOffset: CGFloat {
isShowingBanner ? 0 : 60
}
var body: some View {
VStack {
VStack {
Spacer()
BannerView()
.offset(y: bannerOffset)
}
.border(Color.black, width: 1.0)
.clipped()
Spacer()
Button("Toggle Banner") {
withAnimation {
isShowingBanner.toggle()
}
}
}
.padding()
}
}
Run Code Online (Sandbox Code Playgroud)
明显的问题是,这只是使用任意值作为动画偏移,并且在考虑动态类型时很快就会崩溃
我的问题是:
有没有办法正确确定高度BannerView以正确调整此动画。或者有没有更好的方法来达到这个效果?
谢谢大家
有没有办法在Xcode中运行post-actions脚本,只有在项目已经在特定的构建配置中运行时(发生在Release中,不在Debug中).
非常感谢