Skip to content

Commit

Permalink
weak object container
Browse files Browse the repository at this point in the history
  • Loading branch information
hongxinhope committed Nov 4, 2016
1 parent 4e8c5c1 commit 5491bb4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Example/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>2.3</string>
<string>2.4</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>14</string>
<string>15</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down
2 changes: 1 addition & 1 deletion TBEmptyDataSet.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "TBEmptyDataSet"
s.version = "2.3"
s.version = "2.4"
s.summary = "An extension of UITableView/UICollectionView's super class, it will display a placeholder when the data is empty."

s.homepage = "https://github.com/teambition/TBEmptyDataSet"
Expand Down
4 changes: 4 additions & 0 deletions TBEmptyDataSet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
objects = {

/* Begin PBXBuildFile section */
D331287B1DCC259C00773E72 /* WeakObjectContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D331287A1DCC259C00773E72 /* WeakObjectContainer.swift */; };
D33C7C741C44DCA000E1687A /* TBEmptyDataSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = D33C7C731C44DCA000E1687A /* TBEmptyDataSet.swift */; };
D33C7C781C44DF0A00E1687A /* EmptyDataView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D33C7C771C44DF0A00E1687A /* EmptyDataView.swift */; };
D33C7C7A1C44E17000E1687A /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = D33C7C791C44E17000E1687A /* Constants.swift */; };
D33C7C7C1C44E21700E1687A /* Protocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = D33C7C7B1C44E21700E1687A /* Protocols.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
D331287A1DCC259C00773E72 /* WeakObjectContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WeakObjectContainer.swift; sourceTree = "<group>"; };
D33C7C671C44DBED00E1687A /* TBEmptyDataSet.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TBEmptyDataSet.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D33C7C731C44DCA000E1687A /* TBEmptyDataSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TBEmptyDataSet.swift; sourceTree = "<group>"; };
D33C7C751C44DCC200E1687A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -57,6 +59,7 @@
D33C7C7B1C44E21700E1687A /* Protocols.swift */,
D33C7C771C44DF0A00E1687A /* EmptyDataView.swift */,
D33C7C731C44DCA000E1687A /* TBEmptyDataSet.swift */,
D331287A1DCC259C00773E72 /* WeakObjectContainer.swift */,
);
path = TBEmptyDataSet;
sourceTree = "<group>";
Expand Down Expand Up @@ -149,6 +152,7 @@
buildActionMask = 2147483647;
files = (
D33C7C781C44DF0A00E1687A /* EmptyDataView.swift in Sources */,
D331287B1DCC259C00773E72 /* WeakObjectContainer.swift in Sources */,
D33C7C7C1C44E21700E1687A /* Protocols.swift in Sources */,
D33C7C741C44DCA000E1687A /* TBEmptyDataSet.swift in Sources */,
D33C7C7A1C44E17000E1687A /* Constants.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion TBEmptyDataSet/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>2.3</string>
<string>2.4</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
12 changes: 7 additions & 5 deletions TBEmptyDataSet/TBEmptyDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ extension UIScrollView: UIGestureRecognizerDelegate {
// MARK: - Properties
public var emptyDataSetDataSource: TBEmptyDataSetDataSource? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.emptyDataSetDataSource) as? TBEmptyDataSetDataSource
let container = objc_getAssociatedObject(self, &AssociatedKeys.emptyDataSetDataSource) as? WeakObjectContainer
return container?.object as? TBEmptyDataSetDataSource
}
set {
if let newValue = newValue {
objc_setAssociatedObject(self, &AssociatedKeys.emptyDataSetDataSource, newValue, .OBJC_ASSOCIATION_ASSIGN)
objc_setAssociatedObject(self, &AssociatedKeys.emptyDataSetDataSource, WeakObjectContainer(object: newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

switch self {
case is UITableView:
Expand All @@ -36,11 +37,12 @@ extension UIScrollView: UIGestureRecognizerDelegate {

public var emptyDataSetDelegate: TBEmptyDataSetDelegate? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.emptyDataSetDelegate) as? TBEmptyDataSetDelegate
let container = objc_getAssociatedObject(self, &AssociatedKeys.emptyDataSetDelegate) as? WeakObjectContainer
return container?.object as? TBEmptyDataSetDelegate
}
set {
if let newValue = newValue {
objc_setAssociatedObject(self, &AssociatedKeys.emptyDataSetDelegate, newValue, .OBJC_ASSOCIATION_ASSIGN)
objc_setAssociatedObject(self, &AssociatedKeys.emptyDataSetDelegate, WeakObjectContainer(object: newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
} else {
handlingInvalidEmptyDataSet()
}
Expand All @@ -52,7 +54,7 @@ extension UIScrollView: UIGestureRecognizerDelegate {
return objc_getAssociatedObject(self, &AssociatedKeys.emptyDataView) as? EmptyDataView
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.emptyDataView, newValue, .OBJC_ASSOCIATION_ASSIGN)
objc_setAssociatedObject(self, &AssociatedKeys.emptyDataView, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}

Expand Down
18 changes: 18 additions & 0 deletions TBEmptyDataSet/WeakObjectContainer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// WeakObjectContainer.swift
// TBEmptyDataSet
//
// Created by Xin Hong on 2016/11/4.
// Copyright © 2016年 Teambition. All rights reserved.
//

import UIKit

class WeakObjectContainer: NSObject {
weak var object: AnyObject?

init(object: Any) {
super.init()
self.object = object as AnyObject
}
}

0 comments on commit 5491bb4

Please sign in to comment.