UITouch class missing init function

341 views Asked by At

As I've thoroughly enjoyed my novice tutorial today on creating my own classes in Swift that can spawn object instances. I'm hoping to get better by building on this knowledge to start using Apple's Cocoa Touch API's. However while reading through class references such as UIViewController, UIResponder, and UITouch I've noticed that these classes don't have init functions to spawn instances. Am I thinking of this all wrong or is it safe to assume that these classes are meant to be subclassed? And if they are meant to be subclassed then do I need to write my own init function in order to spawn an instance and use it??

A little background, I'm thinking in terms of small legos where I can build whatever I'd like as long as I adhere to Apple's abstract recipes (class references documentation) and placing its desired types such as strings, doubles, integers, etc. in the correct places to make the objects come alive. Am I thinking about this correctly in terms of Apple's objects?

Here's an example of what I did in a tutorial today which has an init function where I can spawn objects of this type and the reason why I'm thinking in terms of legos:

struct Location: Equatable {

    let name: String

    let coordinate: CLLocationCoordinate2D?

    init(name: String, coordinate: CLLocationCoordinate2D? = nil) {
        self.name = name
        self.coordinate = coordinate
    }

}

I read the swift programming guide and have a general understanding. I'd like to start enjoying Cocoa Touch as soon as possible because it looks exciting.

I know I'm a noob and probably to some an idiot who shouldn't touch a keyboard so thank you in advance for any clarification.

1

There are 1 answers

0
rmaddy On BEST ANSWER

UIViewController does have a couple of init methods.

Classes like UITouch don't have any public init methods because you are not supposed to create your own instances.

Classes like UIViewController are meant to be subclassed. It's not common to use instances of UIViewController directly.

And don't forget that in Swift, any subclass gets the init methods of its parent class unless you explicitly add your own init method. Then the subclass only has the init methods you add explicitly.