我刚尝试了一个HackerRank挑战,如果一个问题给你x行输入,把x行设为let someVariable = readLine()根本就不会削减它,因为有很多测试用例可以拍摄更多输入到我们编写的代码,因此每行输入的硬编码readLine()都不会飞.
有没有办法让多行输入到一个变量?
在 xCode 9 之前,当我想用图像创建 UIImageView 时,我会将图像拖到项目侧栏中并使用该文件的名称创建一个 UIImage。这很有效,一切都很好。
但是,使用 Xcode 9 这将不起作用。它将无法找到该文件,即使它在主包中。我需要做的是将图像放入Assets文件夹,然后它就能够找到图像并将其显示在UIImageView中。
同样,在使用 Sprite Kit 时,我会在我的桌面上创建一个扩展名为 .atlas 的文件夹,将我的所有图像拖到该文件夹中,然后将 .atlas 文件夹拖到项目中。然后我会用 .atlas 文件夹的名称创建一个 SKTextureAtlas,并且它起作用了。然而,使用 Xcode 9,这再次不起作用。我会在控制台中收到“找不到纹理图集 'foo'”消息。所以我转到 Assets 文件夹,单击添加图标并选择 New Sprite Atlas,重命名该文件夹,然后将我的图像拖入其中。现在使用该名称创建纹理图集工作。
我现在正在尝试做的并且遇到问题是使用 init(filedNamed: "foo.mov") 创建一个 SKVideoNode,正如您所猜测的,如果您将文件拖到项目的侧栏中,这将不再有效。所以我想也许我再次需要通过 Assets 文件夹以某种方式执行此操作。我发现当我将我的 .mov 文件添加到 Assets 文件夹时,它被创建为“数据”类型(“数据”显示在资产文件夹中其框的右上角)。我现在能做的是:
let d = NSDataAsset(name: "foo") // 'foo' is the .mov file in Assets
d.data // the underlying content
Run Code Online (Sandbox Code Playgroud)
结果 d 不是 nil,这是个好消息,但访问底层内容的唯一方法只是通过 .data,并且您不能使用数据创建 SKVideoNode。我还想加载一个声音文件,我以前是这样做的:
let a = SKAction.playSoundFileNamed("bar.mp3", waitForCompletion: false)
Run Code Online (Sandbox Code Playgroud)
但我收到消息“加载声音资源时出错:“bar.mp3””。
如果我需要监听通知,我总是这样做:
NotificationCenter.default.addObserver(self, selector: #selector(foo), name: UIApplication.didEnterBackgroundNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)
但是,如果我在异步方法体内移动该行,编译器突然希望将该行标记为等待:
func setup() async {
let importantStuff = await someTask()
// store my important stuff, now setup is complete
// This line won't compile without the await keyword.
// But addObserver is not even an async method
await NotificationCenter.default.addObserver(self, selector: #selector(foo), name: UIApplication.didEnterBackgroundNotification, object: nil)
// But this works fine
listen()
}
func listen() {
NotificationCenter.default.addObserver(self, selector: #selector(foo), name: UIApplication.didEnterBackgroundNotification, object: nil)
}
Run Code Online (Sandbox Code Playgroud)
最小的例子
此代码无法编译(编译器想要添加awaitaddObserver 调用)。
class Test {
func …Run Code Online (Sandbox Code Playgroud)