Created
February 11, 2016 20:43
-
-
Save naokits/67a786cc053b860ac6e0 to your computer and use it in GitHub Desktop.
NCMBObjectはサブクラス化して使おう! ref: http://qiita.com/naokits/items/db6dfd0eddcf5a38f1dc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func addLocation() { | |
// ...サブクラス化した場合と同じなので省略 | |
let location = NCMBObject(className: "Location") | |
location.setObject(geoPoint, forKey: "geoPoint") | |
location.setObject("新宿駅", forKey: "name") | |
// ...サブクラス化した場合と同じなので省略 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 位置情報をデータストアに追加します | |
func addLocation() { | |
let latitude = Double(35.690921) | |
let longitude = Double(139.700258) | |
let geoPoint = NCMBGeoPoint(latitude: latitude, longitude: longitude) | |
let location = Location.object() as! Location | |
location.geoPoint = geoPoint | |
location.name = "新宿駅" | |
location.saveInBackgroundWithBlock { error in | |
if let e = error { | |
print("保存失敗: \(e)") | |
return | |
} | |
print("保存成功") | |
} | |
} | |
/// ストアデータの位置情報を検索します | |
func searchLocation() { | |
let latitude = Double(35.690921) | |
let longitude = Double(139.700258) | |
let geoPoint = NCMBGeoPoint(latitude: latitude, longitude: longitude) | |
let geoQuery = Location.query() | |
// 現在のSDKでは、検索対象フィールド(プロパティ)を文字列で指定しなければいけないのが残念 | |
geoQuery.whereKey("geoPoint", nearGeoPoint: geoPoint, withinKilometers: Double(2.0)) | |
geoQuery.limit = 20 | |
do { | |
let points = try geoQuery.findObjects() as NSArray | |
for point in points as! [Location] { | |
print(point.name) | |
} | |
} catch { | |
print("失敗: \(error)") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import NCMB | |
@objc(Location) // この宣言をしないと、サブクラスの登録時にクラッシュします | |
public class Location: NCMBObject, NCMBSubclassing { | |
var name: String! { | |
get { | |
return objectForKey("name") as! String | |
} | |
set { | |
setObject(newValue, forKey: "name") | |
} | |
} | |
var geoPoint: NCMBGeoPoint { | |
get { | |
return objectForKey("geoPoint") as! NCMBGeoPoint | |
} | |
set { | |
setObject(newValue, forKey: "geoPoint") | |
} | |
} | |
// MARK: - NCMBSubclassing Protocol | |
/// mobile backend上のクラス名を返す | |
/// - returns: サブクラスのデータストア上でのクラス名 | |
public static func ncmbClassName() -> String! { | |
return "Location" // クラス名と同じ名前であること | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func setupNCMB() { | |
// NCMBSubclassingに準拠しているクラスのセットアップ | |
Location.registerSubclass() | |
// NCMBのセットアップ | |
NCMB.setApplicationKey(applicationKey, clientKey: clientKey) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment