상세 컨텐츠

본문 제목

[SeSAC] 20220823_TIL (Realm 필터링, 정렬 )

IOS/TIL

by 카키IOS 2022. 8. 23. 09:39

본문

1.필터링

//source by.
//https://www.mongodb.com/docs/realm/sdk/swift/crud/filter-data/

let highPriorityTasks = tasks.where {
    $0.priority > 5
}
print("High-priority tasks: \(highPriorityTasks.count)")
let longRunningTasks = tasks.where {
    $0.progressMinutes >= 120
}
print("Long running tasks: \(longRunningTasks.count)")
let unassignedTasks = tasks.where {
    $0.assignee == nil
}
print("Unassigned tasks: \(unassignedTasks.count)")

//NSPredicate Quary
let predicate = NSPredicate(format: "progressMinutes > 1 AND name == %@", "Ali")

// Use [c] for case-insensitivity.
let startWithE = projects.filter("name BEGINSWITH[c] 'e'")
print("Projects that start with 'e': \(startWithE.count)")

let containIe = projects.filter("name CONTAINS 'ie'")
print("Projects that contain 'ie': \(containIe.count)")

// [d] for diacritic insensitivty: contains 'e', 'E', 'é', etc.
let containElike = projects.filter("name CONTAINS[cd] 'e'")
print("Projects that contain 'e', 'E', 'é', etc.: \(containElike.count)")

 

필터 예시

@objc func filterButton() {
    //스트링 내 문자 필터링
    tasks = localRealm.objects(ShopList.self).filter("title CONTAINS '1'")
        
    //스트링 기반 필터링
    tasks = localRealm.objects(ShopList.self).filter("title = '18_안녕하세요'")
    }

 

2.정렬

@objc func sortButton() {
    tasks = localRealm.objects(ShopList.self).sorted(byKeyPath: "regiDate", ascending: true)

}

 

3.셀 스와이프 액션

//좌 -> 우 스와이프
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        
    let favorite = UIContextualAction(style: .normal, title: "즐겨찾기") { action, view, completionHandler in
        print("favorite Button Clicked")
    }
        
    let example = UIContextualAction(style: .normal, title: "예시") { action, view, completionHandler in
        print("example Button Clicked")
    }
    return UISwipeActionsConfiguration(actions: [favorite, example])
    //UIContextualAction -> 각 아이템
    }
    
//반대
//leadingSwipeActions -> trailingSwipeActions

 

-결과

 

 

4.Update

try! self.localRealm.write {
    //하나의 레코드에서 특정 컬럼 하나만 변경
    self.tasks[indexPath.row].favorite = !self.tasks[indexPath.row].favorite
    //해당 indexPath.row의 favorite만 변경


    //Update1.
    //하나의 테이블에 특정 컬럼 전체 값을 변경
    //self.tasks.setValue(true, forKey: "favorite")
    //모든 favorite 변경

    //Update2.
    //하나의 레코드에서 여러 컴럼들이 변경
    //self.localRealm.create(ShopList.self, value: ["objectID": self.tasks[indexPath.row].objectID, "title": "변경테스트"], update: .modified)
    //2가지 이상 컬럼 변경
}

 

 

5.UIMenu

iOS13+ - UIMenu(최초등장은 iOS14+)

-얼럿 or 액션시트 or UIMenu

 

6.lazy (지연저장 초기화)

-TableView Delegate, DataSource

ex.

lazy var tableView: UITableView = {
    let view = UItableView()
    
    //delegate, datasource 선언은 테이블 뷰 변수 선언과 동시에 사용 불가
    //지연 저장을 통하여 초기화 이후에 실행되도록 명명
    view.delegate = self
    view.datasource = self
    .
    .
    .
}

 

-StackView

ex.

lazy var stackView: UIStackView = {
    let view = UIStackView(arrnagedSubviews: [뷰1, 뷰2, 뷰3, ..., 뷰n])
}
728x90
반응형

관련글 더보기