Each language has its own style of recommendation . obviously OC And Swift There are different styles . When we started to write Swift, The first thing to notice is to follow Swift The style of writing , Instead of using OC Style .

Omit the semicolon at the end of the sentence

swift It's about simple grammar . If there is only one line of code , Don't write a semicolon at the end of the sentence .

Omit self.

Omit when calling its own properties or methods when not in a closure self.. This point has also been discussed in the community , It has been suggested that mandatory statements are needed self., But Apple loudly refused . So make the code a little simpler .

Note the format of the type annotation

When we type a variable or attribute, the syntax is like this :

Notice that there is a space after the colon , If it's a dictionary key There is a space after the colon of .
The colon followed by the type is not standard .

Of course you can use SwiftLint Check for you .

Notice the space behind the function declaration

A standard function syntax is like this :

Notice the function implementation { There's a space in front of it

There are spaces before and after binary operators

Closure 、 Function implementation should not be written in one line

Object initialization, do not explicitly call init

adopt () Call the initialization method of the object directly , No call required init

Function name

oc The first parameter name of is omitted , So the first parameter name is brought to the method name .swift 3 Later, it is adjusted to show the first parameter name when called . So when you name a function, don't put the first parameter related name on the function name .

However, it is more concise to remove prepositions :

Related links :
swift style guide

Swift Review summary : from Swift Style More related articles at the beginning

  1. Swift 3.0 【Swift 3.0 Compare with Swift 2.2 The change of 】

    One . Compiler and syntax changes Function or method arguments When calling a function or method, you must specify a parameter name starting with the first parameter stay Swift In the historical version of, you don't need to specify any function parameters when calling a function ( Or specify the parameter name starting with the second parameter ), When a method is called ...

  2. Swift Developing learning -01 Swift Introduce

    This course is self-taught IOS/Swift Summary of knowledge , Apply to through to Objictive C The use of programming and need to understand based on iOS programmatic iPhone and iPad The programmer . Make a bigger Of ’ Siege lions ‘, Work with dignity , fast ...

  3. Swift On Translation - About Swift

    IMPORTANT Important This is a preliminary document for an API or technology in development. Apple is suppl ...

  4. iOS8 Development ~Swift( 5、 ... and )Swift And OC zlc

    One . Summary First of all to see <The Swift Programming Language> I mentioned "Swift's compatibility with Objective-C lets y ...

  5. Swift Review Conclusion 1 : from Swift Style Start

    Recently, I gathered some enthusiastic friends to write some Swift novice demo( I should be able to meet you in two weeks ), I was involved in review. So I borrowed demo To summarize the new handwriting Swift What to pay attention to , Especially from oc Transfer to swift It's easy to write ...

  6. 3.Swift Translation course series ——Swift Basic knowledge of

    English PDF Download link http://download.csdn.net/detail/tsingheng/7480427 Swift It's for development iOS and OS X The new language of application , But it's used in many places C perhaps OC yes ...

  7. iOS Development Notes (Swift)- in the light of Swift call PPiFlatSegmentedControl Some modifications to the project

    PPiFlatSegmentedControl Project is a very popular open source iOS Control library , Provides a flat style (Flat style) Of SegmentedControl, You can customize segment The color of the , Icon . size ...

  8. 5.Swift Course translation series ——Swift Strings and characters

    English version PDF Download address http://download.csdn.net/detail/tsingheng/7480427 A string is an ordered sequence of characters , example "hello,china" or ...

  9. swift project oc and swift A mixture of , Files refer to each other

    establish swift After the project , If you want to build a new one later oc file , This will generate a  AppName-Bridging-Header.h file One , stay swift In file 1> quote swift file Nothing needs to be done ...

Random recommendation

  1. PHPWord

    PHPWord Chinese garbled I am here   Use PHPWord$section->addText(), Output Chinese is encountering garbled code ,PHPWord The solution of Chinese garbled code is as follows : First step : open phpword/Section.php ...

  2. Phone Gap [error] cmd: Command failed with exit code 1

    Next vote I don't know how to solve this problem , But tried this , It will be settled for sure . This is because ANT Tools can't be found tools.jar stay JRE lib Under the table of contents . When I copy from tools.jar JDK Of lib Under the table of contents , With JRE li ...

  3. java Yang Hui's triangle

    public class study{ public static void main(String args[]){ int i,j,level=7; int Yang[][] = new int[ ...

  4. iOS The event penetration of

    Preface Have you ever met such a demand in the development , A part of a control is obscured by another control , When you click on this overlap , Need to respond to the click event of the masked control , As shown in the figure below   When we click on the area 3 when , Respond to the click event of the blue button , ...

  5. aptana Plug in offline download mode

    aptana Plug in offline download mode Aptana The website was cancelled after revision eclipse The plug-in zip Direct download address , Actually aptana The official website still provides aptana The plug-in zip The package download is just a little hidden . When a lot of people install online ...

  6. JVM Memory area division and garbage collection mechanism detailed explanation

    Before we write Java Code , Most of the time, I don't care about you New Whether the object is released , Or when it's released . because JVM There is an automatic garbage collection mechanism . In previous blogs we talked about Objective-C Medium MRC( Manual reference count ) With ...

  7. ruby--Hash Method summary

    One . to Hash Add default  :h = {1,2,3,4}    #=> {1 => 2, 3 => 4}    h.default = 7   h[1]                 ...

  8. V4L2 To translate documents ( One )【 turn 】

    from :https://blog.csdn.net/airk000/article/details/23218903 Related information https://www.kernel.org/doc/Documentat ...

  9. _itemmod_extra_equipments_enable

    If this table is empty All equipment can be used as double armor   If there are configuration items in this list   Then only the inside and outside   Can be regarded as double a Table description `comment` remarks `entry` goods ID

  10. Maven management jar Package dependency is a common problem that cannot instantiate classes

    you'ji stay maven management jar When a package is dependent , There is a common problem . pom.xml File configuration is OK , adopt eclipse Inside maven dependencies see , And there is this jar package , Or this class . ...