iOS Developer Journey. Zmienne w Swift

Posted by Maciej Gos on Monday, November 13, 2017

TOC

W życiu prawie każdego programista pojawia się taka chwila kiedy chce poznać nowy język programowania/technologię. U mnie ten wybór padł na  Swift i ogólnie cały ekosystem związany z iOS.

Przynajmniej części z nas :). Dziś krótki wstęp i porównanie tworzenia zmiennych w Swift 4 i C# 7.

  <td style="background-color: #ffc000; border: 1pt solid #A3A3A3;">
    C#
  </td>
</tr>

<tr>
  <td style="vertical-align: top; width: 3.7402in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var firstname = &#8222;Maciej&#8221;</span>
    </p>
  </td>
  
  <td style="vertical-align: top; width: 4.0548in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">string firstname = &#8222;Maciej&#8221;</span>
    </p>
  </td>
</tr>

<tr>
  <td style="vertical-align: top; width: 3.7402in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var firstname:String = &#8222;Maciej&#8221;</span>
    </p>
  </td>
  
  <td style="vertical-align: top; width: 4.0548in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var firstname = &#8222;Maciej&#8221;</span>
    </p>
  </td>
</tr>

<tr>
  <td style="vertical-align: top; width: 3.7402in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var money = 4500.00</span>
    </p>
  </td>
  
  <td style="vertical-align: top; width: 4.0548in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">double money = 4500.00</span>
    </p>
  </td>
</tr>

<tr>
  <td style="vertical-align: top; width: 3.7402in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var money:Double = 4500.00</span>
    </p>
  </td>
  
  <td style="vertical-align: top; width: 4.0548in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var money = 4500.00</span>
    </p>
  </td>
</tr>

<tr>
  <td style="vertical-align: top; width: 3.7402in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var age = 20</span>
    </p>
  </td>
  
  <td style="vertical-align: top; width: 4.0548in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">int age = 20</span>
    </p>
  </td>
</tr>

<tr>
  <td style="vertical-align: top; width: 3.7402in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var age:Int = 20</span>
    </p>
  </td>
  
  <td style="vertical-align: top; width: 4.0548in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var age = 20</span>
    </p>
  </td>
</tr>

<tr>
  <td style="vertical-align: top; width: 3.7402in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var isDone = true</span>
    </p>
  </td>
  
  <td style="vertical-align: top; width: 4.0548in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">bool isDone = true</span>
    </p>
  </td>
</tr>

<tr>
  <td style="vertical-align: top; width: 3.7402in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var isDone:Bool = true</span>
    </p>
  </td>
  
  <td style="vertical-align: top; width: 4.0548in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;">
    <p lang="pl-PL" style="margin: 0in; font-family: Calibri; font-size: 11.0pt; color: #444444;">
      <span style="background: white;">var isDone = true</span>
    </p>
  </td>
</tr>

Jak widać zmienne w Swift można definiować jako zmienne dynamiczne lub silnie typowane. Osobiście bardziej preferuje podejście silnie typowane gdzie wiadomo od razu jakiego typu będzie zmienna.

Również oba języki mają dużo cech wspólnych. Może nauka nie będzie taka straszna 🙂

Maciej Gos

真诚赞赏,手留余香

使用微信扫描二维码完成支付


comments powered by Disqus