Nathan McKaskle
Nathan McKaskle

Reputation: 3073

How do I reference Classes in Swift?

As far as iOS languages go I really only know Swift, but I'm trying to learn how to translate from Objective C. I'm doing ok at it (helps that I know C#) and using this as a guide for translation but I'm getting stumped at certain points that aren't in the guide.

For instance, from this tutorial, trying to translate these instructions makes absolutely no sense to me:

Open up the newly added header CERangeSliderKnobLayer.h and replace its contents with the following:

#import <QuartzCore/QuartzCore.h>

@class CERangeSlider;

@interface CERangeSliderKnobLayer : CALayer

@property BOOL highlighted;
@property (weak) CERangeSlider* slider;

@end

What would this look like in Swift?

I know how to import QuartzCore but the rest...

What is @Class in Swift?

What is @interface?

I kind of get what @property is now, I'm guessing for that I just do this:

var highlighted: bool
weak var slider: CERangeSlider

I mean, currently my newly created class looks like this:

import UIKit
import QuartzCore

class CERangeSliderKnobLayer: CALayer {

}

Where would all that go? Inside the class I assume. Especially the property settings.

Upvotes: 2

Views: 2595

Answers (1)

awph
awph

Reputation: 566

First of all, in Swift you don't have interface and implementation. You write the implementation part (class) and the interface in done by the Swift compiler and usually never seen by the developer. The @class are no longer available, because Swift doing the importation job for you. You just need to be sure that the class is reachable (in the project, bridged if from objc or imported if from another framework).

What you want here is this:

import UIKit
import QuartzCore

class CERangeSliderKnobLayer: CALayer {
    var highlighted: Bool
    weak var slider: CERangeSlider
}

This should be in the file CERangeSliderKnobLayer.swift

The getter and setter generated by @property in objc, is done automatically when you declare a var in Swift

Upvotes: 6

Related Questions