我正在尝试从我的 swift mac 应用程序运行终端命令,我希望用户像在终端中一样输入任何命令,而不必指定真实路径。有没有办法运行命令并使其在 $PATH 变量中指定的每个路径中查找?
我可以在指定路径时运行命令,但我需要它从系统 $PATH 变量中的任何路径自动查找二进制文件
let path = "/usr/bin/killall"
let arguments = ["Dock"]
let task = Process.launchedProcess(launchPath: path, arguments: arguments)
task.waitUntilExit()
Run Code Online (Sandbox Code Playgroud)
将来我想读取 .sh 文件并运行其中的每一行,因此对于长脚本,并非所有二进制文件都在默认路径中。
谢谢!
所以我试图读取下面“食谱”下的 autoID 子项的子项是我的 Firebase 数据库的图片,下面是应该检索“描述”和“名称”的值并将它们插入到变量中的方法.
我目前在运行应用程序时遇到的错误是:
Could not cast value of type '__NSCFString' (0x10ad2afb8) to 'NSDictionary' (0x10ad2bfa8).
Run Code Online (Sandbox Code Playgroud)
ref = Database.database().reference()
databaseHandle = ref?.child("Recipes").observe(.childAdded) { (snapshot) in
for snap in snapshot.children
{
let recipeSnap = snap as! DataSnapshot
let recipeID = recipeSnap.key
let dict = recipeSnap.value as! [String:AnyObject]
let recipeName = dict["Name"] as! String
let recipeDescription = dict["Description"] as! String
print("key = \(recipeID) and name = \(recipeName) and description = \(recipeDescription)")
}
}
Run Code Online (Sandbox Code Playgroud)
打印语句仅用于测试。
在我的应用程序中,当用户向下滑动视图时,我希望视图关闭到某个帧。这是我迄今为止尝试过的,但由于某种原因,它一直给我这个错误:
“#selector”的参数不引用“@objc”方法、属性或初始值设定项
这是我的代码如下:
class VideoLauncher: NSObject {
@objc func dismissView(myView: UIView) {
UIView.animate(withDuration: 0.4) {
if let theWindow = UIApplication.shared.keyWindow {
myView.frame = CGRect(x:theWindow.frame.width - 15 , y: theWindow.frame.height - 15, width: 10 , height: 10)
}
}
}
func showVideoPlayer() {
print("showing player")
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.backgroundColor = UIColor.spaceBlue
view.frame = CGRect(x: keyWindow.frame.width - 10, y: keyWindow.frame.height - 10, width: 10, height: 10)
let videoPlayerView = VideoPlayerView(frame: keyWindow.frame)
keyWindow.addSubview(view)
view.addSubview(videoPlayerView)
let …Run Code Online (Sandbox Code Playgroud) 基本上我想将以下值打印为“true”、“false”或“nil”。如果我尝试仅使用包装的值,我会得到我想要的“nil”,但会获得不需要的“Optional(true)”或“Optional(false)。如果我强制解开该值并且值为 nil,则会出现致命错误。我尝试了下面的代码,因为我已经看到它适用于字符串,但是因为“nil”不是 Bool 类型,所以它不被接受。有什么解决方案吗?
var isReal: Bool?
String("Value is \(isReal ?? "nil")")
Run Code Online (Sandbox Code Playgroud)
我正在导出到 csv 文件,了解此值是真还是假或尚未检查很有用。
我有一个 segue,它是通过一个按钮触发的,该按钮在当前上下文中以模态方式显示另一个视图控制器。我正在使用自定义类来自定义过渡,这是这样的:
class ShowAnimator: NSObject {
}
extension ShowAnimator: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard
let fromVC = transitionContext.viewController(forKey: .from),
let toVC = transitionContext.viewController(forKey: .to) else {
return
}
let containerView = transitionContext.containerView
containerView.insertSubview(fromVC.view, belowSubview: toVC.view)
let toFrame = transitionContext.finalFrame(for: toVC)
let screenBounds = UIScreen.main.bounds
let bottomLeftCorner = CGPoint(x: screenBounds.width, y: 0)
let finalFrame = CGRect(origin: bottomLeftCorner, size: screenBounds.size)
UIView.animate(withDuration: transitionDuration(using: transitionContext),
animations: {
fromVC.view.frame = finalFrame
}, …Run Code Online (Sandbox Code Playgroud) 现在我正在使用参数发出 Alamofire 请求。在发出请求之前我需要最终 URL,因为我需要散列最终 URL 并将其添加到请求标头中。我就是这样做的,但它没有给我最终的 URL 来散列并放入标题中。
Alamofire.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON
Run Code Online (Sandbox Code Playgroud)
我想在发出此请求之前获取编码的 URL,因此请求如下所示
Alamofire.request(url, method: .get, headers: headers).responseJSON
Run Code Online (Sandbox Code Playgroud)
现在作为一种解决方法,我通过手动附加每个参数来手动创建 URL。有没有更好的方法来做到这一点?
let rexUrl = "https://www.google.com"
let requestPath = "/accounts"
let url = rexUrl + requestPath + "?apikey=\(apiKey)&market=USD&quantity=\(amount)&rate=\(price)&nonce=\(Date().timeIntervalSince1970)"
Run Code Online (Sandbox Code Playgroud) 我试图在启动时在第一个视图控制器之上呈现第二个视图控制器,而不会关闭第一个视图控制器。这篇文章Swift 3 - 在启动时加载多个 ViewControllers看起来确实有答案。它建议:
添加您的主视图控制器
var secondViewController:UIViewController!
Run Code Online (Sandbox Code Playgroud)
在您的 viewDidLoad 中:
secondViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourIdentifier") as! SecondViewController
Run Code Online (Sandbox Code Playgroud)
就是这样。当您想展示它时,请使用:
self.present(secondViewController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
例如,如果我将它作为一个动作附加到按钮上,那么第三行效果很好。但是,如果它在第一个 viewController 的 viewDidLoad: 中,则它不起作用。这就是我需要的。
如何在启动时自动在第一个 viewController 之上显示第二个 viewController?
我有两个日期,即2/02/2016和19/03/2018
我正在尝试获取这些日期之间的月份和年份
输出应该是
2016 年 2 月、2016 年 3 月……等等……2018 年 1 月、2018 年 2 月、2018 年 3 月。
试过月间隙代码 -
let date1 = DateComponents(calendar: .current, year: 2016, month: 2, day: 2).date!
let date2 = DateComponents(calendar: .current, year: 2018, month: 3, day: 19).date!
let monthGap = Calendar.current.dateComponents([.month], from: date1, to: date2)
print("monthGap is \(String(describing: monthGap.month))")//prints monthGap is 25
Run Code Online (Sandbox Code Playgroud) 我通过可可豆提取 OHHTTPStubs,但我遇到了一些我无法弄清楚原因的问题。这是我的 podFile 的副本
# platform :ios, '11.3'
target 'myApp' do
use_frameworks!
target 'myAppTests' do
inherit! :search_paths
pod 'OHHTTPStubs/Swift'
end
end
Run Code Online (Sandbox Code Playgroud)
在我的测试文件中:
import OHHTTPStubs
@testable import myApp
class myTest: XCTestCase {
var client: CryptoCompareClient!
override func setUp() {
super.setUp()
OHHTTPStubs.onStubMissing { request in
XCTFail("Missing stub for \(request)")
}
}
Run Code Online (Sandbox Code Playgroud)
在这一行:
OHHTTPStubs.onStubMissing { request in
XCTFail("Missing stub for \(request)")
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Type 'OHHTTPStubs' has no member 'onStubMissing'
Run Code Online (Sandbox Code Playgroud)
此外,我正在尝试使用 OHHTTPStubs 从包中加载文件:
let bundle = OHResourceBundle("myFixtures", FixtureLoader.self)!
let path = OHPathForFileInBundle(filename, bundle)!
return …Run Code Online (Sandbox Code Playgroud) 我一直在使用以下UIView扩展来改变视图:
func shake(count: Float = 4, for duration: TimeInterval = 0.5,
withTranslation translation: CGFloat = 5) {
let animation: CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x")
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.repeatCount = count
animation.duration = duration / TimeInterval(animation.repeatCount)
animation.autoreverses = true
animation.fromValue = NSValue(cgPoint: CGPoint(x: -translation, y: self.center.y))
animation.toValue = NSValue(cgPoint: CGPoint(x: translation, y: self.center.y))
layer.add(animation, forKey: "shake")
}
Run Code Online (Sandbox Code Playgroud)
以及用法:
view.shake(count: 3, for: 0.2, withTranslation: 8)
Run Code Online (Sandbox Code Playgroud)
这适用于摇动视图(在我的情况下,我正在摇动一些按钮、UIView 和一些图像视图)。当我尝试在抖动动画期间单击其中一个视图时出现问题。
我收到:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSConcreteValue doubleValue]:无法识别的选择器发送到实例 0x600000442640”
在按钮的情况下,如果我禁用按钮然后启动抖动动画,它似乎可以防止错误发生。不幸的是,这似乎不适用于图像视图和 UIViews。
我还禁用了图像视图/ UIView 上的用户交互,但无济于事。
我觉得这与在对视图进行动画处理时在命中测试检查期间将错误的坐标传递给较低层有关。
对此错误的任何见解将不胜感激。