[英]Compiler hangs indefinitely while compiling multiple files - Swift 5.5 Release
As the question states, compiling and running the 2 files below makes the compiler hang indefinitely.正如问题所述,编译和运行下面的 2 个文件会使编译器无限期挂起。 Here are the CLI commands I have tried:
swiftc *.swift -o combined &&./combined
and cat *.swift | swift -
以下是我尝试过的
swiftc *.swift -o combined &&./combined
命令cat *.swift | swift -
cat *.swift | swift -
. cat *.swift | swift -
。
The only 2 swift files in the directory are main.swift
and Vehicle.swift
.目录中仅有的2个swift文件是
main.swift
和Vehicle.swift
。
I have tried compiling and running both files separately, Vehicle.class
has no errors and compiles properly, main.swift
has a error: cannot find 'Vehicle' in scope
error, which is understandable since I need to compile them together.我试过分别编译和运行这两个文件,
Vehicle.class
没有错误并且编译正确, main.swift
有一个error: cannot find 'Vehicle' in scope
因为需要一起编译,这是可以理解的。 There is no other feedback from the compiler.编译器没有其他反馈。 What can cause the compiler to hang?
什么会导致编译器挂起?
Code:代码:
main.swift main.swift
// Stub program to demonstrate the Vehicle class
let vehicle1 = Vehicle(newNumOfDoors: 4, newMaxSpeed: 150,
newLicensePlate: "ASDF404", newColour: "Red")
vehicle1.licensePlate = "FGHJ968"
vehicle1.colour = "Green"
vehicle1.accelerate(accelerateBy: 60)
print("\nLicense Plate: " + vehicle1.licensePlate
+ "\nColour: " + vehicle1.colour
+ "\nNumber of Doors: " + vehicle1.numOfDoors
+ "\nMax Speed: " + vehicle1.maxSpeed
+ "\nCurrent Speed: " + vehicle1.speed)
Vehicle.swift车辆.swift
public class Vehicle {
// Properties Declaration
let numOfDoors: Int
let maxSpeed: Int
private(set) var speed: Int
var licensePlate: String
var colour: String
// Initializes a Vehicle
init (newNumOfDoors: Int, newMaxSpeed: Int,
newLicensePlate: String, newColour: String) {
self.numOfDoors = newNumOfDoors
self.licensePlate = newLicensePlate
self.maxSpeed = newMaxSpeed
self.colour = newColour
self.speed = 0
}
func accelerate(accelerateBy: Int) {
self.speed += accelerateBy
}
func brake(brakeBy: Int) {
self.speed -= brakeBy
}
}
The compiler is having issues with your print
statement (which I determined by removing elements from the program until it worked).编译器与您的
print
语句有问题(我通过从程序中删除元素直到它起作用来确定)。 This is likely because it's struggling to figure out the type when using +
to concatenate everything.这可能是因为它在使用
+
连接所有内容时难以确定类型。
One option is to use a multi-line string literal:一种选择是使用多行字符串文字:
let vehicle1 = Vehicle(newNumOfDoors: 4, newMaxSpeed: 150,
newLicensePlate: "ASDF404", newColour: "Red")
vehicle1.licensePlate = "FGHJ968"
vehicle1.colour = "Green"
vehicle1.accelerate(accelerateBy: 60)
let str = """
License Plate: \(vehicle1.licensePlate)
Colour: \(vehicle1.colour)
Number of Doors: \(vehicle1.numOfDoors)
Max Speed: \(vehicle1.maxSpeed)
Current Speed: \(vehicle1.speed)
"""
print(str)
Another option is to use interpolation rather than the +
:另一种选择是使用插值而不是
+
:
print("\nLicense Plate: \(vehicle1.licensePlate)"
+ "\nColour: \(vehicle1.colour)"
+ "\nNumber of Doors: \(vehicle1.numOfDoors)"
+ "\nMax Speed: \(vehicle1.maxSpeed)"
+ "\nCurrent Speed: \(vehicle1.speed)")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.