2

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.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
Nathan McKaskle
  • 2,926
  • 12
  • 55
  • 93
  • @class is now just "class" as in; class myClass { } You no longer need a separate interface definition. Just define you class and methods in one file. – john elemans Nov 04 '15 at 23:36

1 Answers1

6

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

awph
  • 566
  • 6
  • 15
  • So my whole project is all in Swift 2.0, this tutorial is in objective C. That part of the tutorial where it tells me to reference the class for CERangeSlider, I don't need to do that? I just need to put exactly what you put above and that's all? He says the whole point is to store the logic of which knob is being dragged in the layer.(Adding interactive logic section.) – Nathan McKaskle Nov 05 '15 at 03:04
  • I don't follow the whole tutorial but I would say, yes! – awph Nov 05 '15 at 05:25
  • What you really need to notice is that in Objc you have the headers (\*.h) that hold the name of visible functions and properties (public). And the implementation files (\*.m) that hold the implementation of the public functions/properties, but also you can add new functions/properties that you don't want to expose (private). In Swift you only have one type of file (\*.swift) that contains your implementation part. – awph Nov 05 '15 at 05:38
  • And to declare a public or private function / property in swift you need to set the appropriate modifier for each func/var/let. Take a look at this https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html and this https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html – awph Nov 05 '15 at 05:39
  • In the code above it looks like i'll have to initialize them with = bool() instead of : Bool or make them optionals. – Nathan McKaskle Nov 05 '15 at 19:37
  • 1
    Yes, or `var highlighted: Bool!` with the exclamation point to to said to the swift compiler that the variable will have a value. Or the best way would be to use `var highlighted: Bool = false` or `true` to know explicitly the initial value. (FYI `Bool()` === false) – awph Nov 05 '15 at 19:44