Swift: Can I create an instance of a subclass with the the class properties referencing an instance of the class? - class

In Swift, I am trying to create subclasses of a class which will add properties and methods that are specific to certain instances of the class. I would like to initialize an instance of a subclass with an instance of the class, such that the class properties of the subclass instance actually refer to the class instance from which it initialized. My goal is to be able to set a class property in the subclass instance, and have it also set the same property in the class instance from which it was initialized. I have not been able to find a way to create an initializer which accomplishes this, even if I set all the class properties to be the same for the subclass instance and the class instance. The only way I have been able to do this is to add a variable to the subclass and set it to the class instance, but this is cumbersome. Is there a way to do this?

You need to add an appropriate init like this:
class A {
// ...
}
class B:A {
init(clone:A) {
// assign from the other object what needed
}
}

Related

How to select a specific class from set of classes which are implementing same interface class?

How to select a specific class from set of classes which are implementing same interface class ?
You don't call a method directly from an interface, you call it on a reference that points to an instance of a class. Whichever class that is determines which method gets called.

Swift Complex type in parameter

In order to be a bit more clear I am looking for a solution for the user to pass a class with a specific subclass and protocol, i.e. a class that inherits a viewController and delegate protocol. I know its possible to create a protocol list but cannot find a solution that works correctly. Currently I use a initializer and use the viewcontroller as a parameter and check for delegate inside the function but I would rather if I can have these types in the parameter instead.
EDIT:
Looking to do something similar to this
Protocol prot:Delegate{}
class cla:ViewController{}
class impl{
init(mainUI: cla, prot){
do things
}
}
That way back in the main view controller I can do something like this
class viewController: cla, prot{
var view:impl
override func loadView() {
//Attach to Screen
view = impl(mainUI: self);
}
}
Their is a bit more happening but that's the only part thats really relevant. Currently I use a initializer to just fail if the class doesn't inherit the correct protocols
You could create a dummy type that represents your requirements. A typealias doesn't work in this case, but this might:
class DummyClass: MyViewController, MyDelegateProtocol {}
func needsRequiredClass(input: DummyClass) {...}
With MyViewController and MyDelegateProtocol being the superclass and delegate you mentioned. The DummyClass would have an empty implementation.
Then, make your specific classes sub classes of DummyClass:
class TestClass: DummyClass {...}
And you can pass in that new class:
let test = TestClass()
self.needsRequiredClass(test)
You're asking the wrong question. In other words trying to shoe-horn in a serious design mistake.
A view should not know that its delegate is a UIViewController or a subclass.
A delegate should be any class that obeys (adopts) a specific delegate protocol.
A view should only delegate specific responsibilities. Each of those responsibilities must be described by a protocol method or property.
If you explain what your issue is in more detail (why you think you need direct access to the entire definition of a UIViewController within a UIView), we can help you avoid this mistake.

Calling methods from another class in objective-c

I know usually, when you want to call a method on another object, you do:
NewObject *object = [NewObject alloc]init];
[object callMethod];
But I created a class that isn't an object itself meaning it doesn't have properties or memory management. It has a couple methods that calculate some stuff.
From any other class, all I have to do is import the header for this class and do:
#import "MyClass.h"
[MyClass callMethod];
Why in this case do I not have to alloc init? It works just fine.
It sounds like you are trying to call a class method. These are methods which have been defined as:
+(void) myStaticMethod;
instead of
-(void) myMethod;
The plus sign indicates that the method does not use any fields, and thereby does not need to instantiate the object.
In your example, "object" is an instance of a class "NewObject" which has been allocated memory and initialized. Where-as your example, "MyClass" is only a class which because it has static members declared as above, does not need to be instantiated.
Class methods provide a nice way to combine a bunch of related functions into one place, rather than having them spread out in the regular namespace, as would usually be done in straight C. You can also have both class methods and instance methods in the same class, using the class ones when needed, and instantiating the class to use the instance ones when needed.
EDIT: Changed terminology to refer to class methods instead of static methods.
because you are calling a class method. You only need to alloc init objects. Classes only need to be included but not alloc inited. So you don't need to init an NSString class, say.
Edit:
Let's just have some nonsense examples:
+ (void)classMethod {
NSLog("Hi!");
}
[SomeClass classMethod]; // prints Hi!
- (void)instanceMethod { // (say it's an instance method of NSString)
NSLog(self);
}
[#"someNSString" instanceMethod]; // prints someNSString. But you need to have a string first, otherwise you cannot use this method.
There is a difference between "instance" methods (normal ones), that have to be called on an object and have access to self, and "class" methods (called static, in many programming languages), that are invoked on the class and thus do not have a self.
Class methods are similar to C++ static methods, in that they can be invoked without creating a concrete instance of the class. The usefulness of this is you can call a class's specialized factory methods to create a new instance; or, you can define a utility library under the scope of a class that may or may not provide concrete instances depending on the task.
Look at NSDate and NSNumber are good examples of this in the Foundation framework.

Is singleton class equal to a class with static method

I created a Class A in which all the methods are class methods (+). Another Class B is a singleton.
I want to know if I can check if Class A [A someoperation] Is like class B in that only one instance of A exists and I do not need to instantiate it.
How can I accomplish this?
When you call a class method, the class is not necessarily instantiated, unless the class method actually creates a class.
Also - class methods do not make a class a Singleton. It just means that the method is called on the class instead of an objet of the class.

what is the difference between doing a class as subclass by inheritance and composition

what is the difference between doing a class as subclass by inheritance and composition
Composition : the new class has the original class as an instance variable. The interface of the new class starts from the scratch. Only the properties and methods that the new class defines are available to the users of the class. The new class internally uses the old class object.
Subclass : the new class has all the properties and methods it's superclass defines. Any users can use the properties and methods. If the new class does not override them, the superclass implementation is automatically called. The subclass may add new properties or methods.
Usually subclassing is more helpful, but some cases composition can be helpful ( for example when working with class clusters).
http://www.artima.com/designtechniques/compoinh.html
http://www.mapleshirefarm.com/eric/CompositionVsInheritance.html
http://www.apl.jhu.edu/Notes/LMBrown/resource/Composition.pdf
and concisely...
http://www.codeproject.com/Articles/80045/Composition-VS-Inheritance.aspx