4

I was trying to initialize an array containing optional values using the repeatedValues initializer and I was surprised to discover the following code doesn't compile

let a: Int?[] = Int?[](count: 10, repeatedValue:nil)
// error - Value of Int?[]? not unwrapped; did you mean to use '!' or '?'?

What's interesting is that type signature Int?[]?, e.g. an optional Array of optional Int. This feels like a bug but maybe there's something I'm missing about the grammar. I've looked through the language reference some but have yet to find an answer.

The more explicit Array<Int?> type initializer works as expected

let b: Int?[] = Array<Int?>(count: 10, repeatedValue:nil)
// compiles fine

Has anyone else run into this and can shed some light?

EDIT

Couple extra working examples with non-optional types to highlight the failure

let c: Int[] = Int[](count: 10, repeatedValue:0)
// non-optional shorthand works fine

class D { var foo = 1 }
let d: D[] = D[](count:10, repeatedValue:D())
// custom class works fine using the shorthand too

enum E { case a, b, c, d, e }
let e: E[] = E[](count:10, repeatedValue:.e)
// enums work too
3
  • Don't know if this helps shed any light on the issue, but if you let the compiler infer the type, i.e. let a = Int?[](count: 10, repeatedValue:nil), it compiles. However, a is described in the Playground view as {[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]}, not [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], which seems to imply that the actual type isn't what you're expecting with that particular initializer. I haven't learned enough about Swift yet to know what it all means, but thought I'd mention it anyway. Commented Jun 10, 2014 at 5:33
  • @Aky Those outer braces are there cause the array itself is optional too, not just the contained type. You can see this if you explicitly add the type to the declaration - let a: Array<Int?>? = Int?[](count:10, repeatedValue:nil) Commented Jun 10, 2014 at 5:39
  • 1
    @BryanChen That doesn't compile because Int[] and Int?[] are two different types. The first is an array of integers (Array<Int>) while the second is an array of optional integers (Array<Int?>) Commented Jun 10, 2014 at 5:47

2 Answers 2

5

Swift 3:

   let pageViews = [UIImageView?](repeating: nil, count: pageCount)

Swift 2:

    let pageViews = [UIImageView?](count: pageCount, repeatedValue: nil)
Sign up to request clarification or add additional context in comments.

Comments

0

you can use

let a: Int?[] = Array(count: 10, repeatedValue:nil)

for code

let a: Int?[] = Int?[](count: 10, repeatedValue:nil)

I think the compiler is reading Int?[] as calling ?[] on Int (like parsing a?[1]) and hence the compiler error. This looks like compiler bug to me.

this also compile

let a: Int?[] = Optional<Int>[](count: 10, repeatedValue:nil)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.