0

What I want to achieve is to have a "transparent" unifying class that can contain one item of a number of classes that fulfil a number of criteria (one of them is inheritance from UIView). What i mean by transparent is that if i ask this generic class for a property that it does not contain (directly) it checks if the item inside it contains that property automatically. I'm not sure how to describe this any better without an example, so here's what I'm looking to achieve.

Instead of this:

objectOfGenericClass.itemInside.center

I want this:

objectOfGenericClass.center

without absurd amounts of repeating code in the generic class that looks like this:

public var center:CGPoint{
    return itemInside.center
}

If I need to be more specific please ask questions, but I might be unavailable for a couple of hours in a meeting (will remove this bit when I'm back)

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Simen91
  • 73
  • 7
  • I would do this like this `let insideItem = objectOfGenericClass.itemInside` and than just call `insideItem.center` – NSDmitry Jan 11 '17 at 12:47
  • @NSDmitry Yeah, that is a viable option, I was just hoping to avoid all that as this generic class I'm making doesn't really do anything except allowing me to store objects of different classes in a container. I could be making it too complicated of course – Simen91 Jan 11 '17 at 12:54
  • 2
    Key-Value Coding (KVC) can help you achieving the desired solution. For more details on KVC check the link https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/ – dev gr Jan 11 '17 at 12:59
  • 1
    Please clarify what the criteria are. In the case that the shared criterion is "inherits from X", just have a variable of type `X`. – Raphael Jan 11 '17 at 13:05
  • @Raphael yes, the shared criteria is "inherits from X", I should've specified that in my question. I do have a variable of type X that I want to avoid using (if it's possible). But I think your static explanation was what I needed, throw it up as an answer and I think I'll choose it as the accepted one – Simen91 Jan 11 '17 at 13:13
  • Using the variable may even lead to the wrong result if you hit static dispatching. Huh. – Raphael Jan 11 '17 at 13:42

2 Answers2

1

You seem to be used to features of dynamic languages. Welcome to the static world! Here, all signatures have to be known at compile time.

That should answer your question in a fundamental way: no, what you want (auto-dispatching) is not possible.

Dispatching in Swift is not trivial. This seems to be a decent overview.

Raphael
  • 9,779
  • 5
  • 63
  • 94
0

Do I understand your question correctly, when I assume the following is what you want..?

class A // could e.g. be UIView
{
protected:
    int a1;
    int a2;
};

class B : A
{
protected:
    int b1;
    int b2;
};

class C : A
{
protected:
    int c1;
    int c2;
};


class GenericClass : public B, public C
{
    /* ... */
};

.....

GenericClass myObject();

int some_a1 = myObject.a1; // get members of other classes..
int some_a2 = myObject.a2;

The above is ONLY possible if GenericClass is derived (inherits) from both class C and class B, and the class members you ask for are either public or protected (not private). Private members are not available for derived classes. However, one has to be extremely careful regarding the Diamond Problem (https://en.wikipedia.org/wiki/Multiple_inheritance), and this kind of inhertance should (in my personal opinion) be discouraged.

However, the way I understand your question, this does provide a solution.

Without inheritance this is impossible. One cannot trivially retrieve a list of a class' members, see: How to get a list of a class's member variables in C++?

Community
  • 1
  • 1
Wololo
  • 1,249
  • 1
  • 13
  • 25
  • Almost, in your example both class A and class B would inherit from UIView, and I would like to access those UIView variables without having to create a corresponding variable for each of those in the generic class – Simen91 Jan 11 '17 at 13:02
  • My generic class is defined like this: class GenericClass where T:UIView {...} I'm sorry that the formatting is off, I'm on mobile atm – Simen91 Jan 11 '17 at 13:04
  • 2
    @Simen91 If you want to clarify, please edit your question. – Raphael Jan 11 '17 at 13:06
  • @Raphael I'll edit the question when I'm back at a computer – Simen91 Jan 11 '17 at 13:07
  • @Simen91 You say you want to "access the UIView variables, without having to create a corresponding variable". With the above code, you can create your original UIView objects as GenericClass objects instead. That way, they will have the features of all the above classes. If this is not what you wanted, then I've completely misunderstood and I apologize. – Wololo Jan 11 '17 at 13:24