What Are the Rules for Spaces in Swift

what are the rules for spaces in swift

Answer to the second part of your question can be found here swift docs

The whitespace around an operator is used to determine whether an operator is used as a prefix operator, a postfix operator, or a binary operator. This behavior is summarized in the following rules:

If an operator has whitespace around both sides or around neither side, it is treated as a binary operator. As an example, the + operator in a+b and a + b is treated as a binary operator.

If an operator has whitespace on the left side only, it is treated as a prefix unary operator. As an example, the ++ operator in a ++b is treated as a prefix unary operator.

If an operator has whitespace on the right side only, it is treated as a postfix unary operator. As an example, the ++ operator in a++ b is treated as a postfix unary operator.

If an operator has no whitespace on the left but is followed immediately by a dot (.), it is treated as a postfix unary operator. As an example, the ++ operator in a++.b is treated as a postfix unary operator (a++ .b rather than a ++ .b).

etc... (read the docs for more on this)

As for the first part of your question, I didn't see any issue with either way of declaring the variables.

var j: Int = 34
var j:Int=23

The only issue with that provided code is that you declare j twice in the same scope. Try changing one of the j's to an x or y or something else.

If you were wondering about

var j:Int =10

or

var j:Int= 10

look at the rules above. = is an operator so if you were to do either of those, it would be treated as prefix or postfix, and you would get the error that prefix/postfix = is reserved

These rules are important due to the existence of operators such as the unary plus and unary minus operators. The compiler needs to be able to distinguish between a binary plus and a unary plus operator. List of operators

Divided operation in Swift

Swift has strict rules about the whitespace around operators. Divide '/' is a binary operator.

The important rules are:

  • If an operator has whitespace around both sides or around neither
    side, it is treated as a binary operator. As an example, the +
    operator in a+b and a + b is treated as a binary operator.
  • If an operator has whitespace on the left side only, it is treated as a
    prefix unary operator. As an example, the ++ operator in a ++b is
    treated as a prefix unary operator.
  • If an operator has whitespace on
    the right side only, it is treated as a postfix unary operator. As an
    example, the ++ operator in a++ b is treated as a postfix unary
    operator.

That means that you need to add a space before the / or remove the space after it to indicate that it is a binary operator:

var rotation = Double(arc4random_uniform(50)) / (100.0 - 0.2)

If you want rotation to be a Float, you should use that instead of Double:

var rotation = Float(arc4random_uniform(50)) / (100.0 - 0.2)

There is no need to specify the type explicitly since it will be inferred from the value you are assigning to. Also, you do not need to explicitly construct your literals as a specific type as those will conform to the type you are using them with.

Xcode indentation settings for Swift vs. Objective C

To answer your question of:

Is there any way in Xcode 8+ to configure indentation on a per file type basis?

Yes, there is. On the File Inspector of each file, you can set the Indent type to Spaces or Tab and also the number of spaces.

I have attached the screenshot here:
Sample Image

You can select multiple file and update the settings at the same time. The settings here will overwrite what you have set in the Preferences > Text Editing > Indentation.

SwiftLint Custom Spacing Rule After slass/struct/enum/extension

Figured it out thanks to this comment to an issue on the swiftlint repo. Posting my answer here in case other folks go searching for a similar solution and don't want to spend hours on it like I did:

(class|struct|enum|extension)((?-s)\s.*\{$\n)(?!^\s*$)

Are there other ways to solve this, almost certainly, but I will let people smarter than me post about why I'm a dummy for not figuring those out. ;)

How to control the line spacing in UILabel

I thought about adding something new to this answer, so I don't feel as bad... Here is a Swift answer:

import Cocoa

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 40

let attrString = NSMutableAttributedString(string: "Swift Answer")
attrString.addAttribute(.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attrString.length))

var tableViewCell = NSTableCellView()
tableViewCell.textField.attributedStringValue = attrString

"Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels."

See: Set UILabel line spacing


This is a really old answer, and other have already addded the new and better way to handle this.. Please see the up to date answers provided below.

Firestore security rules with spaces in path

According to fire base support:

To fix this, you can encode the space within security rules using %20. So the rules would be:

Service cloud.firestore { 

match /databases/{database}/documents { 

match /companies/{company} { 
allow read: if getUserCompany() == company || userHasAnyRole(['Super', 'Manager']); 
allow write: if getUserCompany() == company || userHasAnyRole(['Super', 'Manager']); 

match /Test%20Cases/{tests} {                      <------- 
allow read, write: if isSignedIn(); 



I tried it and worked for me. Please give it a try and let us know if you have any issues. 

How to compare Character for empty space

Use NSCharacterSet to check for white space, including \n, \t, etc.

NSCharacterSet.whitespaces.contains(val.unicodeScalars.first!)


Related Topics



Leave a reply



Submit