iOS-Coder
iOS-Coder

Reputation: 1241

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!

Upvotes: 1

Views: 1787

Answers (2)

Jasper Reddin
Jasper Reddin

Reputation: 26

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

Upvotes: 0

Chris Wagner
Chris Wagner

Reputation: 20993

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

Upvotes: 5

Related Questions