Xcode 플레이그라운드에서 Realm 사용
Atlas Device SDK는 더 이상 사용되지 않습니다. 자세한 내용은 지원 중단 페이지 를 참조하세요.
전제 조건
Swift 패키지는 체계와 대상이 하나 이상 있는 Xcode 프로젝트 내에서만 사용할 수 있습니다. Xcode 플레이그라운드에서 Realm을 사용하려면 먼저 Swift SDK를 설치한 Xcode 프로젝트가 있어야 합니다.
플레이그라운드 만들기
팁
기타 빠른 시작 및 튜토리얼도 참조하세요.
Realm과 Swift 또는 SwiftUI를 시작하는 지침에 대한 빠른 시작 또는 튜토리얼을 참조하세요.
프로젝트 내에서 File > New > Playground 로 Go합니다. 원하는 플레이그라운드 유형을 선택합니다. 이 예제에서는 빈 iOS 플레이그라운드를 사용했습니다.

클릭하여 확대
플레이그라운드의 이름을 지정하고 프로젝트 루트에 저장합니다. 프로젝트에 추가해야 합니다.

클릭하여 확대
프로젝트 탐색기에 새 플레이그라운드가 표시되어야 합니다.

클릭하여 확대
Realm 가져오기
플레이그라운드에서 Realm을 사용하려면 다음 가져오기 문을 추가하세요.
import RealmSwift
Realm으로 실험하기
Realm으로 실험해 보세요. 이 예제에서는 다음을 수행합니다.
class Drink: Object { var name = "" var rating = 0 var source = "" var drinkType = "" } let drink = Drink(value: ["name": "Los Cabellos", "rating": 10, "source": "AeroPress", "drinkType": "Coffee"]) let realm = try! Realm(configuration: config) try! realm.write { realm.add(drink) } let drinks = realm.objects(Drink.self) let coffeeDrinks = drinks.where { $0.drinkType == "Coffee" } print(coffeeDrinks.first?.name)
플레이그라운드에서 Realm 파일 관리하기
플레이그라운드에서 기본 영역으로 작업할 때 영역을 삭제해야 하는 상황이 발생할 수 있습니다. 예를 들어, Realm 객체 유형을 실험하고 객체에 속성을 추가하면 영역을 마이그레이션해야 한다는 오류가 발생할 수 있습니다.
Realm.configuration 세부 정보를 사용하여 특정 경로에서 파일 을 열고 영역이 경로에 있는 경우 영역 을 삭제 합니다.
var config = Realm.Configuration() config.fileURL!.deleteLastPathComponent() config.fileURL!.appendPathComponent("playgroundRealm") config.fileURL!.appendPathExtension("realm") if Realm.fileExists(for: config) { try Realm.deleteFiles(for: config) print("Successfully deleted existing realm at path: \(config.fileURL!)") } else { print("No file currently exists at path") }
또는 Realm 인메모리 영역 열거나 deleteRealmIfMigrationNeeded 메서드를 사용하여 마이그레이션 이 필요할 때 영역 을 자동으로 삭제 수 있습니다.