2016年7月18日 星期一

Swift init() 筆記

        第一次接觸到init,看了官方的說明之後還是不太懂實際是做什麼的,但似乎有在接觸OO語言的都十分熟悉。

class NamedShape {
    var numberOfSides: Int = 0
    var name: String
 
    init(name: String) {
        self.name = name
    }
 
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}

init就是初始化的概念,如果變數name在宣告type之後就結束了,並沒有賦值或是為空值,Swift認定name尚未初始化,所以compiler之後會出現錯誤:


class NamedShape {
    var numberOfSides: Int = 0
    var name: String

    //init(name: String) {
    //   self.name = name
    //}

    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}


Class 'NamedShape' has no initializers

要解決的話:

var name: String    ->    var name: String?

or

var name: String    ->    var name: String = "myString"

or

在後面加上




    init(name: String) {
          self.name = name
    }


這樣就初始化完成了。

參考資料:

https://hugolu.gitbooks.io/learn-swift/content/Advanced/Class.html
https://gradyzhuo.gitbooks.io/meetswifttutorial/content/Episode_1/Chapter_1.html
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-ID1



2016年7月16日 星期六

CocoaPods 簡單上手

       1. 簡單介紹原理 


       SWIFT 的特點可以用Swift官方文件的一句話表達:


                                             Swift organizes code into modules.


       在編寫Swift的時候,常常會需要在一支程式裡面import不同的moudule,因時程或其他原因,有時import進來的module會是別人開發的套件,例如需要加入socket的功能,去github下載完開源碼之後,再回到project裡面貼上,再調用裡面的method連上socket,但這樣往往會遇到很多問題。
  1. github上的程式碼如果不watch的話,貢獻者在優化或者是修復bug的時候,本地端不會立即收到消息,就算watch,也會多一件瑣碎的事情要處理。
  2. 就算努力follow最新訊息,還是免不了下載->貼上的步驟,弄不好會造成奇怪的bug
  3. 不優雅XD
       身為swift的開發者,Apple都努力提供了這麼好的開發享受,當然也要相對應有質感的方式去開發(假文青?)

       於是我們需要第三方套件管理來解決這些問題

       Swift Package Manager(SPM)為Apple官方開發,而CocoaPods則是由一群熱心的開發者共同維護,由於SPM還在研發階段,目前並沒有支援iOS、watchOS、tvOS,所以想開發相關os的可能要先去用CocoaPods或其他的套件管理工具。

       稍微提到一點Dependencies的觀念,Apple則是直接獨立出來成一段,這裡引述一下:

Dependencies

A target’s dependencies are modules that are required by code in the package. A dependency consists of a relative or absolute URL to the source of the package and a set of requirements for the version of the package that can be used. The role of the package manager is to reduce coordination costs by automating the process of downloading and building all of the dependencies for a project. This is a recursive process: A dependency can have its own dependencies, each of which can also have dependencies, forming a dependency graph. The package manager downloads and builds everything that is needed to satisfy the entire dependency graph.


        各支code裡面會有要import的不同module,而import近來的module或許也import了其他的module,師範大學的資工系畫了一個簡單的dependency graph:




        把不等式轉變成swift的話,a要import b,而b需要import c,但a可能也需要直接import c的某些方法,甚至c又有自己要import的層層關係,如果全部都要自己手動更新的話,那會是多麼累人的事,至於可能有高耦合這個問題之後研究更深入之後再回頭探討。

        2. 實際應用:

        使用的套件:https://github.com/tidwall/SwiftWebSocket
        目標:在專案裡面成功import這個套件。

        接下來使用CocoaPods實作一次。

        xcode新增一個專案取名為"TestPalce",在project的主目錄新增一個檔案"Podfile",這邊注意,絕對不要用任何副檔名



        之前實測過用mac內建的textedit,但有時引號'或是雙引號"在textedit裡輸入的時候會不一樣,最好的方式就是用terminal的vim或其他的編輯器直接新增,並編輯。

         如果不想注意這麼多細節的話,CocoaPods提供了一個指令:

pod init

        就會自動生成Podfile,並還會貼心地先幫你把基本的內容打好還附上解說。

  • # Uncomment this line to define a global platform for your project
  • # platform :ios, '9.0'

  • target 'TestPlace' do
  •   # Comment this line if you're not using Swift and don't want to use dynamic frameworks
  •   use_frameworks!

  •   # Pods for TestPlace

  • end

        target旁的紅字就是我們Project的主要名稱。

        接著,在SwiftWebSocket的套件裡面有提供CocoaPods的引入方法:


       我們把 pod 'SwiftWebSocket' 加入到Podfile裡,像這樣:

  • # Uncomment this line to define a global platform for your project
  • # platform :ios, '9.0'

  • target 'TestPlace' do
  •   # Comment this line if you're not using Swift and don't want to use dynamic frameworks
  •   use_frameworks!

  •   # Pods for TestPlace
  • pod 'SwiftWebSocket' 
  • end

        接著執行: pod install

        成功的話就會像這樣:


       專案資料夾:



       安裝完成後,Cocoapods請我們關掉原本的專案,改用新生成的TestPlace.xcworkspace開啟,之後開發就統一用這個了。

        開啟之後會發現在原本的專案列表裡面,新增了一個Pods的項目:


        接著在ViweController.swift裡輸入import SwiftWebSocket:


      如果這裡出現了一個錯誤,似乎是還沒有讀到,爬了stackoverflow之後發現解法:

http://stackoverflow.com/questions/36417151/no-such-module-alamofire-xcode-wont-recognize-alamofire-framework

     簡單來說就是,先clean,在build,就OK了!

      


     最後,我們將範例輸入進去確定可行:




     成功!


參考資料: