Last active
August 29, 2015 14:20
-
-
Save matsuhisa/51f378ba84d031144d6c to your computer and use it in GitHub Desktop.
SugarRecord(Swift 1.2)を使ってCoreDataの操作を楽にしてみる ref: http://qiita.com/matsuhisa@github/items/499284a38733799117fa
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 UIKit | |
import CoreData | |
import SugarRecord | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
let stack: DefaultCDStack = DefaultCDStack(databaseName: "TestAppCocoaPods01.sqlite", automigrating: true) | |
SugarRecord.addStack(stack) | |
//ログレベルを設定(任意らしい) | |
SugarRecordLogger.currentLevel = .logLevelError | |
return true | |
} | |
} |
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 Foundation | |
import CoreData | |
class Categories: NSManagedObject { | |
@NSManaged var name: String | |
func name_plus() -> NSString{ | |
var neme_plus = self.name + "だよ" | |
return neme_plus | |
} | |
} |
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
$ gem install cocoapods | |
$ pod --version | |
0.36.4 |
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
$ cd | |
$ pwd | |
/Users/user_name/TestAppCocoaPods01 | |
$ pod init |
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 readData() { | |
var items = Categories.all().find() | |
for(var i = 0; i < items.count; i++) { | |
var categories = items[i] as! Categories | |
} | |
} |
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
$ pwd | |
/Users/user_name/TestAppCocoaPods01 | |
$ vi Podfile |
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
$ pod install |
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 UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
initDataInsert() | |
readData() | |
if let category: Categories = Categories.by("name", equalTo: "foo").find().firstObject() as? Categories { | |
println("edit ------------") | |
category.beginWriting() | |
category.name = "fooはフーになります" | |
category.endWriting() | |
category.save() | |
println("------------") | |
} | |
readData() | |
} | |
// データーの取得 | |
func readData() { | |
var items = Categories.all().find() | |
println("readData ------------") | |
println(items.count) | |
for(var i = 0; i < items.count; i++) { | |
var categories = items[i] as! Categories | |
println(categories.name_plus()) | |
} | |
println("------------") | |
} | |
// データーの登録 | |
func initDataInsert() { | |
let category_names: [String] = ["日記", "写真", "技術", "映画", "foo", "bar"] | |
for category_name in category_names { | |
println("------------") | |
println(category_name) | |
var categories:Categories = Categories.create() as! Categories | |
categories.name = category_name | |
var saved:Bool = categories.save() | |
println(saved) | |
println("------------") | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
} | |
} |
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 initDataInsert() { | |
let category_names: [String] = ["日記", "写真", "技術", "映画", "foo", "bar"] | |
for category_name in category_names { | |
var categories:Categories = Categories.create() as! Categories | |
categories.name = category_name | |
var saved:Bool = categories.save() | |
} | |
} |
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
if let category: Categories = Categories.by("name", equalTo: "foo").find().firstObject() as? Categories { | |
category.beginWriting() | |
category.name = "fooはフーになります" | |
category.endWriting() | |
category.save() | |
} |
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
target 'TestAppCocoaPods01' do | |
use_frameworks! | |
pod 'Alamofire', :git => "https://github.com/Alamofire/Alamofire.git" | |
pod 'SwiftyJSON', :git => "https://github.com/SwiftyJSON/SwiftyJSON.git" | |
pod 'SugarRecord/CoreData', :git => 'https://github.com/SugarRecord/SugarRecord.git' | |
end | |
target 'TestAppCocoaPods01' do | |
use_frameworks! | |
pod 'Alamofire', :git => "https://github.com/Alamofire/Alamofire.git" | |
pod 'SwiftyJSON', :git => "https://github.com/SwiftyJSON/SwiftyJSON.git" | |
pod 'SugarRecord/CoreData', :git => 'https://github.com/SugarRecord/SugarRecord.git' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment