2015年12月17日 星期四

一個關於遵循多個Swift 2.0 protocol extension的問題

問題

最近練習用Protocol-Oriented Programming實做可移動以及縮放的View,遇到一個很有趣的問題,假設有以下兩個protocol

protocol Draggable: class {
    var view: UIView { get }
    var initialLocation: CGPoint { get set }
}

extension Draggable where Self: UIView {
    var view: UIView { get { return self } }
    var parentView: UIView? { get { return self.view.superview } }
}
protocol Scalable: class {
    var view: UIView { get }
}

extension Scalable where Self: UIView {
    var view: UIView { get { return self } }
    var parentView: UIView? { get { return self.view.superview } }
}

然後實做一個View遵循這兩個protocol

public class VWView: UIView, Draggable, Scalable {
    var initialLocation: CGPoint = CGPointZero
}

這時候會發生compile error,告訴你
Type 'VWView' does not conform to protocol 'Scalable'
Type 'VWView' does not conform to protocol 'Draggable'

怎麼一回事呢?

原來是因為兩個protocol都有var view: UIView { get { return self } }的default implementations,編譯器不曉得該用哪一個,乾脆來一個編譯錯誤。

解法

其實解決方法很簡單,就是在class裡實做這個值,這樣編譯器就知道該用class裡面的這個了。

public class VWView: UIView, Draggable, Scalable {
    var view: UIView { get { return self } }
    var initialLocation: CGPoint = CGPointZero
}

Reference:
http://stackoverflow.com/questions/31586864/swift-2-0-protocol-extensions-two-protocols-with-the-same-function-signature-c

2015年12月9日 星期三

Parse Cloud Code初體驗

最近在我的App背包客住宿裡需要新增一個上傳住宿資料的功能,由於App是用Parse.com當後端,所以可以選擇用Client SDK直接上傳或是呼叫Cloud Code去管理Data。這次我選擇使用Cloud Code,這樣之後如果要對上傳上來的資料做篩選或是處理都可以立即生效,不用等下一版App上架。

安裝步驟那些就不說了,網路上資料一大把,紀錄幾個自己遇到的問題。

beforeSave

我一開始以為beforeSave會比function早執行,結果卻相反,是先執行function,在儲存前才執行beforeSave。

Geocode

住宿資料上傳前,需要先對Address做Geocode,這個部份可以使用Google Maps API達成,在Geocoding with Google Maps API via Parse Cloud Code這個stackoverflow上的問題有人提供了作法,但是語法跟現在的有點不同,在原答案是用success: error:去分別處理呼叫成功與失敗,現在要用

Parse.Cloud.httpRequest({
  url: 'http://www.example.com/',
  followRedirects: true
}).then(function(httpResponse) {
  console.log(httpResponse.text);
}, function(httpResponse) {
  console.error('Request failed with response code ' + httpResponse.status);
});

then之後接的第一個function處理成功,第二個處理失敗。
一般在Parse Object裡我們會用GeoPoint表示地理位置,那麼從Maps API回來的資料要怎麼轉換為GeoPoint呢?
假設我的Hostel物件有一個欄位叫做location,而它是GeoPoint型別,那麼用下面這樣的轉換方式。

var lat = geocodeResponse.results[0].geometry.location.lat;
var lng = geocodeResponse.results[0].geometry.location.lng;
var point = new Parse.GeoPoint({latitude: lat, longitude: lng});
hostel.set("location", point)

除錯

目前還沒有找到什麼好方法debug,只能在code裡多log,然後在command line用parse log看結果。

console.log(request.params);

2015年12月4日 星期五

Swiftlint - 維護coding style的好工具

https://github.com/realm/SwiftLint

在多人協作開發專案時,遵守團隊訂出的coding style是一個好習慣,可以讓其他協作者更容易看懂我們產出的程式碼。Realm釋出的這個tool就可以幫助我們將專案中不符合coding style的地方用warning甚至是error標出,利用Xcode幫我們自動把關,節省其他協作者幫我們檢查coding style的時間。目前Swiftlint是遵照GitHub’s Swift Style Guide 做檢查。

安裝

brew install swiftlint

設定

lint

在target的run script加入

if which swiftlint >/dev/null; then
  swiftlint
else
  echo "SwiftLint does not exist, download from https://github.com/realm/SwiftLint"
fi

然後在Xcode裡Build專案,哇,怎麼連Pod裡面的code都檢查了呢?

在專案root path新建一個 .swiftlint.yml 檔案,輸入

included:
  - YourProjectSourceDirectiryName

把YourProjectSourceDirectiryName替換成你的專案原始碼資料夾名稱就可以只檢查自己專案的coding style囉。

2015年11月26日 星期四

Swift Optionals

在swift中使用Optional來處理值缺失的狀況,如果看見一個variable是Optional類型,只有兩種可能,要嘛有值,要嘛就是nil。

宣告方式

var coffee:String?

等於下面這種宣告方式

var coffee: Optional<String>

由此可知 ? 其實是syntactic sugar。

看看Optional的原始宣告

public enum Optional<Wrapped> : _Reflectable, NilLiteralConvertible {
    case None
    case Some(Wrapped)
    /// Construct a `nil` instance.
    public init()
    /// Construct a non-`nil` instance that stores `some`.
    public init(_ some: Wrapped)
    /// If `self == nil`, returns `nil`.  Otherwise, returns `f(self!)`.
    @warn_unused_result
    public func map<U>(@noescape f: (Wrapped) throws -> U) rethrows -> U?
    /// Returns `nil` if `self` is nil, `f(self!)` otherwise.
    @warn_unused_result
    public func flatMap<U>(@noescape f: (Wrapped) throws -> U?) rethrows -> U?
    /// Create an instance initialized with `nil`.
    public init(nilLiteral: ())
}

如果沒有賦值的話,Optional的預設值是nil。在Objective-C中,nil是一個0指標,在swift中nil是一個確定的值,任何Optioanl都可以設為nil。關於瞭解空值,可以參考悟空

其他關於Optional chaining以及一些更深度的剖析,可以參考喵神的文章或是玉令天下的Blog

另外有一種情況是如果Optional呼叫的function需要傳入Optional本身,像

class ViewController: UIViewController {

    let finishedMessage = "Network call has finished"
    let messageLabel = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()

        someNetworkCall { [weak self] in
            self?.finished(self?.finishedMessage)
        }
    }

    func finished(message: String) {
        messageLabel.text = message
    }
}

這一段是編譯不過的,因為self?.finishedMessage是Optional chaining,而Optional chaining回傳的一定是Optional值。那麼應該怎麼解呢?

可以直接使用!,因為當self?為nil時不會繼續呼叫function,所以一旦呼叫了function就代表有值。

someNetworkCall { [weak self] in
            self?.finished(self!.finishedMessage)
}

詳情可以參考http://blog.xebia.com/swift-optional-chaining-and-method-argument-evaluation/

2015年11月4日 星期三

隱藏無內容的Self Sizing Cell

基本上看完优化UITableViewCell高度计算的那些事就可以大概瞭解從iOS7以後計算TableViewCell高度需要注意的事。

假設cell裡只有一個addressLabel,而且我們使用iOS8的self sizing,想讓無內容的cell高度變為0,也就是不顯示,可以把top跟bottom constraint的constant設為0。先把xib或storyboard中上下的constraint拉出IBOutlet到 .m 檔中,在cellForRowAtIndexPath的時候根據內容是否為空決定constant大小。

Constraint

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"ListTableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];
    if (cell.addressLabel.text.length == 0) {
        cell.addressLabelTopConstraint.constant = 0;
        cell.addressLabelBottomConstraint.constant = 0;
    }
    else {
        cell.addressLabelTopConstraint.constant = 15;
        cell.addressLabelBottomConstraint.constant = 15;
    }
    [cell layoutIfNeeded];
    return cell;
}

addressLabel如果沒有內容,因為Label約束與上下邊緣一致 (constant為0),所以Label高度會被自動計算為0,且不會佔任何空間。