You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pod init
.Podfile에 가시거나 vi 사용해서 설치 ㄱㄱ
pdd 'RealmSwift', '~>10'
pod install
2. RealmModel 만들기
import Foundation
import RealmSwift
//UsedDiary: 테이블 이름//@Persisted: 컬럼1. 저장하고 싶은 데이터가 총 5가지. 필수, 옵션으로 나누어 타입을 설정해줌
classUserDiary:Object{@PersistedvardiaryTitle:String// 제목 (필수)@Persistedvarcontent:String?// 내용 (옵션)@PersistedvarwriteDate=Date()// 날짜 (필수)@PersistedvarregisterDate=Date()// 등록일 (필수)@Persistedvarfavorite:Bool// 즐겨찾기 기능 (필수)2. primaryKey를 설정
// PK 필수: Int, String, UUID, ObjectID -> AutoIncrement@Persisted(primaryKey: true)var_id:ObjectIdconvenienceinit(diaryTitle:String, content:String?, writeDate:Date, registerDate:Date){self.init()self.diaryTitle = diaryTitle
self.content = content
self.writeDate = writeDate
self.registerDate = registerDate
// 초기값을 false로 할당self.favorite = false
}}
3. 데이터를 Read 할 ViewController로 이동
import UIKit
// 1번
import RealmSwift
classWriteViewController:UIViewController{@IBOutletvartitleTextField:UITextField!@IBOutletvarcontentTextView:UITextView!// 2번 : Realm 파일에 접근하는 상수 선언// Open the local-only default realmletlocalRealm=try!Realm()overridefunc viewDidLoad(){
super.viewDidLoad()print("Realm is located at:", localRealm.configuration.fileURL!)}@IBActionfunc dismissHome(_ sender:UIButton){self.dismiss(animated: true, completion:nil)}@IBActionfunc touchSavedButton(_ sender:UIButton){// 3번 : Realm 에 데이터 저장lettask=UserDiary(diaryTitle: titleTextField.text!, content: contentTextView.text!, writeDate:Date(), registerDate:Date())try! localRealm.write{
localRealm.add(task)}print(task)}}
4. 저장한 데이터 Scene에 보여주기
import UIKit
// 1번
import RealmSwift
classSearchViewController:UIViewController{// 2번 : Realm 파일에 접근하는 상수 선언letlocalRealm=try!Realm()// 3번 : Realm에서 읽어온 데이터를 담을 배열 선언vartasks:Results<UserDiary>!@IBOutletvarsearchTableView:UITableView!overridefunc viewDidLoad(){
super.viewDidLoad()}overridefunc viewWillAppear(_ animated:Bool){
super.viewWillAppear(animated)// 4번 : 배열에 Realm의 데이터 초기화
tasks = localRealm.objects(UserDiary.self)print(tasks)}privatefunc assignDelegate(){
searchTableView.delegate =self
searchTableView.dataSource =self}privatefunc registerXib(){letnibName=UINib(nibName:"SearchTableViewCell", bundle:nil)
searchTableView.register(nibName, forCellReuseIdentifier:"SearchTableViewCell")}}extensionSearchViewController:UITableViewDelegate{}extensionSearchViewController:UITableViewDataSource{func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int)->Int{return tasks.count
}func tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath)->UITableViewCell{
guard let cell = searchTableView.dequeueReusableCell(withIdentifier:"SearchTableViewCell")as?SearchTableViewCellelse{returnUITableViewCell()}// 5번 : 테이블뷰에 데이터 표현 letrow=tasks[indexPath.row]
cell.contentLabel.text = row.content
cell.dateLabel.text ="\(row.writeDate)"
cell.titleLabel.text = row.diaryTitle
return cell
}}
MongoDB에 실시간으로 데이터가 생성
The text was updated successfully, but these errors were encountered:
1. Realm 설치 (SPM, Cocopods, Carthage 모두 가능)
2. RealmModel 만들기
3. 데이터를 Read 할 ViewController로 이동
4. 저장한 데이터 Scene에 보여주기
MongoDB에 실시간으로 데이터가 생성
The text was updated successfully, but these errors were encountered: