0

I have an array of nullable items which may or may not have any nils in it:

let source = [1, 2, 3] as [Int?]
// or
let source = [1, 2, nil] as [Int?]

I want to turn it into an [Int]? with the values as Ints if no items are nil or nil if any item is nil.

What's the idiomatic way to do this?

Things I've tried:

// fails with: fatal error: can't unsafeBitCast between types of different sizes 
let result = source as? [Int]

and:

func toNonNullable<T>(array: [T?]) -> [T]? {
    if array.filter({$0 == nil}).count != 0 {
        return nil;
    }

    return array.map({$0!}) 
}

// This works, but seems likey to be non-idiomatic (as well as being ineffecient). 
let result = toNonNullable(source)
2
  • But the same variable cannot be an [Int] or nil - unless it is an [Int]?. Is that what you want? Commented Apr 28, 2015 at 1:33
  • Yes it is. I'll edit to make that clear. Commented Apr 28, 2015 at 1:37

1 Answer 1

3

Here's how I would write it:

let source = [1, 2, nil] as [Int?]
var result : [Int]? = {
    for i in source {
        if i == nil {
            return nil
        }
    }
    return source.map {$0!}
}()

But that doesn't really meet your "inefficient" consideration. Someone has to look through the array to see if it contains nil, so nothing is lost by looping and doing that; but the inefficiency is that we loop twice, because map is a loop. If you really hate that, here's a way out:

var result : [Int]? = {
    var temp = [Int]()
    for i in source {
        if let i = i {
            temp.append(i)
        } else {
            return nil
        }
    }
    return temp
}()

Lots of very idiomatic Swifties in those formulations, I think!

Sign up to request clarification or add additional context in comments.

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.