简体   繁体   English

快速错误类型“ T”不符合协议“ IntegerLiteralConvertible”

[英]Swift error Type 'T' does not conform to protocol 'IntegerLiteralConvertible'

I want to factorize a number into its prime factors with the following code fragment, but I don't understand fully the given error-message (see in above title). 我想通过以下代码片段将数字分解为其主要因子,但我不完全理解给定的错误消息(请参见上面的标题)。 First I tried using a Dictionary but I got stuck on sorting this dictionary by keys. 首先,我尝试使用字典,但是我坚持按键对字典进行排序。 Second I tried the tuple-version but now I'm stuck with the compiler-error. 其次,我尝试了元组版本,但现在我陷入了编译器错误。

Can anybody see whats wrong in the last line of the following code fragment? 有人可以在下面的代码片段的最后一行看到什么地方出问题吗?

var pfc : [(prime: Int, count: Int)] = []
pfc.append(prime: 2, count: 2)
pfc += [(prime: 3, count: 4)]
var p = 5, c = 1
pfc.append(prime: p, count: c)

In stack overflow similar questions can be found regarding String.Index, but the answers give me not enough clues yet. 在堆栈溢出中,可以找到有关String.Index的类似问题,但是答案还不足以为我提供线索。 So any help would be very welcome, thanks in advance! 因此,非常感谢您的帮助,在此先感谢您!

This is very interesting, seems buggy as Nate mentions. 这是非常有趣的,就像Nate提到的那样似乎很麻烦。 I was able to work around it through some different syntax. 我可以通过一些不同的语法解决它。

var pfc : [(prime: Int, count: Int)] = []

pfc.append(prime: 2, count: 2)

pfc += [(prime: 3, count: 4)]

var p = 5
var c = 1

var tuple = (prime: p, count: c)

pfc += [tuple]

pfc

I think you should put the tuple in two sets of parenthesis instead of just one. 我认为您应该将元组放在两组括号中,而不是一组。

var pfc : [(prime: Int, count: Int)] = []
pfc.append((prime: 2, count: 2))
pfc += [(prime: 3, count: 4)]
var p = 5, c = 1
pfc.append((prime: p, count: c))

I think that the compiler thought you wanted to call a method called Array.append(prime: Int, count: Int) but that method couldn't be found for type Array 我认为编译器认为您想调用一个名为Array.append(prime: Int, count: Int)方法,但是找不到Array类型的方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“T”不符合协议“AnyObject” - Type 'T' does not conform to protocol 'AnyObject' 类型不符合协议'AVCaptureFileOutputRecordingDelegate' - Type does not conform to protocol 'AVCaptureFileOutputRecordingDelegate' xcode8更新错误; 类型(控制器)“不符合协议'WCSessionDelegate' - xcode8 update error; Type (controller) "does not conform to protocol 'WCSessionDelegate' 类型“ Double”不符合协议“序列类型” - Type 'Double' does not conform to protocol 'Sequence Type' 类型'[String,AnyObject?]'不符合协议AnyObject?:为什么? - Type '[String, AnyObject?]' does not conform to protocol AnyObject?: why? 错误:不兼容的类型:推断的类型不符合推断的上限:INT#1 上限: - error: incompatible types: inferred type does not conform to upper bound(s) inferred: INT#1 upper bound(s): 这个深奥的泛型错误是编译器错误还是新限制? (推断类型不符合上限) - Is this esoteric generics error a compiler bug or a new restriction? (inferred type does not conform to upper bounds) 参数类型'customClass.Type'不符合预期类型'NSItemProviderWriting' - Argument type 'customClass.Type' does not conform to expected type 'NSItemProviderWriting' 参数类型 '()' 不符合预期类型 'View' SwiftUI? - Argument type '()' does not conform to expected type 'View' SwiftUI? 协议定义中的编译器错误“预期类型” - Compiler error “expected a type” in protocol definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM