使用'任何'和任何对象' Swift Linux中的类型会产生错误

时间:2017-07-20 05:58:43

标签: swift linux perfect

我有这行代码:

produceJSONMessage(message: message as AnyObject)

在XCode(Mac)中可行。但是使用swift build在linux中构建它会产生错误:

/home/ubuntu/x/x/objects.swift:x:x: error: 'Any' is not convertible to 'AnyObject'; did you mean to use 'as!' to force downcast?

produceJSONMessage(message: message as AnyObject)

所以我使用produceJSONMessage(message: message as! AnyObject)来遵循它的建议。它在构建期间不会抛出错误,但在运行时会崩溃:

Could not cast value of type 'Any' (0x9aab88) to 'Swift.AnyObject' (0x7f7c84007c88).

无论如何,我得到它来构建而不是抛出错误:

produceJSONMessage(message: message as? AnyObject)

新问题是当函数接收到对象时,它总是为nil(在进入函数之前它不是nil)。这是函数签名:

func produceJSONMessage(message: AnyObject? = nil)

其中message通常是字符串:任何类型或普通字符串

我应该注意哪些事情?我发布的所有代码组合都适用于Mac XCode。

1 个答案:

答案 0 :(得分:0)

我通过从上面的代码中移除cgi-bin并将其替换为AnyObject,使其成功。也许我误用了Any类型,但它没有帮助它在Mac中构建好但在Linux中没有!