Giter VIP home page Giter VIP logo

swiftcheatsheet's Issues

Several small suggestions for improvement of an already good document

When reading through the sheet for the first time, one recognizes some minor issues that, if fixed, would increase the value of the sheet without adding substantial weight:

1. String concatenation

Swift has a few basic math operators:

a + b for addition (works for strings too)

The little "works for strings too" is easily overlooked. String concatenation is so fundamental that is should be included into the String section examples.

2. Lazy computed property

lazy var articles:String = {
      return Database.getArticles()
   }()

The trailing parentheses are unintuitive. What are they for? When and how often is the evaluation being performed?

3. Underscore

func startWorking(_ time:String, withWorkers workers:Int)

The underscore is not found in other languages. What is it used for and why?

4. Enums in switch

let weather = ...

switch weather 
{
    case .rain:
        print("Bring a raincoat!")

The leading dot in ".rain" seems to indicate some kind of scope. What does it mean?

5. Loop basics
The Loops-Section should at least mention continue and break and repeat ... while.

6. Closure syntax

let authenticate = { (name: String, userLevel: Int) -> Bool in
   return (name == "Bob" || name == "Alice") && userLevel > 3
}

The syntax of the Closure should be explained with some additional words since it differs from what other languages do.

7. Enum syntax with values

enum Item {
   case weapon(Int, Int)
   case food(Int)
   case armor(Int, Int, Double)
}

This example for an enum does not make sense without further explanation.

8. Comments
The whole section looks like a discussion of personal taste and style, not like a discussion of the language. If there are some strict recommendations from the language authors, this should be pointed out. Otherwise this section should be shortened.

9. Final remarks
I've been looking for a while for a simple document highlighting the essentials for getting started with Swift for people who have long experience with multiple languages. Your cheat sheet is exactly that and I have not yet found anything better. Thanks for the good work.

Protocols - idea

Hello Reinder!

I've prepared a basics for protocol subsection but I'm not sure it's enough. I'm trying to keep it brief but I don't want to miss on crucial information. Could you check the idea and tell me if it's enough or should I add something else?

Protocols

Protocols define a "contract" - they define a set of methods and properties that a type must implement if it conforms to the protocol.

Protocols are declared like this:

protocol Jedi {
        var name: String { get set }
        var rank: String { get set }
        func useTheForce()
    }

Example of struct that conform to the Jedi protocol:

    protocol Jedi {
        var name: String { get set }
        var rank: String { get set }
        func useTheForce()
    }

    struct Master: Jedi {
        var name = "Obi-Wan Kenobi"
        var rank = "Jedi Master"
        
        func useTheForce() {
            print("These aren't the droids you're looking for.")
        }
    }

Commenting

// This is a single line comment.

/* This is a multi-
-line comment */

Mention the use of braces in Conditionals

Would be beneficial to state that conditionals can be made more readable by adding braces around each expression, instead of simply saying that they can become confusing if we have multiple expressions (this is currently the case)

Structs - idea

Structs

Structures, or structs in Swift are a value data type(meaning its value is copied when assigned or passed) that allows to encapsulate related properties and behaviors and later reuse it in our code. We declare them like this:

struct Jedi {
    var name: String
    var age: Int
    var midi_chlorianCountOver9000: Bool
}

With defined struct we can create the instance of it(Xcode will show our Jedi struct in the auto-completion menu).

var obi_wan = Jedi(name: "Obi-Wan Kenobi", age: 57, midi_chlorianCountOver9000: true)
var tionne = Jedi(name: "Tionne Solusar", age: 21, midi_chlorianCountOver9000: false)

To read a property do this:

print(obi_wan.midi_chlorianCountOver9000)
print(tionne.age)

Functions inside structs

We can also include functions inside our structs like this:

struct Jedi {
    var name: String
    var age: Int
    var midi_chlorianCountOver9000: Bool
    
    // Function inside struct
    func mindTrick() {
        print("These aren't the Droids you're looking for...")
    }
}

// Instance of a struct
var obi_wan = Jedi(name: "Obi-Wan Kenobi", age: 57, midi_chlorianCountOver9000: true)
var tionne = Jedi(name: "Tionne Solusar", age: 21, midi_chlorianCountOver9000: false)

// Reading a property
print(obi_wan.midi_chlorianCountOver9000)
print(tionne.age)

// Calling mindTrick() function
obi_wan.mindTrick()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.