Swift1.2(SugarRecord)를 사용하여 CoreData 작업을 간편하게 수행
17262 단어 SugarRecordCoreDataXcode6SwiftXcode
SugarRecord는 CoreData의 래치이자 Realm의 래치입니다.
CocoaPods
CocoaPods 설치
슈가레코드는 코코팟을 사용하기 때문에 코코아팟을 설치한다.스위프트도 쓸 수 있는 상태가 돼서 회상하면서...$ gem install cocoapods
$ pod --version
0.36.4
Xcode로 프로젝트 만들기
Xcode로 프로젝트를 만듭니다.먼저 "Testapp CocoapPod01"이라고 부릅니다.
"Use Cocoa Data"에서 선택$ cd
$ pwd
/Users/user_name/TestAppCocoaPods01
$ pod init
Podfile 준비
$ pwd
/Users/user_name/TestAppCocoaPods01
$ vi Podfile
Podfile 편집
슈가레코드 외에 이번에도 설치했어요.
Podfiletarget '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
SugarRecord 설치
$ pod install
SugarRecord
CoreData 설정
$ gem install cocoapods
$ pod --version
0.36.4
$ cd
$ pwd
/Users/user_name/TestAppCocoaPods01
$ pod init
$ pwd
/Users/user_name/TestAppCocoaPods01
$ vi Podfile
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
$ pod install
CoreData 설정
AppDelegate 준비
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
}
}
SugarRecord를 통해 획득한 객체
Categories.swift
import Foundation
import CoreData
class Categories: NSManagedObject {
@NSManaged var name: String
func name_plus() -> NSString{
var neme_plus = self.name + "だよ"
return neme_plus
}
}
전체 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()
}
}
쓰기(저장)
저장 가능(Active Record) 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()
}
}
편집자
거래가 필요해서요.
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()
}
}
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()
}
}
if let category: Categories = Categories.by("name", equalTo: "foo").find().firstObject() as? Categories {
category.beginWriting()
category.name = "fooはフーになります"
category.endWriting()
category.save()
}
넣다
for 회전으로 획득
func readData() {
var items = Categories.all().find()
for(var i = 0; i < items.count; i++) {
var categories = items[i] as! Categories
}
}
컨디션
Reference
이 문제에 관하여(Swift1.2(SugarRecord)를 사용하여 CoreData 작업을 간편하게 수행), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/matsuhisa_h/items/499284a38733799117fa텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)