3

I was looking through the Apple documentation on the UICollectionViewLayout and saw this:

An abstract base class for generating layout information for a collection view.

Source

As you can see, they refer to this as an abstract class, and yet, there are questions like this littered all over SO:

Abstract class in Swift

Abstract classes in Swift Language

In all of these, the answers say that there is no concept of "abstract class" in Objective-C or Swift.

If this is true, why do they continuously refer to it?

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • Technically there is nothing like abstract. So it will not give you errors if you don't override the methods or use it as it is. But you will achieve nothing by doing so. It would be futile to use it as it is. – Mohammad Sadiq Aug 02 '17 at 12:10
  • 2
    I know it is hard for us developers to understand that "abstract" is also a word in the real world and not only for classes :) I think they not referring to abstract, they just using the word like it is normally used. – Retterdesdialogs Aug 02 '17 at 12:14
  • 1
    There _is_ a **concept** of "abstract" in ObjC and Swift, as your research has demonstrated. It's just not the exact meaning that it has in other languages, such as Java (just like the word "primitive" doesn't have _exactly_ the same meaning). What is it that you're looking for that isn't answered by the questions you've linked and others like them? – jscs Aug 02 '17 at 12:49
  • 1
    Additionally, you're asking about a class that's implemented in ObjC, but linking to SO questions about Swift. The two languages have very different semantics in many areas, and Swift's first-class protocols (especially given default implementations), compared to ObjC's, are one of them. – jscs Aug 02 '17 at 12:52
  • @JoshCaswell regarding your comment on Swift vs Obj-C, the documentation uses the same phrasing for both languages. – David Pilkington Aug 02 '17 at 12:55
  • The doc was originally written when there was no Swift, and other than method signatures, they don't seem to be interested in editing Swift-specific stuff in. – jscs Aug 02 '17 at 13:15

1 Answers1

1

True, there are no abstract classes in Swift, the closest representation would be protocols.

But in this particular case abstract class means that you're not supposed to use UICollectionViewLayout class yourself - either subclass it to provide a custom layout and behaviour, or use UICollectionViewFlowLayout (which inherits from UICollectionViewLayout) if you want the default layout.

mag_zbc
  • 6,801
  • 14
  • 40
  • 62
  • While I can understand that, it find it odd that they would allow me to instantiate an object that they dont want me to. This from the same language that removed `++` because they were afraid that people would get confused by `++x` and `x++`? – David Pilkington Aug 02 '17 at 12:20
  • That's because `UICollectionViewLayout` is an Objective-C class and in Objective-C there are no access modifiers. So I suppose that the only option to prevent you from initializing it would be to rewrite entire UIKit to pure Swift. – mag_zbc Aug 02 '17 at 12:25
  • 1
    @DavidPilkington From the documentation: "You must subclass UICollectionViewLayout in order to use it." It's abstract for that reason, if you don't subclass it and provide the necessary implementations for the methods then it doesn't do much. So it's abstract in the sense that it's useless without a specialized implementation. –  Aug 02 '17 at 12:50