How do I reference Classes in Swift?

房东的猫 提交于 2019-12-13 15:33:15

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/33533989/how-do-i-reference-classes-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!