Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
Tags
- Protocol
- Clean Code
- UICollectionView
- uiscrollview
- swiftUI
- 리팩토링
- Refactoring
- 애니메이션
- Observable
- combine
- Human interface guide
- 클린 코드
- 스위프트
- clean architecture
- ribs
- uitableview
- UITextView
- HIG
- rxswift
- SWIFT
- collectionview
- tableView
- MVVM
- 리펙토링
- swift documentation
- map
- scrollview
- RxCocoa
- ios
- Xcode
Archives
- Today
- Total
김종권의 iOS 앱 개발 알아가기
[iOS - swift] 15. NSCoding 본문
1. 저장될 클래스 타입 정의
class Meal {
var name: String
var photo: UIImage?
var rating: Int
init?(name: String, photo: UIImage?, rating: Int) {
self.name = name
self.photo = photo
self.rating = rating
}
}
2. NSObject, NSCoding 상속 & 구현
func encode(with coder: NSCoder) {
coder.encode(name, forKey: PropertyKeys.name)
coder.encode(photo, forKey: PropertyKeys.photo)
coder.encode(rating, forKey: PropertyKeys.rating)
}
required convenience init?(coder: NSCoder) {
guard let name = coder.decodeObject(forKey: PropertyKeys.name) as? String else {return nil}
let photo = coder.decodeObject(forKey: PropertyKeys.photo) as? UIImage
let rating = coder.decodeInteger(forKey: PropertyKeys.rating)
self.init(name: name, photo: photo, rating: rating)
}
3. NSKeyedArchiver, NSKeyedUnarchiver를 이용하여 디스크 접근
1) 주소 세팅
static let DoucumentDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DoucumentDirectory.appendingPathComponent("meals")
2) 접근
let meals = Meal(name: "as", photo: nil, rating: 3)!
let isSuccessful = NSKeyedArchiver.archiveRootObject(meals, toFile: Meal.ArchiveURL.path)
let load = NSKeyedUnarchiver.unarchiveObject(withFile: Meal.ArchiveURL.path) as? Meal
Comments