This question already has answers here:
Why is there no universal base class in Swift?
(2 answers)
Closed 5 years ago.
The Apple's developer documentation says:
NSObject is
The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system.
I wonder what is the root class of all classes in Swift in the example below:
class MyClass {
....
}
Swift classes do not inherit from a universal base class. Classes you
define without specifying a superclass automatically become base
classes for you to build upon.
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html
Is that what you were looking for? :)
Related
This question already has answers here:
Difference between static function and singleton class in swift [closed]
(3 answers)
Closed 6 years ago.
I have made a class that will contain all the utility methods. So, instead of making it as a singleton, I have marked the methods as static and accessing those methods by the ClassName.methodName without the need for instantiation.
Is this approach OK?
Just consider that a singleton is used in order to ensure that only one instance exists for a given class, and that there’s a global access point to that instance.
I believe that having all utility functions marked as static within a class is a good approach since, as you have stated, you will need to use ClassName.methodName in order to use them.
In addition, based on what you want to achieve and the information provided by this link, I would reassert that having a class with static methods is the best alternative.
This question already has answers here:
In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?
(5 answers)
Closed 6 years ago.
Given there are 2 Protocols P1 and P2, it is possible to specify a type that conforms to both protocols, eg:
typealias P = protocol<P1, P2>
Is there a similar way to specify a type that is kind of a class and also conforms to a protocol, e.g. something like this (which does not work):
typealias P = UIView: P1
Unfortunately it's imposible in Swift 2.2 and won't be added in Swift 3.0.
The idea that you want to create Type rule for types that inherit some class and implement protocol.
So it's not very common that you will have such hierarchy and will have property where you want to store one of this classes.
Also in POP paradigm you should have additional protocol that give you properties that you need from UIView.
Maybe you have such usecase:
Then create additional class:
And your type will be P1Base
P.S. That's why if you create #property (nonatomic) UIViewController<UITableViewDelegate> *protocolClassProperty; in Obj-c, it will be bridged as UIViewController! in Swift
This question already has answers here:
What is the rationale behind having companion objects in Scala?
(7 answers)
Closed 7 years ago.
As we know we don't have static methods in scala. If we have to achieve that taste we declare that class as object. But the problem is that when we declare class as object then all methods present in that object becomes static. And in class all methods all non-static. What I want is that to have static as well as non static methods in the same class, Is it possible? Definitely it would be possible but how??????
To do what you are trying do in Scala you create an object and a class of same name. Put static in object and instance members in Class version of it. This object is called a companion object.
This question already has answers here:
Is there more to an interface than having the correct methods
(17 answers)
Closed 8 years ago.
why interface is introduced in java? The abstract class only do the job of interface then, what is use of interface ?
Abstract class can have common methods and abstract methods so that if class need to implement methods which interface forced to imppliments ,instead class can extend abstract class which also forced to implement methods like interface and child class can get inherits methods of abstract class which interface dosent have this facilty .
Advantage: by using interface concept we can achieve multiple inheritance means one class can implement more than one interfaces.
Which of the following is NOT a root class in the context of Objective-C?
a. NSObject
b. NSNetworkService
c. NSBase
d. NSProxy
(its a single select question)
I get this question in a online exam for iOS 6. I knowNSObject and NSProxy are root classes. But I am not aware of any class with name NSNetworkService or NSBase.
I am wondering what is the correct answer for this question.
Note: I have cleared that test and not posting this question for cheating.
I'm not aware of the classes NSNetworkService and NSBase (of course someone could name his class with prefix NS). NSObject and NSProxy are base classes, though. So the correct answer is b and c.
Edit (thanks Abizern): In the context of iOS, only NSObject is available which would add answer d to the set of classes NOT being a root class. I guess the opposite question "Which of the following classes are root classes?" would have been easier to answer given the fact that you said you can choose only one option ;)
see in the following link
1) NSObject is the root class of most Objective-C class hierarchies; it has no superclass.
2) Cocoa supplies two root classes: NSObject and NSProxy. Cocoa defines the latter class, an abstract superclass, for objects that act as stand-ins for other objects; thus NSProxy is essential in the distributed objects architecture. Because of this specialized role, NSProxy appears infrequently in Cocoa programs. When Cocoa developers refer to a root or base class, they almost always mean NSObject.
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW35
Edit
see this link http://samplecodebank.blogspot.in/2013/06/NSNetService-getInputStream-example.html
but there is no any discription of NSBase on Google or SO
So the answer is c. NSBase