简体   繁体   中英

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. So any help would be very welcome, thanks in advance!

This is very interesting, seems buggy as Nate mentions. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM