0

Xcode is giving the following compiler error.

enter image description here

I assume it is complaining about using the NSLayoutAttribute enum. If it is complaining about this why isn't it complaining about the NSLayoutRelation enum. How do I write this method in a way that is compatible with Objective-C.

JAL
  • 41,701
  • 23
  • 172
  • 300
villy393
  • 2,985
  • 20
  • 28

2 Answers2

1

I think it's because Objective C objects cannot have arrays of things that are not NSObjects.

Edit for future searchers: Erasing the type using AnyObject will make the warning disappear, however you will then need to be careful with casting.

Yoav Schwartz
  • 2,017
  • 2
  • 23
  • 43
  • I think you are right. When I replaced the `NSLayoutAttributes` with `AnyObjects` it compiled fine. Thanks. – villy393 Mar 07 '16 at 14:35
  • Accept the answer? so that other people who have the same error get to this page and see that this solved the problem. It's a pretty common error. – Yoav Schwartz Mar 07 '16 at 14:37
  • I can't accept the answer within 10 mins. It must be some Stack overflow constraint. Will accept the answer as soon as I can. – villy393 Mar 07 '16 at 14:38
0

The reason you get this error (even in a class that inherits from NSObject) is because some Swift types cannot be represented in Objective-C, such as an array of enums.

[[NSLayoutAttribute]] will not work but [[Int]] will because while Array<SomeEnumType> cannot be bridged, an array of Ints are implicitly bridged to an NSArray of NSNumber objects through Foundation.

Yoav Schwartz's answer is wrong because Objective-C objects can have arrays of things that are not NSObjects. Look at C arrays: NSUInteger someNums[5] = {1, 2, 3, 4, 5}; is valid in Objective-C. NSArrays however, cannot hold value types and must hold objects.

Community
  • 1
  • 1
JAL
  • 41,701
  • 23
  • 172
  • 300
  • Thats strange because I changed the `[[NSLayoutAttribute]]` to `[[Int]]` and it is compiling fine :\ – villy393 Mar 07 '16 at 15:00
  • @villy393 from your screenshot it looked like you had an optional 2D array of `NSLayoutAttribute`s: `[[NSLayoutAttribute]]?` Removing the optional probably solved that issue. Also, please post your full code rather than a screenshot in your question. – JAL Mar 07 '16 at 15:05
  • I changed it to `horizontalAttributes: [[Int]]? = nil,` and `horizontalRelations: [[Int]]? = nil,` which is still and optional 2D array and it is working fine. I suspected what you suggested as well but it turns out not to be right. – villy393 Mar 08 '16 at 11:48
  • @villy393 Ah, actually the problem is not because of the optionals, it's because you're trying to bridge an array of enums to Objective-C, which is not supported. `NSLayoutAttribute` is just an `Int`, and `Int` arrays can be bridged to Objective-C as an `NSArray` of `NSNumber` objects. That's the real issue here. I'll edit my answer. – JAL Mar 08 '16 at 18:24
  • @villy393 Hope my answer is clearer now, thanks for following up and correcting me! – JAL Mar 08 '16 at 18:31