Skip to content

Commit

Permalink
UserDefaults deletes an object if nil is saved (#10)
Browse files Browse the repository at this point in the history
* fixed
* podpec and changelog are updated
* style improvements in changelog
  • Loading branch information
Artem Stepanenko authored Feb 27, 2017
1 parent 02f35d4 commit 012da2a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
16 changes: 11 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@

---

## 0.2.1

### Fix

* `UserDefaults` deletes an object if nil is saved.

## 0.2.0

### Features

* This release contains a public interface breaking change: methods to encode/decode objects are moved from the `Koting` protocol to `NSKeyedArchiver` and `NSKeyedUnarchiver` extensions.
* UserDefaults extension.
* `UserDefaults` extension.
* Features section is added to the readme file.
* NSKeyedArchiver extension.
* NSKeyedUnarchiver extension.
* `NSKeyedArchiver` extension.
* `NSKeyedUnarchiver` extension.

## 0.1.1

### Features

* Handy UserDefauls extension
* More unit tests
* Handy `UserDefauls` extension.
* More unit tests.

## 0.1.0

Expand Down
2 changes: 1 addition & 1 deletion Dekoter.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Dekoter'
s.version = '0.2.0'
s.version = '0.2.1'
s.summary = "NSCoding's counterpart for Swift structs."

s.description = <<-DESC
Expand Down
12 changes: 6 additions & 6 deletions Dekoter/Classes/UserDefaults+Dekoter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public extension UserDefaults {
/// - value: The object which implements the `Koting` protocol to store in the defaults database.
/// - defaultName: The key with which to associate with the value.
public func de_set(_ value: Koting?, forKey defaultName: String) {
guard let value = value else {
return
var data: Data? = nil
if let value = value {
data = NSKeyedArchiver.de_archivedData(withRootObject: value)
}
let data = NSKeyedArchiver.de_archivedData(withRootObject: value)
set(data, forKey: defaultName)
}

Expand All @@ -48,10 +48,10 @@ public extension UserDefaults {
/// - value: The array of objects which implement the `Koting` protocol to store in the defaults database.
/// - defaultName: The key with which to associate with the array.
public func de_set(_ value: [Koting]?, forKey defaultName: String) {
guard let value = value else {
return
var data: Data? = nil
if let value = value {
data = NSKeyedArchiver.de_archivedData(withRootObject: value)
}
let data = NSKeyedArchiver.de_archivedData(withRootObject: value)
set(data, forKey: defaultName)
}

Expand Down
11 changes: 11 additions & 0 deletions Example/Tests/UserDefaultTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ class UserDefaultTests: XCTestCase {
super.tearDown()
}

func testUserDefaults_IfNilSaved_RemovesObject() {
let puff: Cat = Cat(name: "Puff", surname: nil, sex: .female, nationality: "US", birthPlace: nil)

userDefaults.de_set(puff, forKey: Key.cat)
userDefaults.de_set(nil as Cat?, forKey: Key.cat)

let cat: Cat? = userDefaults.de_object(forKey: Key.cat)
XCTAssertNil(cat)
}


func testUserDefaults_IfNilSaved_ReturnsNil() {
let missingCat: Cat? = userDefaults.de_object(forKey: Key.cat)
XCTAssertNil(missingCat)
Expand Down

0 comments on commit 012da2a

Please sign in to comment.