AnyObject arrays

35 views
Skip to first unread message

Alex Hall

unread,
Oct 13, 2015, 9:30:49 AM10/13/15
to swift-l...@googlegroups.com
Hello list,
The below code:

var a: [AnyObject] = []
let b: [AnyObject] = [2, "b", 17, 0.6]
a.appendContentsOf(b)

gives me the error:
error: value of type 'Int' does not conform to expected element type 'AnyObject'
let b: [AnyObject] = [2, "b", 17, 0.6]
                      ^

In my project, I have an array that will hold three or four object types, each type a subclass of a single parent. The only way I saw of doing this was with an array of AnyObject, but Xcode told me that I couldn't append arrays of my different objects to my AnyObject array. I therefore made the above playground to see what would happen, and while this error isn't the same as what I was seeing, it still suggests I've badly misunderstood AnyObject arrays. I suppose it could be a bug, but a mistake on my part is much more probable. :) Thanks for any light anyone can shed on this. Oh, Xcode 7.1 if it matters.

--
Have a great day,
Alex Hall
meh...@icloud.com

Jeremy Pereira

unread,
Oct 13, 2015, 10:39:31 AM10/13/15
to Alex Hall, swift-l...@googlegroups.com
What version of Xcode? For me (7.0.1 OS X playground) it runs OK.
> --
> You received this message because you are subscribed to the Google Groups "Swift Language" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
> To post to this group, send email to swift-l...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/5A5EA2A7-6E68-488F-BECA-6701B1053A3C%40icloud.com.
> For more options, visit https://groups.google.com/d/optout.

Alex Hall

unread,
Oct 13, 2015, 10:45:15 AM10/13/15
to Jeremy Pereira, swift-l...@googlegroups.com

> On Oct 13, 2015, at 10:38, Jeremy Pereira <jeremy.j...@googlemail.com> wrote:
>
> What version of Xcode? For me (7.0.1 OS X playground) it runs OK.

I'm on Xcode 7.1, the current beta.

Garth Snyder

unread,
Oct 13, 2015, 10:47:27 AM10/13/15
to Alex Hall, swift-l...@googlegroups.com
Ints are indeed not objects; you need [Any] for this.

Note that your error is on the “let b…” line, not on the append line.

Hope this helps,
Garth


Jeremy Pereira

unread,
Oct 13, 2015, 10:47:55 AM10/13/15
to Alex Hall, swift-l...@googlegroups.com

> On 13 Oct 2015, at 15:44, Alex Hall <meh...@icloud.com> wrote:
>
>
>> On Oct 13, 2015, at 10:38, Jeremy Pereira <jeremy.j...@googlemail.com> wrote:
>>
>> What version of Xcode? For me (7.0.1 OS X playground) it runs OK.
>
> I'm on Xcode 7.1, the current beta.

I’d say raise a bug with Apple, but I was quite surprised when it ran under my Xcode, I was expecting the same error because Int is a struct and therefore not a subclass of AnyObject. I suspect, on 7.0.1, it is being silently converted into an NSInteger.

Alex Hall

unread,
Oct 13, 2015, 10:59:18 AM10/13/15
to Garth Snyder, swift-l...@googlegroups.com
On Oct 13, 2015, at 10:47, Garth Snyder <ga...@garthsnyder.com> wrote:

Ints are indeed not objects; you need [Any] for this.

Thanks, I figured it was something simple I was missing. I've never heard of Any as a type, only AnyObject, but you're right, switching it to Any made things work as expected. I even made a simple class and stored an instance in the array, and that worked. Well, this makes my app's problem all the more perplexing, because nothing there is simply an Int. I'll look more, then make a separate post if I can't work it out. Thanks guys.

Bruno Berisso

unread,
Oct 13, 2015, 1:36:10 PM10/13/15
to Swift Language, ga...@garthsnyder.com, meh...@icloud.com
What you are missing is the magic on Foundation. Ugly things happen there :(

This is a synthesis of your case in the Swift REPL:

Welcome to Apple Swift version 2.0 (700.0.59 700.0.72). Type :help for assistance.
 
1> let a: AnyObject = 1
repl
.swift:1:20: error: value of type 'Int' does not conform to specified type 'AnyObject'
let a
: AnyObject = 1
                   
^
 
1> import Foundation
 
2> let a: AnyObject = 1
a
: __NSCFNumber = Int64(1)
 
3>

As you see there. Importing Foundation do things that translate Int structs to NSNumber (not NSInteger because those are typedefs) which make invalid Swift statements become valid.

In practice this is annoying but at the end NSNumber have value semantics (you can't mutate it) so it's not that bad. I think that it's the price to pay for use Swift with the Objective-C runtime.

Fritz Anderson

unread,
Oct 13, 2015, 3:16:03 PM10/13/15
to Jeremy Pereira, Alex Hall, swift-l...@googlegroups.com
If you're right about Int being silently bridged to NSNumber, I'd ask if your playground imported Foundation (UIKit, Cocoa) and the OP’s didn't.

— F
> To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/3A2DBEFA-D3C3-474D-AAC6-DB89440D55C6%40googlemail.com.

Jeremy Pereira

unread,
Oct 13, 2015, 7:07:46 PM10/13/15
to Fritz Anderson, Alex Hall, swift-l...@googlegroups.com

> On 13 Oct 2015, at 20:15, Fritz Anderson <fri...@manoverboard.org> wrote:
>
> If you're right about Int being silently bridged to NSNumber, I'd ask if your playground imported Foundation (UIKit, Cocoa) and the OP’s didn't.

It could be. I was importing Foundation and I do get an error if I remove it. The error is less helpful than with 7.1, it’s

error: type of expression is ambiguous without more context
let b: [AnyObject] = [2, "b", 17, 0.6]

and the caret points at the 2 as you would expect.
Reply all
Reply to author
Forward
0 new messages