What Is the Swift Syntax " .Bar" Called

What is the Swift syntax .bar called?

It is called an implicit member expression. From the grammar section of the language guide:

An implicit member expression is an abbreviated way to access a member
of a type, such as an enumeration case or a type method, in a context
where type inference can determine the implied type. It has the
following form:

.member name

For example:

var x = MyEnumeration.someValue
x = .anotherValue

Why do I see some variables used with leading dot?

The method is (most likely) declared as

func autoPinEdge(_ from: UIRectEdge, toEdge: UIRectEdge, ofView: UIView)

so the compiler knows that the type of the first two parameters is UIRectEdge.


The full syntax to call the method is

view1.autoPinEdge(UIRectEdge.top, toEdge: UIRectEdge.bottom, ofView: view2)

but as the compiler knows (the documentation says can infer) the type you can pass only the members

view1.autoPinEdge(.top, toEdge: .bottom, ofView: view2)

#pragma mark in Swift?

You can use // MARK:


There has also been discussion that liberal use of class extensions might be a better practice anyway. Since extensions can implement protocols, you can e.g. put all of your table view delegate methods in an extension and group your code at a more semantic level than #pragma mark is capable of.

Swift: Understanding // MARK

The // MARK: and // MARK: - syntax in Swift functions identically to the #pragma mark and #pragma mark - syntax in Objective-C.

When using this syntax (plus // TODO: and // FIXME:), you can get some extra information to show up in the quick jump bar.

Consider these few lines of source code:

// MARK: A mark comment lives here.

func isPrime(_ value: UInt) -> Bool { return true }

Sample Image

And for reference, the quick jump bar is at the top in Xcode:

Sample Image

It exists mostly to help with quick navigation within the file.

Note that the dash (// MARK: -) causes a nice separation line to show up. Consider this MARK comment:

// MARK: - A mark comment lives here.

Sample Image

The darker gray separator line just above the bolded option in that menu comes from the dash.

Additionally, we can achieve this separator line without a comment by simply not having any text after the dash:

// MARK: -

Sample Image

As mentioned, // TODO: and // FIXME: comments will also appear here.

// MARK: - Prime functions

func isPrime(_ value: UInt) -> Bool {
// TODO: Actually implement the logic for this method
return true
}

func nthPrime(_ value: UInt) -> Int {
// FIXME: Returns incorrect values for some arguments
return 2
}

Sample Image

  • FIXMEs get a little band-aid icon that help them standout.
  • MARK icon looks like a table of contents
  • TODO icons look more like a checklist

Clicking on any line in the quick jump bar takes you directly to that line in the source code.

Calling a Swift class factory method with leading dot notation?

This feature is called "Implicit Member Expression"

An implicit member expression is an abbreviated way to access a member of a type, such as an enumeration case or a class method, in a context where type inference can determine the implied type. It has the following form:

.member name


But, as of right now, I advise you should not use this feature in Optional or ImplicitlyUnwrappedOptional context.

Although this works:

// store in Optional variable
let col: UIColor?
col = .redColor()

// pass to function
func f(arg:UIColor?) { println(arg) }
f(.redColor())

This crashes the compiler :(

func f(arg:UIColor?, arg2:Int) { println(arg) }
// ^^^^^^^^^^ just added this.
f(.redColor(), 1)

The compiler has some bugs. see: does swift not allow initialization in function parameters?

Variable type with dot before name?

It's value of enum.

enum CompassPoint {
case north
case south
case east
case west
}

When you have that enum like in example above then you can use that:

let compassPoint = CompassPoint.west
let compassPoint: CompassPoint = .west

When the type is known then you can use the syntax with ..

SWIFT Syntax question with regards to a CLOSURE

As you have correctly identified, this is a closure (put into the variable called columnYPoint, giving it a name):

let columnYPoint = { (graphPoint: Int) -> CGFloat in
let y = CGFloat(graphPoint) / CGFloat(maxValue) * graphHeight
return graphHeight + topBorder - y // Flip the graph
}

So really, it's like a function called columnYPoint:

func columnYPoint(_ graphPoint: Int) -> CGFloat {
let y = CGFloat(graphPoint) / CGFloat(maxValue) * graphHeight
return graphHeight + topBorder - y // Flip the graph
}

Why did the author wrote a closure and put it into a variable, instead of writing a function? I have no idea, because I can't read minds. It's a stylistic choice by the author.

And if you look at how it is being called, this function/closure calculates the Y coordinate of the bar, given the height of the bar, graphPoint. graphPoint is the parameter of the function, so of course it is not used in the rest of the code. As you can see from the caller:

graphPath.move(to: CGPoint(x: columnXPoint(0), y: columnYPoint(graphPoints[0])))
// and
let nextPoint = CGPoint(x: columnXPoint(i), y: columnYPoint(graphPoints[i]))

columnYPoint will be called for each element in graphPoints, so graphPoint will be each value in graphPoints. We need to calculate the coordinates of every bar, after all.

There seems to also be a columnYPoint closure mentioned earlier, which calculates the X coordinate given a given bar index. You can combine these two closures to give you a single closure that gives you a single CGPoint:

let margin = Constants.margin
let graphWidth = width - margin * 2 - 4
let topBorder = Constants.topBorder
let bottomBorder = Constants.bottomBorder
let graphHeight = height - topBorder - bottomBorder
let maxValue = graphPoints.max()!
let columnPoint = { (column: Int, graphPoint: Int) -> CGPoint in
//Calculate the gap between points
let spacing = graphWidth / CGFloat(self.graphPoints.count - 1)
let x = CGFloat(column) * spacing + margin + 2
let y = CGFloat(graphPoint) / CGFloat(maxValue) * graphHeight
return CGPoint(x: x, y: graphHeight + topBorder - y) // Flip the graph
}

?? operator in Swift

It is "nil coalescing operator" (also called "default operator"). a ?? b is value of a (i.e. a!), unless a is nil, in which case it yields b. I.e. if favouriteSnacks[person] is missing, return assign "Candy Bar" in its stead.

Swift default parameter & Selector Syntax

You're getting a crash because it expects a different parameter. 'addTarget' expects to get a function who's sole parameter (if any) is AnyObject (or maybe UIView), not Bool



Related Topics



Leave a reply



Submit